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


tuw_uvc
Author(s):
autogenerated on Sun May 29 2016 02:50:28