00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <utils.hpp>
00025 #include <color.hpp>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 #include <linux/types.h>
00030 #include <string.h>
00031 #include <fcntl.h>
00032 #include <wait.h>
00033 #include <time.h>
00034 #include <limits.h>
00035 #include <huffman.hpp>
00036
00037
00039
00040 #define ISHIFT 11
00041
00042 #define IFIX(a) ((int)((a) * (1 << ISHIFT) + .5))
00043
00044 #ifndef __P
00045 # define __P(x) x
00046 #endif
00047
00048
00049 #define M_BADHUFF -1
00050 #define M_EOF 0x80
00051
00052 struct jpeg_decdata {
00053 int dcts[6 * 64 + 16];
00054 int out[64 * 6];
00055 int dquant[3][64];
00056 };
00057
00058 struct in {
00059 unsigned char *p;
00060 unsigned int bits;
00061 int left;
00062 int marker;
00063 int (*func) __P((void *));
00064 void *data;
00065 };
00066
00067
00068 struct dec_hufftbl;
00069 struct enc_hufftbl;
00070
00071 union hufftblp {
00072 struct dec_hufftbl *dhuff;
00073 struct enc_hufftbl *ehuff;
00074 };
00075
00076 struct scan {
00077 int dc;
00078
00079 union hufftblp hudc;
00080 union hufftblp huac;
00081 int next;
00082
00083 int cid;
00084 int hv;
00085 int tq;
00086 };
00087
00088
00089
00090 #define DECBITS 10
00091
00092 struct dec_hufftbl {
00093 int maxcode[17];
00094 int valptr[16];
00095 unsigned char vals[256];
00096 unsigned int llvals[1 << DECBITS];
00097 };
00098 static int huffman_init(void);
00099 static void decode_mcus
00100 __P((struct in *, int *, int, struct scan *, int *));
00101 static int dec_readmarker __P((struct in *));
00102 static void dec_makehuff
00103 __P((struct dec_hufftbl *, int *, unsigned char *));
00104
00105 static void setinput __P((struct in *, unsigned char *));
00106
00107
00108 #undef PREC
00109 #define PREC int
00110
00111 static void idctqtab __P((unsigned char *, PREC *));
00112
00113 inline static void idct(int *in, int *out, int *quant, long off, int max);
00114
00115 int is_huffman(unsigned char *buf);
00116
00117
00118
00119 static void yuv420pto422(int * out,unsigned char *pic,int width);
00120 static void yuv422pto422(int * out,unsigned char *pic,int width);
00121 static void yuv444pto422(int * out,unsigned char *pic,int width);
00122 static void yuv400pto422(int * out,unsigned char *pic,int width);
00123 typedef void (*ftopict) ( int *out, unsigned char *pic, int width) ;
00124
00125
00126 #define M_SOI 0xd8
00127 #define M_APP0 0xe0
00128 #define M_DQT 0xdb
00129 #define M_SOF0 0xc0
00130 #define M_DHT 0xc4
00131 #define M_DRI 0xdd
00132 #define M_SOS 0xda
00133 #define M_RST0 0xd0
00134 #define M_EOI 0xd9
00135 #define M_COM 0xfe
00136
00137 static unsigned char *datap;
00138
00139 static int getbyte(void)
00140 {
00141 return *datap++;
00142 }
00143
00144 static int getword(void)
00145 {
00146 int c1, c2;
00147 c1 = *datap++;
00148 c2 = *datap++;
00149 return c1 << 8 | c2;
00150 }
00151
00152 struct comp {
00153 int cid;
00154 int hv;
00155 int tq;
00156 };
00157
00158 #define MAXCOMP 4
00159 struct jpginfo {
00160 int nc;
00161 int ns;
00162 int dri;
00163 int nm;
00164 int rm;
00165 };
00166
00167 static struct jpginfo info;
00168 static struct comp comps[MAXCOMP];
00169
00170 static struct scan dscans[MAXCOMP];
00171
00172 static unsigned char quant[4][64];
00173
00174 static struct dec_hufftbl dhuff[4];
00175
00176 #define dec_huffdc (dhuff + 0)
00177 #define dec_huffac (dhuff + 2)
00178
00179 static struct in in;
00180
00181 static int readtables(int till, int *isDHT)
00182 {
00183 int m, l, i, j, lq, pq, tq;
00184 int tc, th, tt;
00185
00186 for (;;) {
00187 if (getbyte() != 0xff)
00188 return -1;
00189 if ((m = getbyte()) == till)
00190 break;
00191
00192 switch (m) {
00193 case 0xc2:
00194 return 0;
00195
00196 case M_DQT:
00197
00198 lq = getword();
00199 while (lq > 2) {
00200 pq = getbyte();
00201 tq = pq & 15;
00202 if (tq > 3)
00203 return -1;
00204 pq >>= 4;
00205 if (pq != 0)
00206 return -1;
00207 for (i = 0; i < 64; i++)
00208 quant[tq][i] = getbyte();
00209 lq -= 64 + 1;
00210 }
00211 break;
00212
00213 case M_DHT:
00214
00215 l = getword();
00216 while (l > 2) {
00217 int hufflen[16], k;
00218 unsigned char huffvals[256];
00219
00220 tc = getbyte();
00221 th = tc & 15;
00222 tc >>= 4;
00223 tt = tc * 2 + th;
00224 if (tc > 1 || th > 1)
00225 return -1;
00226 for (i = 0; i < 16; i++)
00227 hufflen[i] = getbyte();
00228 l -= 1 + 16;
00229 k = 0;
00230 for (i = 0; i < 16; i++) {
00231 for (j = 0; j < hufflen[i]; j++)
00232 huffvals[k++] = getbyte();
00233 l -= hufflen[i];
00234 }
00235 dec_makehuff(dhuff + tt, hufflen, huffvals);
00236 }
00237 *isDHT= 1;
00238 break;
00239
00240 case M_DRI:
00241 printf("find DRI \n");
00242 l = getword();
00243 info.dri = getword();
00244 break;
00245
00246 default:
00247 l = getword();
00248 while (l-- > 2)
00249 getbyte();
00250 break;
00251 }
00252 }
00253
00254 return 0;
00255 }
00256
00257 static void dec_initscans(void)
00258 {
00259 int i;
00260
00261 info.nm = info.dri + 1;
00262 info.rm = M_RST0;
00263 for (i = 0; i < info.ns; i++)
00264 dscans[i].dc = 0;
00265 }
00266
00267 static int dec_checkmarker(void)
00268 {
00269 int i;
00270
00271 if (dec_readmarker(&in) != info.rm)
00272 return -1;
00273 info.nm = info.dri;
00274 info.rm = (info.rm + 1) & ~0x08;
00275 for (i = 0; i < info.ns; i++)
00276 dscans[i].dc = 0;
00277 return 0;
00278 }
00279
00280
00281 int jpeg_decode(unsigned char **pic, unsigned char *buf, int *width,
00282 int *height)
00283 {
00284 struct jpeg_decdata *decdata;
00285 int i, j, m, tac, tdc;
00286 int intwidth, intheight;
00287 int mcusx, mcusy, mx, my;
00288 int ypitch ,xpitch,bpp,pitch,x,y;
00289 int mb;
00290 int max[6];
00291 ftopict convert;
00292 int err = 0;
00293 int isInitHuffman = 0;
00294 decdata = (struct jpeg_decdata *) malloc(sizeof(struct jpeg_decdata));
00295
00296 if (!decdata) {
00297 err = -1;
00298 goto error;
00299 }
00300 if (buf == NULL) {
00301 err = -1;
00302 goto error;
00303 }
00304 datap = buf;
00305 if (getbyte() != 0xff) {
00306 err = ERR_NO_SOI;
00307 goto error;
00308 }
00309 if (getbyte() != M_SOI) {
00310 err = ERR_NO_SOI;
00311 goto error;
00312 }
00313 if (readtables(M_SOF0, &isInitHuffman)) {
00314 err = ERR_BAD_TABLES;
00315 goto error;
00316 }
00317 getword();
00318 i = getbyte();
00319 if (i != 8) {
00320 err = ERR_NOT_8BIT;
00321 goto error;
00322 }
00323 intheight = getword();
00324 intwidth = getword();
00325
00326 if ((intheight & 7) || (intwidth & 7)) {
00327 err = ERR_BAD_WIDTH_OR_HEIGHT;
00328 goto error;
00329 }
00330 info.nc = getbyte();
00331 if (info.nc > MAXCOMP) {
00332 err = ERR_TOO_MANY_COMPPS;
00333 goto error;
00334 }
00335 for (i = 0; i < info.nc; i++) {
00336 int h, v;
00337 comps[i].cid = getbyte();
00338 comps[i].hv = getbyte();
00339 v = comps[i].hv & 15;
00340 h = comps[i].hv >> 4;
00341 comps[i].tq = getbyte();
00342 if (h > 3 || v > 3) {
00343 err = ERR_ILLEGAL_HV;
00344 goto error;
00345 }
00346 if (comps[i].tq > 3) {
00347 err = ERR_QUANT_TABLE_SELECTOR;
00348 goto error;
00349 }
00350 }
00351 if (readtables(M_SOS,&isInitHuffman)) {
00352 err = ERR_BAD_TABLES;
00353 goto error;
00354 }
00355 getword();
00356 info.ns = getbyte();
00357 if (!info.ns){
00358 printf("info ns %d/n",info.ns);
00359 err = ERR_NOT_YCBCR_221111;
00360 goto error;
00361 }
00362 for (i = 0; i < info.ns; i++) {
00363 dscans[i].cid = getbyte();
00364 tdc = getbyte();
00365 tac = tdc & 15;
00366 tdc >>= 4;
00367 if (tdc > 1 || tac > 1) {
00368 err = ERR_QUANT_TABLE_SELECTOR;
00369 goto error;
00370 }
00371 for (j = 0; j < info.nc; j++)
00372 if (comps[j].cid == dscans[i].cid)
00373 break;
00374 if (j == info.nc) {
00375 err = ERR_UNKNOWN_CID_IN_SCAN;
00376 goto error;
00377 }
00378 dscans[i].hv = comps[j].hv;
00379 dscans[i].tq = comps[j].tq;
00380 dscans[i].hudc.dhuff = dec_huffdc + tdc;
00381 dscans[i].huac.dhuff = dec_huffac + tac;
00382 }
00383
00384 i = getbyte();
00385 j = getbyte();
00386 m = getbyte();
00387
00388 if (i != 0 || j != 63 || m != 0) {
00389 printf("hmm FW error,not seq DCT ??\n");
00390 }
00391
00392 if(!isInitHuffman) {
00393 if(huffman_init() < 0)
00394 return -ERR_BAD_TABLES;
00395 }
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410 if (intwidth != *width || intheight != *height || *pic == NULL) {
00411 *width = intwidth;
00412 *height = intheight;
00413
00414 *pic =
00415 (unsigned char *) realloc((unsigned char *) *pic,
00416 (size_t) intwidth * (intheight +
00417 8) * 2);
00418 }
00419
00420
00421 switch (dscans[0].hv) {
00422 case 0x22:
00423 mb=6;
00424 mcusx = *width >> 4;
00425 mcusy = *height >> 4;
00426 bpp=2;
00427 xpitch = 16 * bpp;
00428 pitch = *width * bpp;
00429 ypitch = 16 * pitch;
00430 convert = yuv420pto422;
00431 break;
00432 case 0x21:
00433
00434 mb=4;
00435 mcusx = *width >> 4;
00436 mcusy = *height >> 3;
00437 bpp=2;
00438 xpitch = 16 * bpp;
00439 pitch = *width * bpp;
00440 ypitch = 8 * pitch;
00441 convert = yuv422pto422;
00442 break;
00443 case 0x11:
00444 mcusx = *width >> 3;
00445 mcusy = *height >> 3;
00446 bpp=2;
00447 xpitch = 8 * bpp;
00448 pitch = *width * bpp;
00449 ypitch = 8 * pitch;
00450 if (info.ns==1) {
00451 mb = 1;
00452 convert = yuv400pto422;
00453 } else {
00454 mb=3;
00455 convert = yuv444pto422;
00456 }
00457 break;
00458 default:
00459 err = ERR_NOT_YCBCR_221111;
00460 goto error;
00461 break;
00462 }
00463
00464 idctqtab(quant[dscans[0].tq], decdata->dquant[0]);
00465 idctqtab(quant[dscans[1].tq], decdata->dquant[1]);
00466 idctqtab(quant[dscans[2].tq], decdata->dquant[2]);
00467 setinput(&in, datap);
00468 dec_initscans();
00469
00470 dscans[0].next = 2;
00471 dscans[1].next = 1;
00472 dscans[2].next = 0;
00473 for (my = 0,y=0; my < mcusy; my++,y+=ypitch) {
00474 for (mx = 0,x=0; mx < mcusx; mx++,x+=xpitch) {
00475 if (info.dri && !--info.nm)
00476 if (dec_checkmarker()) {
00477 err = ERR_WRONG_MARKER;
00478 goto error;
00479 }
00480 switch (mb){
00481 case 6: {
00482 decode_mcus(&in, decdata->dcts, mb, dscans, max);
00483 idct(decdata->dcts, decdata->out, decdata->dquant[0],
00484 IFIX(128.5), max[0]);
00485 idct(decdata->dcts + 64, decdata->out + 64,
00486 decdata->dquant[0], IFIX(128.5), max[1]);
00487 idct(decdata->dcts + 128, decdata->out + 128,
00488 decdata->dquant[0], IFIX(128.5), max[2]);
00489 idct(decdata->dcts + 192, decdata->out + 192,
00490 decdata->dquant[0], IFIX(128.5), max[3]);
00491 idct(decdata->dcts + 256, decdata->out + 256,
00492 decdata->dquant[1], IFIX(0.5), max[4]);
00493 idct(decdata->dcts + 320, decdata->out + 320,
00494 decdata->dquant[2], IFIX(0.5), max[5]);
00495
00496 } break;
00497 case 4:
00498 {
00499 decode_mcus(&in, decdata->dcts, mb, dscans, max);
00500 idct(decdata->dcts, decdata->out, decdata->dquant[0],
00501 IFIX(128.5), max[0]);
00502 idct(decdata->dcts + 64, decdata->out + 64,
00503 decdata->dquant[0], IFIX(128.5), max[1]);
00504 idct(decdata->dcts + 128, decdata->out + 256,
00505 decdata->dquant[1], IFIX(0.5), max[4]);
00506 idct(decdata->dcts + 192, decdata->out + 320,
00507 decdata->dquant[2], IFIX(0.5), max[5]);
00508
00509 }
00510 break;
00511 case 3:
00512 decode_mcus(&in, decdata->dcts, mb, dscans, max);
00513 idct(decdata->dcts, decdata->out, decdata->dquant[0],
00514 IFIX(128.5), max[0]);
00515 idct(decdata->dcts + 64, decdata->out + 256,
00516 decdata->dquant[1], IFIX(0.5), max[4]);
00517 idct(decdata->dcts + 128, decdata->out + 320,
00518 decdata->dquant[2], IFIX(0.5), max[5]);
00519
00520
00521 break;
00522 case 1:
00523 decode_mcus(&in, decdata->dcts, mb, dscans, max);
00524 idct(decdata->dcts, decdata->out, decdata->dquant[0],
00525 IFIX(128.5), max[0]);
00526
00527 break;
00528
00529 }
00530 convert(decdata->out,*pic+y+x,pitch);
00531 }
00532 }
00533
00534 m = dec_readmarker(&in);
00535 if (m != M_EOI) {
00536 err = ERR_NO_EOI;
00537 goto error;
00538 }
00539 if (decdata)
00540 free(decdata);
00541 return 0;
00542 error:
00543 if (decdata)
00544 free(decdata);
00545 return err;
00546 }
00547
00548
00549
00550
00551 static int huffman_init(void)
00552 { int tc, th, tt;
00553 const unsigned char *ptr= JPEGHuffmanTable ;
00554 int i, j, l;
00555 l = JPG_HUFFMAN_TABLE_LENGTH ;
00556 while (l > 0) {
00557 int hufflen[16], k;
00558 unsigned char huffvals[256];
00559
00560 tc = *ptr++;
00561 th = tc & 15;
00562 tc >>= 4;
00563 tt = tc * 2 + th;
00564 if (tc > 1 || th > 1)
00565 return -ERR_BAD_TABLES;
00566 for (i = 0; i < 16; i++)
00567 hufflen[i] = *ptr++;
00568 l -= 1 + 16;
00569 k = 0;
00570 for (i = 0; i < 16; i++) {
00571 for (j = 0; j < hufflen[i]; j++)
00572 huffvals[k++] = *ptr++;
00573 l -= hufflen[i];
00574 }
00575 dec_makehuff(dhuff + tt, hufflen, huffvals);
00576 }
00577 return 0;
00578 }
00579
00580 static int fillbits __P((struct in *, int, unsigned int));
00581 static int dec_rec2
00582 __P((struct in *, struct dec_hufftbl *, int *, int, int));
00583
00584 static void setinput(struct in *in, unsigned char *p)
00585
00586
00587 {
00588 in->p = p;
00589 in->left = 0;
00590 in->bits = 0;
00591 in->marker = 0;
00592 }
00593
00594 static int fillbits(struct in *in, int le, unsigned int bi)
00595
00596
00597
00598 {
00599 int b, m;
00600
00601 if (in->marker) {
00602 if (le <= 16)
00603 in->bits = bi << 16, le += 16;
00604 return le;
00605 }
00606 while (le <= 24) {
00607 b = *in->p++;
00608 if (b == 0xff && (m = *in->p++) != 0) {
00609 if (m == M_EOF) {
00610 if (in->func && (m = in->func(in->data)) == 0)
00611 continue;
00612 }
00613 in->marker = m;
00614 if (le <= 16)
00615 bi = bi << 16, le += 16;
00616 break;
00617 }
00618 bi = bi << 8 | b;
00619 le += 8;
00620 }
00621 in->bits = bi;
00622 return le;
00623 }
00624
00625 static int dec_readmarker(struct in *in)
00626
00627 {
00628 int m;
00629
00630 in->left = fillbits(in, in->left, in->bits);
00631 if ((m = in->marker) == 0)
00632 return 0;
00633 in->left = 0;
00634 in->marker = 0;
00635 return m;
00636 }
00637
00638 #define LEBI_DCL int le, bi
00639 #define LEBI_GET(in) (le = in->left, bi = in->bits)
00640 #define LEBI_PUT(in) (in->left = le, in->bits = bi)
00641
00642 #define GETBITS(in, n) ( \
00643 (le < (n) ? le = fillbits(in, le, bi), bi = in->bits : 0), \
00644 (le -= (n)), \
00645 bi >> le & ((1 << (n)) - 1) \
00646 )
00647
00648 #define UNGETBITS(in, n) ( \
00649 le += (n) \
00650 )
00651
00652
00653 static int dec_rec2(struct in *in, struct dec_hufftbl *hu, int *runp, int c, int i)
00654
00655
00656
00657
00658 {
00659 LEBI_DCL;
00660
00661 LEBI_GET(in);
00662 if (i) {
00663 UNGETBITS(in, i & 127);
00664 *runp = i >> 8 & 15;
00665 i >>= 16;
00666 } else {
00667 for (i = DECBITS;
00668 (c = ((c << 1) | GETBITS(in, 1))) >= (hu->maxcode[i]); i++) ;
00669 if (i >= 16) {
00670 in->marker = M_BADHUFF;
00671 return 0;
00672 }
00673 i = hu->vals[hu->valptr[i] + c - hu->maxcode[i - 1] * 2];
00674 *runp = i >> 4;
00675 i &= 15;
00676 }
00677 if (i == 0) {
00678 LEBI_PUT(in);
00679 return 0;
00680 }
00681
00682 c = GETBITS(in, i);
00683 if (c < (1 << (i - 1)))
00684 c += (-1 << i) + 1;
00685 LEBI_PUT(in);
00686 return c;
00687 }
00688
00689 #define DEC_REC(in, hu, r, i) ( \
00690 r = GETBITS(in, DECBITS), \
00691 i = hu->llvals[r], \
00692 i & 128 ? \
00693 ( \
00694 UNGETBITS(in, i & 127), \
00695 r = i >> 8 & 15, \
00696 i >> 16 \
00697 ) \
00698 : \
00699 ( \
00700 LEBI_PUT(in), \
00701 i = dec_rec2(in, hu, &r, r, i), \
00702 LEBI_GET(in), \
00703 i \
00704 ) \
00705 )
00706
00707 static void decode_mcus(struct in *in, int *dct, int n, struct scan *sc, int* maxp)
00708
00709
00710
00711
00712
00713 {
00714 struct dec_hufftbl *hu;
00715 int i, r, t;
00716 LEBI_DCL;
00717
00718 memset(dct, 0, n * 64 * sizeof(*dct));
00719 LEBI_GET(in);
00720 while (n-- > 0) {
00721 hu = sc->hudc.dhuff;
00722 *dct++ = (sc->dc += DEC_REC(in, hu, r, t));
00723
00724 hu = sc->huac.dhuff;
00725 i = 63;
00726 while (i > 0) {
00727 t = DEC_REC(in, hu, r, t);
00728 if (t == 0 && r == 0) {
00729 dct += i;
00730 break;
00731 }
00732 dct += r;
00733 *dct++ = t;
00734 i -= r + 1;
00735 }
00736 *maxp++ = 64 - i;
00737 if (n == sc->next)
00738 sc++;
00739 }
00740 LEBI_PUT(in);
00741 }
00742
00743 static void dec_makehuff(struct dec_hufftbl *hu, int *hufflen, unsigned char *huffvals)
00744
00745
00746
00747 {
00748 int code, k, i, j, d, x, c, v;
00749 for (i = 0; i < (1 << DECBITS); i++)
00750 hu->llvals[i] = 0;
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762 code = 0;
00763 k = 0;
00764 for (i = 0; i < 16; i++, code <<= 1) {
00765 hu->valptr[i] = k;
00766 for (j = 0; j < hufflen[i]; j++) {
00767 hu->vals[k] = *huffvals++;
00768 if (i < DECBITS) {
00769 c = code << (DECBITS - 1 - i);
00770 v = hu->vals[k] & 0x0f;
00771 for (d = 1 << (DECBITS - 1 - i); --d >= 0;) {
00772 if (v + i < DECBITS) {
00773 x = d >> (DECBITS - 1 - v - i);
00774 if (v && x < (1 << (v - 1)))
00775 x += (-1 << v) + 1;
00776 x = x << 16 | (hu->vals[k] & 0xf0) << 4 |
00777 (DECBITS - (i + 1 + v)) | 128;
00778 } else
00779 x = v << 16 | (hu->vals[k] & 0xf0) << 4 |
00780 (DECBITS - (i + 1));
00781 hu->llvals[c | d] = x;
00782 }
00783 }
00784 code++;
00785 k++;
00786 }
00787 hu->maxcode[i] = code;
00788 }
00789 hu->maxcode[16] = 0x20000;
00790 }
00791
00792
00793
00794
00795
00796
00797 #define IMULT(a, b) (((a) * (b)) >> ISHIFT)
00798 #define ITOINT(a) ((a) >> ISHIFT)
00799
00800 #define S22 ((PREC)IFIX(2 * 0.382683432))
00801 #define C22 ((PREC)IFIX(2 * 0.923879532))
00802 #define IC4 ((PREC)IFIX(1 / 0.707106781))
00803
00804 static unsigned char zig2[64] = {
00805 0, 2, 3, 9, 10, 20, 21, 35,
00806 14, 16, 25, 31, 39, 46, 50, 57,
00807 5, 7, 12, 18, 23, 33, 37, 48,
00808 27, 29, 41, 44, 52, 55, 59, 62,
00809 15, 26, 30, 40, 45, 51, 56, 58,
00810 1, 4, 8, 11, 19, 22, 34, 36,
00811 28, 42, 43, 53, 54, 60, 61, 63,
00812 6, 13, 17, 24, 32, 38, 47, 49
00813 };
00814
00815 inline static void idct(int *in, int *out, int *quant, long off, int max)
00816 {
00817 long t0, t1, t2, t3, t4, t5, t6, t7;
00818 long tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
00819 long tmp[64], *tmpp;
00820 int i, j, te;
00821 unsigned char *zig2p;
00822
00823 t0 = off;
00824 if (max == 1) {
00825 t0 += in[0] * quant[0];
00826 for (i = 0; i < 64; i++)
00827 out[i] = ITOINT(t0);
00828 return;
00829 }
00830 zig2p = zig2;
00831 tmpp = tmp;
00832 for (i = 0; i < 8; i++) {
00833 j = *zig2p++;
00834 t0 += in[j] * (long) quant[j];
00835 j = *zig2p++;
00836 t5 = in[j] * (long) quant[j];
00837 j = *zig2p++;
00838 t2 = in[j] * (long) quant[j];
00839 j = *zig2p++;
00840 t7 = in[j] * (long) quant[j];
00841 j = *zig2p++;
00842 t1 = in[j] * (long) quant[j];
00843 j = *zig2p++;
00844 t4 = in[j] * (long) quant[j];
00845 j = *zig2p++;
00846 t3 = in[j] * (long) quant[j];
00847 j = *zig2p++;
00848 t6 = in[j] * (long) quant[j];
00849
00850
00851 if ((t1 | t2 | t3 | t4 | t5 | t6 | t7) == 0) {
00852
00853 tmpp[0 * 8] = t0;
00854 tmpp[1 * 8] = t0;
00855 tmpp[2 * 8] = t0;
00856 tmpp[3 * 8] = t0;
00857 tmpp[4 * 8] = t0;
00858 tmpp[5 * 8] = t0;
00859 tmpp[6 * 8] = t0;
00860 tmpp[7 * 8] = t0;
00861
00862 tmpp++;
00863 t0 = 0;
00864 continue;
00865 }
00866
00867 tmp0 = t0 + t1;
00868 t1 = t0 - t1;
00869 tmp2 = t2 - t3;
00870 t3 = t2 + t3;
00871 tmp2 = IMULT(tmp2, IC4) - t3;
00872 tmp3 = tmp0 + t3;
00873 t3 = tmp0 - t3;
00874 tmp1 = t1 + tmp2;
00875 tmp2 = t1 - tmp2;
00876 tmp4 = t4 - t7;
00877 t7 = t4 + t7;
00878 tmp5 = t5 + t6;
00879 t6 = t5 - t6;
00880 tmp6 = tmp5 - t7;
00881 t7 = tmp5 + t7;
00882 tmp5 = IMULT(tmp6, IC4);
00883 tmp6 = IMULT((tmp4 + t6), S22);
00884 tmp4 = IMULT(tmp4, (C22 - S22)) + tmp6;
00885 t6 = IMULT(t6, (C22 + S22)) - tmp6;
00886 t6 = t6 - t7;
00887 t5 = tmp5 - t6;
00888 t4 = tmp4 - t5;
00889
00890 tmpp[0 * 8] = tmp3 + t7;
00891 tmpp[1 * 8] = tmp1 + t6;
00892 tmpp[2 * 8] = tmp2 + t5;
00893 tmpp[3 * 8] = t3 + t4;
00894 tmpp[4 * 8] = t3 - t4;
00895 tmpp[5 * 8] = tmp2 - t5;
00896 tmpp[6 * 8] = tmp1 - t6;
00897 tmpp[7 * 8] = tmp3 - t7;
00898 tmpp++;
00899 t0 = 0;
00900 }
00901 for (i = 0, j = 0; i < 8; i++) {
00902 t0 = tmp[j + 0];
00903 t1 = tmp[j + 1];
00904 t2 = tmp[j + 2];
00905 t3 = tmp[j + 3];
00906 t4 = tmp[j + 4];
00907 t5 = tmp[j + 5];
00908 t6 = tmp[j + 6];
00909 t7 = tmp[j + 7];
00910 if ((t1 | t2 | t3 | t4 | t5 | t6 | t7) == 0) {
00911 te = ITOINT(t0);
00912 out[j + 0] = te;
00913 out[j + 1] = te;
00914 out[j + 2] = te;
00915 out[j + 3] = te;
00916 out[j + 4] = te;
00917 out[j + 5] = te;
00918 out[j + 6] = te;
00919 out[j + 7] = te;
00920 j += 8;
00921 continue;
00922 }
00923
00924 tmp0 = t0 + t1;
00925 t1 = t0 - t1;
00926 tmp2 = t2 - t3;
00927 t3 = t2 + t3;
00928 tmp2 = IMULT(tmp2, IC4) - t3;
00929 tmp3 = tmp0 + t3;
00930 t3 = tmp0 - t3;
00931 tmp1 = t1 + tmp2;
00932 tmp2 = t1 - tmp2;
00933 tmp4 = t4 - t7;
00934 t7 = t4 + t7;
00935 tmp5 = t5 + t6;
00936 t6 = t5 - t6;
00937 tmp6 = tmp5 - t7;
00938 t7 = tmp5 + t7;
00939 tmp5 = IMULT(tmp6, IC4);
00940 tmp6 = IMULT((tmp4 + t6), S22);
00941 tmp4 = IMULT(tmp4, (C22 - S22)) + tmp6;
00942 t6 = IMULT(t6, (C22 + S22)) - tmp6;
00943 t6 = t6 - t7;
00944 t5 = tmp5 - t6;
00945 t4 = tmp4 - t5;
00946
00947 out[j + 0] = ITOINT(tmp3 + t7);
00948 out[j + 1] = ITOINT(tmp1 + t6);
00949 out[j + 2] = ITOINT(tmp2 + t5);
00950 out[j + 3] = ITOINT(t3 + t4);
00951 out[j + 4] = ITOINT(t3 - t4);
00952 out[j + 5] = ITOINT(tmp2 - t5);
00953 out[j + 6] = ITOINT(tmp1 - t6);
00954 out[j + 7] = ITOINT(tmp3 - t7);
00955 j += 8;
00956 }
00957
00958 }
00959
00960 static unsigned char zig[64] = {
00961 0, 1, 5, 6, 14, 15, 27, 28,
00962 2, 4, 7, 13, 16, 26, 29, 42,
00963 3, 8, 12, 17, 25, 30, 41, 43,
00964 9, 11, 18, 24, 31, 40, 44, 53,
00965 10, 19, 23, 32, 39, 45, 52, 54,
00966 20, 22, 33, 38, 46, 51, 55, 60,
00967 21, 34, 37, 47, 50, 56, 59, 61,
00968 35, 36, 48, 49, 57, 58, 62, 63
00969 };
00970
00971 static PREC aaidct[8] = {
00972 IFIX(0.3535533906), IFIX(0.4903926402),
00973 IFIX(0.4619397663), IFIX(0.4157348062),
00974 IFIX(0.3535533906), IFIX(0.2777851165),
00975 IFIX(0.1913417162), IFIX(0.0975451610)
00976 };
00977
00978
00979 static void idctqtab(unsigned char *qin, PREC *qout)
00980
00981
00982 {
00983 int i, j;
00984
00985 for (i = 0; i < 8; i++)
00986 for (j = 0; j < 8; j++)
00987 qout[zig[i * 8 + j]] = qin[zig[i * 8 + j]] *
00988 IMULT(aaidct[i], aaidct[j]);
00989 }
00990
00991 #define FOUR_TWO_TWO 2 //Y00 Cb Y01 Cr
00992
00993
00994
00995
00996 unsigned int
00997 Pyuv422torgb24(unsigned char * input_ptr, unsigned char * output_ptr, unsigned int image_width, unsigned int image_height)
00998 {
00999 unsigned int i, size;
01000 unsigned char Y, Y1, U, V;
01001 unsigned char *buff = input_ptr;
01002 unsigned char *output_pt = output_ptr;
01003 size = image_width * image_height /2;
01004 for (i = size; i > 0; i--) {
01005
01006
01007 Y = buff[0] ;
01008 U = buff[1] ;
01009 Y1 = buff[2];
01010 V = buff[3];
01011 buff += 4;
01012 *output_pt++ = R_FROMYV(Y,V);
01013 *output_pt++ = G_FROMYUV(Y,U,V);
01014 *output_pt++ = B_FROMYU(Y,U);
01015
01016 *output_pt++ = R_FROMYV(Y1,V);
01017 *output_pt++ = G_FROMYUV(Y1,U,V);
01018 *output_pt++ = B_FROMYU(Y1,U);
01019 }
01020
01021 return FOUR_TWO_TWO;
01022 }
01023
01024 static void yuv420pto422(int * out,unsigned char *pic,int width)
01025 {
01026 int j, k;
01027 unsigned char *pic0, *pic1;
01028 int *outy, *outu, *outv;
01029 int outy1 = 0;
01030 int outy2 = 8;
01031
01032
01033 pic0 = pic;
01034 pic1 = pic + width;
01035 outy = out;
01036 outu = out + 64 * 4;
01037 outv = out + 64 * 5;
01038 for (j = 0; j < 8; j++) {
01039 for (k = 0; k < 8; k++) {
01040 if( k == 4) {
01041 outy1 += 56;
01042 outy2 += 56;
01043 }
01044 *pic0++ = CLIP(outy[outy1]);
01045 *pic0++ = CLIP(128 + *outu);
01046 *pic0++ = CLIP(outy[outy1+1]);
01047 *pic0++ = CLIP(128 + *outv);
01048 *pic1++ = CLIP(outy[outy2]);
01049 *pic1++ = CLIP(128 + *outu);
01050 *pic1++ = CLIP(outy[outy2+1]);
01051 *pic1++ = CLIP(128 + *outv);
01052 outy1 +=2; outy2 += 2; outu++; outv++;
01053 }
01054 if(j==3) {
01055 outy = out + 128;
01056 } else {
01057 outy += 16;
01058 }
01059 outy1 = 0;
01060 outy2 = 8;
01061 pic0 += 2 * (width -16);
01062 pic1 += 2 * (width -16);
01063
01064 }
01065
01066 }
01067 static void yuv422pto422(int * out,unsigned char *pic,int width)
01068 {
01069 int j, k;
01070 unsigned char *pic0, *pic1;
01071 int *outy, *outu, *outv;
01072 int outy1 = 0;
01073 int outy2 = 8;
01074 int outu1 = 0;
01075 int outv1 = 0;
01076
01077
01078 pic0 = pic;
01079 pic1 = pic + width;
01080 outy = out;
01081 outu = out + 64 * 4;
01082 outv = out + 64 * 5;
01083 for (j = 0; j < 4; j++) {
01084 for (k = 0; k < 8; k++) {
01085 if( k == 4) {
01086 outy1 += 56;
01087 outy2 += 56;
01088 }
01089 *pic0++ = CLIP(outy[outy1]);
01090 *pic0++ = CLIP(128 + outu[outu1]);
01091 *pic0++ = CLIP(outy[outy1+1]);
01092 *pic0++ = CLIP(128 + outv[outv1]);
01093 *pic1++ = CLIP(outy[outy2]);
01094 *pic1++ = CLIP(128 + outu[outu1+8]);
01095 *pic1++ = CLIP(outy[outy2+1]);
01096 *pic1++ = CLIP(128 + outv[outv1+8]);
01097 outv1 += 1; outu1 += 1;
01098 outy1 +=2; outy2 +=2;
01099
01100 }
01101
01102 outy += 16;outu +=8; outv +=8;
01103 outv1 = 0; outu1=0;
01104 outy1 = 0;
01105 outy2 = 8;
01106 pic0 += 2 * (width -16);
01107 pic1 += 2 * (width -16);
01108
01109 }
01110
01111 }
01112 static void yuv444pto422(int * out,unsigned char *pic,int width)
01113 {
01114 int j, k;
01115 unsigned char *pic0, *pic1;
01116 int *outy, *outu, *outv;
01117 int outy1 = 0;
01118 int outy2 = 8;
01119 int outu1 = 0;
01120 int outv1 = 0;
01121
01122 pic0 = pic;
01123 pic1 = pic + width;
01124 outy = out;
01125 outu = out + 64 * 4;
01126 outv = out + 64 * 5;
01127 for (j = 0; j < 4; j++) {
01128 for (k = 0; k < 4; k++) {
01129
01130 *pic0++ =CLIP( outy[outy1]);
01131 *pic0++ =CLIP( 128 + outu[outu1]);
01132 *pic0++ =CLIP( outy[outy1+1]);
01133 *pic0++ =CLIP( 128 + outv[outv1]);
01134 *pic1++ =CLIP( outy[outy2]);
01135 *pic1++ =CLIP( 128 + outu[outu1+8]);
01136 *pic1++ =CLIP( outy[outy2+1]);
01137 *pic1++ =CLIP( 128 + outv[outv1+8]);
01138 outv1 += 2; outu1 += 2;
01139 outy1 +=2; outy2 +=2;
01140 }
01141 outy += 16;outu +=16; outv +=16;
01142 outv1 = 0; outu1=0;
01143 outy1 = 0;
01144 outy2 = 8;
01145 pic0 += 2 * (width -8);
01146 pic1 += 2 * (width -8);
01147 }
01148
01149 }
01150 static void yuv400pto422(int * out,unsigned char *pic,int width)
01151 {
01152 int j, k;
01153 unsigned char *pic0, *pic1;
01154 int *outy ;
01155 int outy1 = 0;
01156 int outy2 = 8;
01157 pic0 = pic;
01158 pic1 = pic + width;
01159 outy = out;
01160
01161 for (j = 0; j < 4; j++) {
01162 for (k = 0; k < 4; k++) {
01163 *pic0++ = CLIP(outy[outy1]);
01164 *pic0++ = 128 ;
01165 *pic0++ = CLIP(outy[outy1+1]);
01166 *pic0++ = 128 ;
01167 *pic1++ = CLIP(outy[outy2]);
01168 *pic1++ = 128 ;
01169 *pic1++ = CLIP(outy[outy2+1]);
01170 *pic1++ = 128 ;
01171 outy1 +=2; outy2 +=2;
01172 }
01173 outy += 16;
01174 outy1 = 0;
01175 outy2 = 8;
01176 pic0 += 2 * (width -8);
01177 pic1 += 2 * (width -8);
01178 }
01179
01180 }
01181
01182 int
01183 is_huffman(unsigned char *buf)
01184 {
01185 unsigned char *ptbuf;
01186 ptbuf = buf;
01187 int i = 0;
01188 while (((ptbuf[0] << 8) | ptbuf[1]) != 0xffda){
01189 if(i++ > 2048)
01190 return 0;
01191 if(((ptbuf[0] << 8) | ptbuf[1]) == 0xffc4)
01192 return 1;
01193 ptbuf++;
01194 }
01195 return 0;
01196 }
01197 static void
01198 getPictureName (char *Picture, int fmt)
01199 {
01200 char temp[80];
01201 char *myext[] = { (char*)("pnm"), (char*)("jpg") };
01202 time_t curdate;
01203 struct tm *tdate;
01204 memset (temp, '\0', sizeof (temp));
01205 time (&curdate);
01206 tdate = localtime (&curdate);
01207 snprintf (temp, 26, "P-%02d:%02d:%04d-%02d:%02d:%02d.%s",
01208 tdate->tm_mon + 1, tdate->tm_mday, tdate->tm_year + 1900,
01209 tdate->tm_hour, tdate->tm_min, tdate->tm_sec, myext[fmt]);
01210 memcpy (Picture, temp, strlen (temp));
01211 }
01212 int
01213 get_picture(unsigned char *buf,int size)
01214 {
01215 FILE *file;
01216 unsigned char *ptdeb,*ptcur = buf;
01217 int sizein;
01218 char *name = NULL;
01219 name = (char *)calloc(80,1);
01220 getPictureName (name, 1);
01221 file = fopen(name, "wb");
01222 if (file != NULL) {
01223 if(!is_huffman(buf)){
01224 ptdeb = ptcur = buf;
01225 while (((ptcur[0] << 8) | ptcur[1]) != 0xffc0)
01226 ptcur++;
01227 sizein = ptcur-ptdeb;
01228 fwrite(buf,
01229 sizein, 1, file);
01230 fwrite(dht_data,
01231 DHT_SIZE, 1, file);
01232 fwrite(ptcur,size-sizein,1,file);
01233 } else {
01234 fwrite(ptcur,size,1,file);
01235 }
01236 fclose(file);
01237 }
01238 if(name)
01239 free(name);
01240 return 0;
01241 }
01242
01243 int
01244 get_pictureYV2(unsigned char *buf,int width,int height)
01245 {
01246 FILE *foutpict;
01247 unsigned char *picture = NULL;
01248 char *name = NULL;
01249 name = (char*)calloc(80,1);
01250 getPictureName (name, 0);
01251 picture = (unsigned char *)malloc(width*height*3*sizeof(char));
01252 if(picture){
01253 Pyuv422torgb24(buf, picture, width, height);
01254 }else{
01255 printf(" no room to take a picture \n");
01256 return 0;
01257 }
01258 if(name){
01259 foutpict = fopen (name, "wb");
01260 fprintf (foutpict, "P6\n%d %d\n255\n", width, height);
01261 fwrite (picture, sizeof (char), width * height * 3, foutpict);
01262 fclose (foutpict);
01263 free(name);
01264 }
01265 free(picture);
01266 picture = NULL;
01267 return 0;
01268 }
01269
01270 #define SAT(c) if (c & (~255)) { if (c < 0) c = 0; else c = 255; }
01271
01272 void yuyv_to_rgb24 (int width, int height, unsigned char *src, unsigned char *dst)
01273 {
01274 unsigned char *s;
01275 unsigned char *d;
01276 int l, c;
01277 int r, g, b, cr, cg, cb, y1, y2;
01278
01279 l = height;
01280 s = src;
01281 d = dst;
01282 int i=0;
01283 while (l--) {
01284 c = width >> 1;
01285 while (c--) {
01286 y1 = *s++;
01287 cb = ((*s - 128) * 454) >> 8;
01288 cg = (*s++ - 128) * 88;
01289 y2 = *s++;
01290 cr = ((*s - 128) * 359) >> 8;
01291 cg = (cg + (*s++ - 128) * 183) >> 8;
01292
01293 i++;
01294 if(i%3==2||1){
01295 r = y1 + cr;
01296 b = y1 + cb;
01297 g = y1 - cg;
01298 }else r=g=b=0;
01299 SAT(r);
01300 SAT(g);
01301 SAT(b);
01302
01303 *d++ = r;
01304 *d++ = g;
01305 *d++ = b;
01306
01307 if(i%3==2 ||1){
01308 r = y2 + cr;
01309 b = y2 + cb;
01310 g = y2 - cg;
01311 }else r=g=b=0;
01312 SAT(r);
01313 SAT(g);
01314 SAT(b);
01315
01316 *d++ = r;
01317 *d++ = g;
01318 *d++ = b;
01319 }
01320 }
01321 }
01322
01323