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