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