vc/intel/fsp/fsp2_0/adl: Update FSP header file version to 1432
[coreboot.git] / src / lib / jpeg.c
blobb3d4c89c5366fb627e21f452fef4b70babec5eaa
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 /*
4 * a tiny jpeg decoder.
6 * written in August 2001 by Michael Schroeder <mls@suse.de>
8 */
10 #define __LITTLE_ENDIAN
11 #include <string.h>
12 #include "jpeg.h"
13 #define ISHIFT 11
15 #define IFIX(a) ((int)((a) * (1 << ISHIFT) + .5))
16 #define IMULT(a, b) (((a) * (b)) >> ISHIFT)
17 #define ITOINT(a) ((a) >> ISHIFT)
19 #ifndef __P
20 # define __P(x) x
21 #endif
23 /* special markers */
24 #define M_BADHUFF -1
25 #define M_EOF 0x80
27 struct in {
28 unsigned char *p;
29 unsigned int bits;
30 int left;
31 int marker;
33 int (*func) __P((void *));
34 void *data;
37 /*********************************/
38 struct dec_hufftbl;
39 struct enc_hufftbl;
41 union hufftblp {
42 struct dec_hufftbl *dhuff;
43 struct enc_hufftbl *ehuff;
46 struct scan {
47 int dc; /* old dc value */
49 union hufftblp hudc;
50 union hufftblp huac;
51 int next; /* when to switch to next scan */
53 int cid; /* component id */
54 int hv; /* horiz/vert, copied from comp */
55 int tq; /* quant tbl, copied from comp */
58 /*********************************/
60 #define DECBITS 10 /* seems to be the optimum */
62 struct dec_hufftbl {
63 int maxcode[17];
64 int valptr[16];
65 unsigned char vals[256];
66 unsigned int llvals[1 << DECBITS];
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 *));
73 static void setinput __P((struct in *, unsigned char *));
74 /*********************************/
76 #undef PREC
77 #define PREC int
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));
83 /*********************************/
85 static void initcol __P((PREC[][64]));
87 static void col221111 __P((int *, unsigned char *, int));
88 static void col221111_16 __P((int *, unsigned char *, int));
89 static void col221111_32 __P((int *, unsigned char *, int));
91 /*********************************/
93 #define M_SOI 0xd8
94 #define M_APP0 0xe0
95 #define M_DQT 0xdb
96 #define M_SOF0 0xc0
97 #define M_DHT 0xc4
98 #define M_DRI 0xdd
99 #define M_SOS 0xda
100 #define M_RST0 0xd0
101 #define M_EOI 0xd9
102 #define M_COM 0xfe
104 static unsigned char *datap;
106 static int getbyte(void)
108 return *datap++;
111 static int getword(void)
113 int c1, c2;
114 c1 = *datap++;
115 c2 = *datap++;
116 return c1 << 8 | c2;
119 struct comp {
120 int cid;
121 int hv;
122 int tq;
125 #define MAXCOMP 4
126 struct jpginfo {
127 int nc; /* number of components */
128 int ns; /* number of scans */
129 int dri; /* restart interval */
130 int nm; /* mcus til next marker */
131 int rm; /* next restart marker */
134 static struct jpginfo info;
135 static struct comp comps[MAXCOMP];
137 static struct scan dscans[MAXCOMP];
139 static unsigned char quant[4][64];
141 static struct dec_hufftbl dhuff[4];
143 #define dec_huffdc (dhuff + 0)
144 #define dec_huffac (dhuff + 2)
146 static struct in glob_in;
148 static int readtables(int till)
150 int m, l, i, j, lq, pq, tq;
151 int tc, th, tt;
153 for (;;) {
154 if (getbyte() != 0xff)
155 return -1;
156 m = getbyte();
157 if (m == till)
158 break;
160 switch (m) {
161 case 0xc2:
162 return 0;
164 case M_DQT:
165 lq = getword();
166 while (lq > 2) {
167 pq = getbyte();
168 tq = pq & 15;
169 if (tq > 3)
170 return -1;
171 pq >>= 4;
172 if (pq != 0)
173 return -1;
174 for (i = 0; i < 64; i++)
175 quant[tq][i] = getbyte();
176 lq -= 64 + 1;
178 break;
180 case M_DHT:
181 l = getword();
182 while (l > 2) {
183 int hufflen[16], k;
184 unsigned char huffvals[256];
186 tc = getbyte();
187 th = tc & 15;
188 tc >>= 4;
189 tt = tc * 2 + th;
190 if (tc > 1 || th > 1)
191 return -1;
192 for (i = 0; i < 16; i++)
193 hufflen[i] = getbyte();
194 l -= 1 + 16;
195 k = 0;
196 for (i = 0; i < 16; i++) {
197 for (j = 0; j < hufflen[i]; j++)
198 huffvals[k++] = getbyte();
199 l -= hufflen[i];
201 dec_makehuff(dhuff + tt, hufflen,
202 huffvals);
204 break;
206 case M_DRI:
207 l = getword();
208 info.dri = getword();
209 break;
211 default:
212 l = getword();
213 while (l-- > 2)
214 getbyte();
215 break;
218 return 0;
221 static void dec_initscans(void)
223 int i;
225 info.nm = info.dri + 1;
226 info.rm = M_RST0;
227 for (i = 0; i < info.ns; i++)
228 dscans[i].dc = 0;
231 static int dec_checkmarker(void)
233 int i;
235 if (dec_readmarker(&glob_in) != info.rm)
236 return -1;
237 info.nm = info.dri;
238 info.rm = (info.rm + 1) & ~0x08;
239 for (i = 0; i < info.ns; i++)
240 dscans[i].dc = 0;
241 return 0;
244 void jpeg_fetch_size(unsigned char *buf, int *width, int *height)
246 datap = buf;
247 getbyte();
248 getbyte();
249 readtables(M_SOF0);
250 getword();
251 getbyte();
252 *height = getword();
253 *width = getword();
256 int jpeg_check_size(unsigned char *buf, int width, int height)
258 datap = buf;
259 getbyte();
260 getbyte();
261 readtables(M_SOF0);
262 getword();
263 getbyte();
264 if (height != getword() || width != getword())
265 return 0;
266 return 1;
269 int jpeg_decode(unsigned char *buf, unsigned char *pic,
270 int width, int height, int depth, struct jpeg_decdata *decdata)
272 int i, j, m, tac, tdc;
273 int mcusx, mcusy, mx, my;
274 int max[6];
276 if (!decdata || !buf || !pic)
277 return -1;
278 datap = buf;
279 if (getbyte() != 0xff)
280 return ERR_NO_SOI;
281 if (getbyte() != M_SOI)
282 return ERR_NO_SOI;
283 if (readtables(M_SOF0))
284 return ERR_BAD_TABLES;
285 getword();
286 i = getbyte();
287 if (i != 8)
288 return ERR_NOT_8BIT;
289 if (((getword() + 15) & ~15) != height)
290 return ERR_HEIGHT_MISMATCH;
291 if (((getword() + 15) & ~15) != width)
292 return ERR_WIDTH_MISMATCH;
293 if ((height & 15) || (width & 15))
294 return ERR_BAD_WIDTH_OR_HEIGHT;
295 info.nc = getbyte();
296 if (info.nc > MAXCOMP)
297 return ERR_TOO_MANY_COMPPS;
298 for (i = 0; i < info.nc; i++) {
299 int h, v;
300 comps[i].cid = getbyte();
301 comps[i].hv = getbyte();
302 v = comps[i].hv & 15;
303 h = comps[i].hv >> 4;
304 comps[i].tq = getbyte();
305 if (h > 3 || v > 3)
306 return ERR_ILLEGAL_HV;
307 if (comps[i].tq > 3)
308 return ERR_QUANT_TABLE_SELECTOR;
310 if (readtables(M_SOS))
311 return ERR_BAD_TABLES;
312 getword();
313 info.ns = getbyte();
314 if (info.ns != 3)
315 return ERR_NOT_YCBCR_221111;
316 for (i = 0; i < 3; i++) {
317 dscans[i].cid = getbyte();
318 tdc = getbyte();
319 tac = tdc & 15;
320 tdc >>= 4;
321 if (tdc > 1 || tac > 1)
322 return ERR_QUANT_TABLE_SELECTOR;
323 for (j = 0; j < info.nc; j++)
324 if (comps[j].cid == dscans[i].cid)
325 break;
326 if (j == info.nc)
327 return ERR_UNKNOWN_CID_IN_SCAN;
328 dscans[i].hv = comps[j].hv;
329 dscans[i].tq = comps[j].tq;
330 dscans[i].hudc.dhuff = dec_huffdc + tdc;
331 dscans[i].huac.dhuff = dec_huffac + tac;
334 i = getbyte();
335 j = getbyte();
336 m = getbyte();
338 if (i != 0 || j != 63 || m != 0)
339 return ERR_NOT_SEQUENTIAL_DCT;
341 if (dscans[0].cid != 1 || dscans[1].cid != 2 || dscans[2].cid != 3)
342 return ERR_NOT_YCBCR_221111;
344 if (dscans[0].hv != 0x22 || dscans[1].hv != 0x11
345 || dscans[2].hv != 0x11)
346 return ERR_NOT_YCBCR_221111;
348 mcusx = width >> 4;
349 mcusy = height >> 4;
352 idctqtab(quant[dscans[0].tq], decdata->dquant[0]);
353 idctqtab(quant[dscans[1].tq], decdata->dquant[1]);
354 idctqtab(quant[dscans[2].tq], decdata->dquant[2]);
355 initcol(decdata->dquant);
356 setinput(&glob_in, datap);
358 dec_initscans();
360 dscans[0].next = 6 - 4;
361 dscans[1].next = 6 - 4 - 1;
362 dscans[2].next = 6 - 4 - 1 - 1; /* 411 encoding */
363 for (my = 0; my < mcusy; my++) {
364 for (mx = 0; mx < mcusx; mx++) {
365 if (info.dri && !--info.nm)
366 if (dec_checkmarker())
367 return ERR_WRONG_MARKER;
369 decode_mcus(&glob_in, decdata->dcts, 6, dscans, max);
370 idct(decdata->dcts, decdata->out, decdata->dquant[0],
371 IFIX(128.5), max[0]);
372 idct(decdata->dcts + 64, decdata->out + 64,
373 decdata->dquant[0], IFIX(128.5), max[1]);
374 idct(decdata->dcts + 128, decdata->out + 128,
375 decdata->dquant[0], IFIX(128.5), max[2]);
376 idct(decdata->dcts + 192, decdata->out + 192,
377 decdata->dquant[0], IFIX(128.5), max[3]);
378 idct(decdata->dcts + 256, decdata->out + 256,
379 decdata->dquant[1], IFIX(0.5), max[4]);
380 idct(decdata->dcts + 320, decdata->out + 320,
381 decdata->dquant[2], IFIX(0.5), max[5]);
383 switch (depth) {
384 case 32:
385 col221111_32(decdata->out, pic
386 + (my * 16 * mcusx + mx) * 16 * 4,
387 mcusx * 16 * 4);
388 break;
389 case 24:
390 col221111(decdata->out, pic
391 + (my * 16 * mcusx + mx) * 16 * 3,
392 mcusx * 16 * 3);
393 break;
394 case 16:
395 col221111_16(decdata->out, pic
396 + (my * 16 * mcusx + mx) * (16 * 2),
397 mcusx * (16 * 2));
398 break;
399 default:
400 return ERR_DEPTH_MISMATCH;
405 m = dec_readmarker(&glob_in);
406 if (m != M_EOI)
407 return ERR_NO_EOI;
409 return 0;
412 /****************************************************************/
413 /************** huffman decoder ***************/
414 /****************************************************************/
416 static int fillbits __P((struct in *, int, unsigned int));
417 static int dec_rec2
418 __P((struct in *, struct dec_hufftbl *, int *, int, int));
420 static void setinput(struct in *in, unsigned char *p)
422 in->p = p;
423 in->left = 0;
424 in->bits = 0;
425 in->marker = 0;
428 static int fillbits(struct in *in, int le, unsigned int bi)
430 int b, m;
432 if (in->marker) {
433 if (le <= 16)
434 in->bits = bi << 16, le += 16;
435 return le;
437 while (le <= 24) {
438 b = *in->p++;
439 if (b == 0xff) {
440 m = *in->p++;
441 if (m != 0) {
442 if (m == M_EOF) {
443 if (in->func) {
444 m = in->func(in->data);
445 if (m == 0)
446 continue;
449 in->marker = m;
450 if (le <= 16)
451 bi = bi << 16, le += 16;
452 break;
455 bi = bi << 8 | b;
456 le += 8;
458 in->bits = bi; /* tmp... 2 return values needed */
459 return le;
462 static int dec_readmarker(struct in *in)
464 int m;
466 in->left = fillbits(in, in->left, in->bits);
467 m = in->marker;
468 if (m == 0)
469 return 0;
470 in->left = 0;
471 in->marker = 0;
472 return m;
475 #define LEBI_DCL int le, bi
476 #define LEBI_GET(in) (le = in->left, bi = in->bits)
477 #define LEBI_PUT(in) (in->left = le, in->bits = bi)
479 #define GETBITS(in, n) ( \
480 (le < (n) ? le = fillbits(in, le, bi), bi = in->bits : 0), \
481 (le -= (n)), \
482 bi >> le & ((1 << (n)) - 1) \
485 #define UNGETBITS(in, n) ( \
486 le += (n) \
490 static int dec_rec2(struct in *in, struct dec_hufftbl *hu, int *runp, int c,
491 int i)
493 LEBI_DCL;
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)))
502 >= (hu->maxcode[i]); i++)
504 if (i >= 16) {
505 in->marker = M_BADHUFF;
506 return 0;
508 i = hu->vals[hu->valptr[i] + c - hu->maxcode[i - 1] * 2];
509 *runp = i >> 4;
510 i &= 15;
512 if (i == 0) { /* sigh, 0xf0 is 11 bit */
513 LEBI_PUT(in);
514 return 0;
516 /* receive part */
517 c = GETBITS(in, i);
518 if (c < (1 << (i - 1)))
519 c += (-1 << i) + 1;
520 LEBI_PUT(in);
521 return c;
524 #define DEC_REC(in, hu, r, i) ( \
525 r = GETBITS(in, DECBITS), \
526 i = hu->llvals[r], \
527 i & 128 ? \
529 UNGETBITS(in, i & 127), \
530 r = i >> 8 & 15, \
531 i >> 16 \
535 LEBI_PUT(in), \
536 i = dec_rec2(in, hu, &r, r, i), \
537 LEBI_GET(in), \
542 static void decode_mcus(struct in *in, int *dct, int n, struct scan *sc,
543 int *maxp)
545 struct dec_hufftbl *hu;
546 int i, r, t;
547 LEBI_DCL;
549 memset(dct, 0, n * 64 * sizeof(*dct));
550 LEBI_GET(in);
551 while (n-- > 0) {
552 hu = sc->hudc.dhuff;
553 *dct++ = (sc->dc += DEC_REC(in, hu, r, t));
555 hu = sc->huac.dhuff;
556 i = 63;
557 while (i > 0) {
558 t = DEC_REC(in, hu, r, t);
559 if (t == 0 && r == 0) {
560 dct += i;
561 break;
563 dct += r;
564 *dct++ = t;
565 i -= r + 1;
567 *maxp++ = 64 - i;
568 if (n == sc->next)
569 sc++;
571 LEBI_PUT(in);
574 static void dec_makehuff(struct dec_hufftbl *hu, int *hufflen,
575 unsigned char *huffvals)
577 int code, k, i, j, d, x, c, v;
578 for (i = 0; i < (1 << DECBITS); i++)
579 hu->llvals[i] = 0;
582 * llvals layout:
584 * value v already known, run r, backup u bits:
585 * vvvvvvvvvvvvvvvv 0000 rrrr 1 uuuuuuu
586 * value unknown, size b bits, run r, backup u bits:
587 * 000000000000bbbb 0000 rrrr 0 uuuuuuu
588 * value and size unknown:
589 * 0000000000000000 0000 0000 0 0000000
591 code = 0;
592 k = 0;
593 for (i = 0; i < 16; i++, code <<= 1) { /* sizes */
594 hu->valptr[i] = k;
595 for (j = 0; j < hufflen[i]; j++) {
596 hu->vals[k] = *huffvals++;
597 if (i < DECBITS) {
598 c = code << (DECBITS - 1 - i);
599 v = hu->vals[k] & 0x0f; /* size */
600 for (d = 1 << (DECBITS - 1 - i); --d >= 0;) {
601 /* both fit in table */
602 if (v + i < DECBITS) {
603 x = d >> (DECBITS - 1 - v -
605 if (v && x < (1 << (v - 1)))
606 x += (-1 << v) + 1;
607 x = x << 16 | (hu->vals[k]
608 & 0xf0) << 4 |
609 (DECBITS - (i + 1 + v))
610 | 128;
611 } else
612 x = v << 16 | (hu->vals[k]
613 & 0xf0) << 4 |
614 (DECBITS - (i + 1));
615 hu->llvals[c | d] = x;
618 code++;
619 k++;
621 hu->maxcode[i] = code;
623 hu->maxcode[16] = 0x20000; /* always terminate decode */
626 /****************************************************************/
627 /************** idct ***************/
628 /****************************************************************/
630 #define ONE ((PREC)IFIX(1.))
631 #define S2 ((PREC)IFIX(0.382683432))
632 #define C2 ((PREC)IFIX(0.923879532))
633 #define C4 ((PREC)IFIX(0.707106781))
635 #define S22 ((PREC)IFIX(2 * 0.382683432))
636 #define C22 ((PREC)IFIX(2 * 0.923879532))
637 #define IC4 ((PREC)IFIX(1 / 0.707106781))
639 #define C3IC1 ((PREC)IFIX(0.847759065)) /* c3/c1 */
640 #define C5IC1 ((PREC)IFIX(0.566454497)) /* c5/c1 */
641 #define C7IC1 ((PREC)IFIX(0.198912367)) /* c7/c1 */
643 #define XPP(a, b) (t = a + b, b = a - b, a = t)
644 #define XMP(a, b) (t = a - b, b = a + b, a = t)
645 #define XPM(a, b) (t = a + b, b = b - a, a = t)
647 #define ROT(a, b, s, c) (t = IMULT(a + b, s), \
648 a = IMULT(a, c - s) + t, \
649 b = IMULT(b, c + s) - t)
651 #define IDCT \
653 XPP(t0, t1), \
654 XMP(t2, t3), \
655 t2 = IMULT(t2, IC4) - t3, \
656 XPP(t0, t3), \
657 XPP(t1, t2), \
658 XMP(t4, t7), \
659 XPP(t5, t6), \
660 XMP(t5, t7), \
661 t5 = IMULT(t5, IC4), \
662 ROT(t4, t6, S22, C22), \
663 t6 -= t7, \
664 t5 -= t6, \
665 t4 -= t5, \
666 XPP(t0, t7), \
667 XPP(t1, t6), \
668 XPP(t2, t5), \
669 XPP(t3, t4) \
672 static unsigned char zig2[64] = {
673 0, 2, 3, 9, 10, 20, 21, 35,
674 14, 16, 25, 31, 39, 46, 50, 57,
675 5, 7, 12, 18, 23, 33, 37, 48,
676 27, 29, 41, 44, 52, 55, 59, 62,
677 15, 26, 30, 40, 45, 51, 56, 58,
678 1, 4, 8, 11, 19, 22, 34, 36,
679 28, 42, 43, 53, 54, 60, 61, 63,
680 6, 13, 17, 24, 32, 38, 47, 49
683 void idct(int *in, int *out, PREC *lquant, PREC off, int max)
685 PREC t0, t1, t2, t3, t4, t5, t6, t7, t;
686 PREC tmp[64], *tmpp;
687 int i, j;
688 unsigned char *zig2p;
690 t0 = off;
691 if (max == 1) {
692 t0 += in[0] * lquant[0];
693 for (i = 0; i < 64; i++)
694 out[i] = ITOINT(t0);
695 return;
697 zig2p = zig2;
698 tmpp = tmp;
699 for (i = 0; i < 8; i++) {
700 j = *zig2p++;
701 t0 += in[j] * lquant[j];
702 j = *zig2p++;
703 t5 = in[j] * lquant[j];
704 j = *zig2p++;
705 t2 = in[j] * lquant[j];
706 j = *zig2p++;
707 t7 = in[j] * lquant[j];
708 j = *zig2p++;
709 t1 = in[j] * lquant[j];
710 j = *zig2p++;
711 t4 = in[j] * lquant[j];
712 j = *zig2p++;
713 t3 = in[j] * lquant[j];
714 j = *zig2p++;
715 t6 = in[j] * lquant[j];
716 IDCT;
717 tmpp[0 * 8] = t0;
718 tmpp[1 * 8] = t1;
719 tmpp[2 * 8] = t2;
720 tmpp[3 * 8] = t3;
721 tmpp[4 * 8] = t4;
722 tmpp[5 * 8] = t5;
723 tmpp[6 * 8] = t6;
724 tmpp[7 * 8] = t7;
725 tmpp++;
726 t0 = 0;
728 for (i = 0; i < 8; i++) {
729 t0 = tmp[8 * i + 0];
730 t1 = tmp[8 * i + 1];
731 t2 = tmp[8 * i + 2];
732 t3 = tmp[8 * i + 3];
733 t4 = tmp[8 * i + 4];
734 t5 = tmp[8 * i + 5];
735 t6 = tmp[8 * i + 6];
736 t7 = tmp[8 * i + 7];
737 IDCT;
738 out[8 * i + 0] = ITOINT(t0);
739 out[8 * i + 1] = ITOINT(t1);
740 out[8 * i + 2] = ITOINT(t2);
741 out[8 * i + 3] = ITOINT(t3);
742 out[8 * i + 4] = ITOINT(t4);
743 out[8 * i + 5] = ITOINT(t5);
744 out[8 * i + 6] = ITOINT(t6);
745 out[8 * i + 7] = ITOINT(t7);
749 static unsigned char zig[64] = {
750 0, 1, 5, 6, 14, 15, 27, 28,
751 2, 4, 7, 13, 16, 26, 29, 42,
752 3, 8, 12, 17, 25, 30, 41, 43,
753 9, 11, 18, 24, 31, 40, 44, 53,
754 10, 19, 23, 32, 39, 45, 52, 54,
755 20, 22, 33, 38, 46, 51, 55, 60,
756 21, 34, 37, 47, 50, 56, 59, 61,
757 35, 36, 48, 49, 57, 58, 62, 63
760 static PREC aaidct[8] = {
761 IFIX(0.3535533906), IFIX(0.4903926402),
762 IFIX(0.4619397663), IFIX(0.4157348062),
763 IFIX(0.3535533906), IFIX(0.2777851165),
764 IFIX(0.1913417162), IFIX(0.0975451610)
768 static void idctqtab(unsigned char *qin, PREC *qout)
770 int i, j;
772 for (i = 0; i < 8; i++)
773 for (j = 0; j < 8; j++)
774 qout[zig[i * 8 + j]] = qin[zig[i * 8 + j]] *
775 IMULT(aaidct[i], aaidct[j]);
778 static void scaleidctqtab(PREC *q, PREC sc)
780 int i;
782 for (i = 0; i < 64; i++)
783 q[i] = IMULT(q[i], sc);
786 /****************************************************************/
787 /************** color decoder ***************/
788 /****************************************************************/
790 #define ROUND
793 * YCbCr Color transformation:
795 * y:0..255 Cb:-128..127 Cr:-128..127
797 * R = Y + 1.40200 * Cr
798 * G = Y - 0.34414 * Cb - 0.71414 * Cr
799 * B = Y + 1.77200 * Cb
801 * =>
802 * Cr *= 1.40200;
803 * Cb *= 1.77200;
804 * Cg = 0.19421 * Cb + .50937 * Cr;
805 * R = Y + Cr;
806 * G = Y - Cg;
807 * B = Y + Cb;
809 * =>
810 * Cg = (50 * Cb + 130 * Cr + 128) >> 8;
813 static void initcol(PREC q[][64])
815 scaleidctqtab(q[1], IFIX(1.77200));
816 scaleidctqtab(q[2], IFIX(1.40200));
819 /* This is optimized for the stupid sun SUNWspro compiler. */
820 #define STORECLAMP(a, x) \
822 (a) = (x), \
823 (unsigned int)(x) >= 256 ? \
824 ((a) = (x) < 0 ? 0 : 255) \
829 #define CLAMP(x) ((unsigned int)(x) >= 256 ? ((x) < 0 ? 0 : 255) : (x))
831 #ifdef ROUND
833 #define CBCRCG(yin, xin) \
835 cb = outc[0 + yin * 8 + xin], \
836 cr = outc[64 + yin * 8 + xin], \
837 cg = (50 * cb + 130 * cr + 128) >> 8 \
840 #else
842 #define CBCRCG(yin, xin) \
844 cb = outc[0 + yin*8 + xin], \
845 cr = outc[64 + yin*8 + xin], \
846 cg = (3 * cb + 8 * cr) >> 4 \
849 #endif
851 #define PIC(yin, xin, p, xout) \
853 y = outy[(yin) * 8 + xin], \
854 STORECLAMP(p[(xout) * 3 + 0], y + cr), \
855 STORECLAMP(p[(xout) * 3 + 1], y - cg), \
856 STORECLAMP(p[(xout) * 3 + 2], y + cb) \
859 #ifdef __LITTLE_ENDIAN
860 #define PIC_16(yin, xin, p, xout, add) \
862 y = outy[(yin) * 8 + xin], \
863 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 8) | \
864 ((CLAMP(y - cg + add) & 0xfc) << 3) | \
865 ((CLAMP(y + cb + add*2+1)) >> 3), \
866 p[(xout) * 2 + 0] = y & 0xff, \
867 p[(xout) * 2 + 1] = y >> 8 \
869 #else
870 #ifdef CONFIG_PPC
871 #define PIC_16(yin, xin, p, xout, add) \
873 y = outy[(yin) * 8 + xin], \
874 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 7) | \
875 ((CLAMP(y - cg + add*2+1) & 0xf8) << 2) | \
876 ((CLAMP(y + cb + add*2+1)) >> 3), \
877 p[(xout) * 2 + 0] = y >> 8, \
878 p[(xout) * 2 + 1] = y & 0xff \
880 #else
881 #define PIC_16(yin, xin, p, xout, add) \
883 y = outy[(yin) * 8 + xin], \
884 y = ((CLAMP(y + cr + add*2+1) & 0xf8) << 8) | \
885 ((CLAMP(y - cg + add) & 0xfc) << 3) | \
886 ((CLAMP(y + cb + add*2+1)) >> 3), \
887 p[(xout) * 2 + 0] = y >> 8, \
888 p[(xout) * 2 + 1] = y & 0xff \
890 #endif
891 #endif
893 #define PIC_32(yin, xin, p, xout) \
895 y = outy[(yin) * 8 + xin], \
896 STORECLAMP(p[(xout) * 4 + 0], y + cr), \
897 STORECLAMP(p[(xout) * 4 + 1], y - cg), \
898 STORECLAMP(p[(xout) * 4 + 2], y + cb), \
899 p[(xout) * 4 + 3] = 0 \
902 #define PIC221111(xin) \
904 CBCRCG(0, xin), \
905 PIC(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0), \
906 PIC(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1), \
907 PIC(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0), \
908 PIC(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1) \
911 #define PIC221111_16(xin) \
913 CBCRCG(0, xin), \
914 PIC_16(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0, 3), \
915 PIC_16(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1, 0), \
916 PIC_16(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0, 1), \
917 PIC_16(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1, 2) \
920 #define PIC221111_32(xin) \
922 CBCRCG(0, xin), \
923 PIC_32(xin / 4 * 8 + 0, (xin & 3) * 2 + 0, pic0, xin * 2 + 0), \
924 PIC_32(xin / 4 * 8 + 0, (xin & 3) * 2 + 1, pic0, xin * 2 + 1), \
925 PIC_32(xin / 4 * 8 + 1, (xin & 3) * 2 + 0, pic1, xin * 2 + 0), \
926 PIC_32(xin / 4 * 8 + 1, (xin & 3) * 2 + 1, pic1, xin * 2 + 1) \
929 static void col221111(int *out, unsigned char *pic, int width)
931 int i, j, k;
932 unsigned char *pic0, *pic1;
933 int *outy, *outc;
934 int cr, cg, cb, y;
936 pic0 = pic;
937 pic1 = pic + width;
938 outy = out;
939 outc = out + 64 * 4;
940 for (i = 2; i > 0; i--) {
941 for (j = 4; j > 0; j--) {
942 for (k = 0; k < 8; k++)
943 PIC221111(k);
944 outc += 8;
945 outy += 16;
946 pic0 += 2 * width;
947 pic1 += 2 * width;
949 outy += 64 * 2 - 16 * 4;
953 static void col221111_16(int *out, unsigned char *pic, int width)
955 int i, j, k;
956 unsigned char *pic0, *pic1;
957 int *outy, *outc;
958 int cr, cg, cb, y;
960 pic0 = pic;
961 pic1 = pic + width;
962 outy = out;
963 outc = out + 64 * 4;
964 for (i = 2; i > 0; i--) {
965 for (j = 4; j > 0; j--) {
966 for (k = 0; k < 8; k++)
967 PIC221111_16(k);
968 outc += 8;
969 outy += 16;
970 pic0 += 2 * width;
971 pic1 += 2 * width;
973 outy += 64 * 2 - 16 * 4;
977 static void col221111_32(int *out, unsigned char *pic, int width)
979 int i, j, k;
980 unsigned char *pic0, *pic1;
981 int *outy, *outc;
982 int cr, cg, cb, y;
984 pic0 = pic;
985 pic1 = pic + width;
986 outy = out;
987 outc = out + 64 * 4;
988 for (i = 2; i > 0; i--) {
989 for (j = 4; j > 0; j--) {
990 for (k = 0; k < 8; k++)
991 PIC221111_32(k);
992 outc += 8;
993 outy += 16;
994 pic0 += 2 * width;
995 pic1 += 2 * width;
997 outy += 64 * 2 - 16 * 4;