"Fossies" - the Fresh Open Source Software Archive

Member "quicktime4linux-2.3/fastjpg.c" (9 Jan 2007, 48039 Bytes) of package /linux/privat/old/quicktime4linux-2.3-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "fastjpg.h"
    2 #include "fastjpgmacro.h"
    3 
    4 /* JPEG decoder from XAnim which ended up 10% slower than libjpeg */
    5 
    6 /* JPEG MARKERS */
    7 #define   M_SOF0    0xc0
    8 #define   M_SOF1    0xc1
    9 #define   M_SOF2    0xc2
   10 #define   M_SOF3    0xc3
   11 #define   M_SOF5    0xc5
   12 #define   M_SOF6    0xc6
   13 #define   M_SOF7    0xc7
   14 #define   M_JPG     0xc8
   15 #define   M_SOF9    0xc9
   16 #define   M_SOF10   0xca
   17 #define   M_SOF11   0xcb
   18 #define   M_SOF13   0xcd
   19 #define   M_SOF14   0xce
   20 #define   M_SOF15   0xcf
   21 #define   M_DHT     0xc4
   22 #define   M_DAC     0xcc
   23 #define   M_RST0    0xd0
   24 #define   M_RST1    0xd1
   25 #define   M_RST2    0xd2
   26 #define   M_RST3    0xd3
   27 #define   M_RST4    0xd4
   28 #define   M_RST5    0xd5
   29 #define   M_RST6    0xd6
   30 #define   M_RST7    0xd7
   31 #define   M_SOI     0xd8
   32 #define   M_EOI     0xd9
   33 #define   M_SOS     0xda
   34 #define   M_DQT     0xdb
   35 #define   M_DNL     0xdc
   36 #define   M_DRI     0xdd
   37 #define   M_DHP     0xde
   38 #define   M_EXP     0xdf
   39 #define   M_APP0    0xe0
   40 #define   M_APP1    0xe1
   41 #define   M_APP2    0xe2
   42 #define   M_APP3    0xe3
   43 #define   M_APP4    0xe4
   44 #define   M_APP5    0xe5
   45 #define   M_APP6    0xe6
   46 #define   M_APP7    0xe7
   47 #define   M_APP8    0xe8
   48 #define   M_APP9    0xe9
   49 #define   M_APP10   0xea
   50 #define   M_APP11   0xeb
   51 #define   M_APP12   0xec
   52 #define   M_APP13   0xed
   53 #define   M_APP14   0xee
   54 #define   M_APP15   0xef
   55 #define   M_JPG0    0xf0
   56 #define   M_JPG13   0xfd
   57 #define   M_COM     0xfe
   58 #define   M_TEM     0x01
   59 #define   M_ERROR   0x100
   60 
   61 static long JJ_ZAG[DCTSIZE2 + 16] = 
   62 {
   63   0,  1,  8, 16,  9,  2,  3, 10,
   64  17, 24, 32, 25, 18, 11,  4,  5,
   65  12, 19, 26, 33, 40, 48, 41, 34,
   66  27, 20, 13,  6,  7, 14, 21, 28,
   67  35, 42, 49, 56, 57, 50, 43, 36,
   68  29, 22, 15, 23, 30, 37, 44, 51,
   69  58, 59, 52, 45, 38, 31, 39, 46,
   70  53, 60, 61, 54, 47, 55, 62, 63,
   71   0,  0,  0,  0,  0,  0,  0,  0, /* extra entries in case k>63 below */
   72   0,  0,  0,  0,  0,  0,  0,  0
   73 };
   74 
   75 static char std_luminance_quant_tbl[64] = {
   76   16,  11,  12,  14,  12,  10,  16,  14,
   77   13,  14,  18,  17,  16,  19,  24,  40,
   78   26,  24,  22,  22,  24,  49,  35,  37,
   79   29,  40,  58,  51,  61,  60,  57,  51,
   80   56,  55,  64,  72,  92,  78,  64,  68,
   81   87,  69,  55,  56,  80, 109,  81,  87,
   82   95,  98, 103, 104, 103,  62,  77, 113,
   83  121, 112, 100, 120,  92, 101, 103,  99
   84 };
   85  
   86 static char std_chrominance_quant_tbl[64] = {
   87   17,  18,  18,  24,  21,  24,  47,  26,
   88   26,  47,  99,  66,  56,  66,  99,  99,
   89   99,  99,  99,  99,  99,  99,  99,  99,
   90   99,  99,  99,  99,  99,  99,  99,  99,
   91   99,  99,  99,  99,  99,  99,  99,  99,
   92   99,  99,  99,  99,  99,  99,  99,  99,
   93   99,  99,  99,  99,  99,  99,  99,  99,
   94   99,  99,  99,  99,  99,  99,  99,  99
   95 };
   96 
   97 int quicktime_fastjpg_skip(quicktime_jpeg_t *jpeg_info, long len)
   98 {
   99     if(len > jpeg_info->chunk_size)
  100         jpeg_info->chunk += jpeg_info->chunk_size;
  101     else
  102         jpeg_info->chunk += len;
  103     
  104     return 0;
  105 }
  106 
  107 int quicktime_fastjpg_readbyte(quicktime_jpeg_t *jpeg_info)
  108 {
  109     if(jpeg_info->chunk_size > 0)
  110     {
  111         jpeg_info->chunk_size--;
  112         return *(jpeg_info->chunk++);
  113     }
  114     else
  115         return 0;
  116 }
  117 
  118 int quicktime_fastjpg_readint16(quicktime_jpeg_t *jpeg_info)
  119 {
  120     if(jpeg_info->chunk_size > 1)
  121     {
  122         jpeg_info->chunk_size -= 2;
  123         jpeg_info->chunk += 2;
  124         return ((int)jpeg_info->chunk[-2] << 8) | (unsigned char)jpeg_info->chunk[-1];
  125     }
  126     else
  127         return 0;
  128 }
  129 
  130 int quicktime_fastjpg_readint32(quicktime_jpeg_t *jpeg_info)
  131 {
  132     if(jpeg_info->chunk_size > 3)
  133     {
  134         jpeg_info->chunk_size -= 4;
  135         return (((unsigned long)*(jpeg_info->chunk++) << 24) | 
  136                 ((unsigned long)*(jpeg_info->chunk++) << 16) |
  137                 ((unsigned long)*(jpeg_info->chunk++) << 8) |
  138                 ((unsigned long)*(jpeg_info->chunk++)));
  139     }
  140     else
  141         return 0;
  142 }
  143 
  144 int quicktime_fastjpg_eof(quicktime_jpeg_t *jpeg_info)
  145 {
  146     if(jpeg_info->chunk_size > 0) 
  147     return 0;
  148     else
  149     return 1;
  150 }
  151 
  152 int quicktime_fastjpg_init_limittable(quicktime_jpeg_t *jpeg_info)
  153 {
  154     unsigned char *table;
  155     int i;
  156 
  157     jpeg_info->jpg_samp_limit = (unsigned char *)malloc((5 * (MAXJSAMPLE + 1) + CENTERJSAMPLE));
  158     jpeg_info->byte_limit = jpeg_info->jpg_samp_limit + MAXJSAMPLE + 1;
  159 
  160 /* create negative subscripts for simple table */
  161     table = jpeg_info->jpg_samp_limit + MAXJSAMPLE + 1;
  162 
  163 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
  164     memset(table - (MAXJSAMPLE + 1), 0, (MAXJSAMPLE + 1));
  165 
  166 /* Main part of "simple" table: limit[x] = x */
  167     for(i = 0; i <= MAXJSAMPLE; i++) table[i] = (unsigned char)i;
  168 
  169 /* Point to where post-IDCT table starts */
  170     table += CENTERJSAMPLE;
  171 
  172 /* End of simple table, rest of first half of post-IDCT table */
  173 
  174     for(i = CENTERJSAMPLE; i < 2 * (MAXJSAMPLE + 1); i++) table[i] = MAXJSAMPLE;
  175 
  176 /* Second half of post-IDCT table */
  177     memset(table + (2 * (MAXJSAMPLE + 1)), 0, (2 * (MAXJSAMPLE + 1) - CENTERJSAMPLE));
  178     memcpy(table + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE),
  179         (char*)(jpeg_info->jpg_samp_limit + (MAXJSAMPLE + 1)), CENTERJSAMPLE);
  180 }
  181 
  182 int quicktime_fastjpg_init_yuv(quicktime_jpeg_t *jpeg_info)
  183 {
  184     long i;
  185     float t_ub,  t_vr,  t_ug,  t_vg;
  186     float t2_ub, t2_vr, t2_ug, t2_vg;
  187 
  188     jpeg_info->yuvtabs.YUV_Y_tab = (long*)malloc(256 * sizeof(long));
  189     jpeg_info->yuvtabs.YUV_UB_tab = (long*)malloc(256 * sizeof(long));
  190     jpeg_info->yuvtabs.YUV_VR_tab = (long*)malloc(256 * sizeof(long));
  191     jpeg_info->yuvtabs.YUV_UG_tab = (long*)malloc(256 * sizeof(long));
  192     jpeg_info->yuvtabs.YUV_VG_tab = (long*)malloc(256 * sizeof(long));
  193 
  194     t_ub = (1.77200 / 2.0) * (float)(1 << 6) + 0.5;
  195     t_vr = (1.40200 / 2.0) * (float)(1 << 6) + 0.5;
  196     t_ug = (0.34414 / 2.0) * (float)(1 << 6) + 0.5;
  197     t_vg = (0.71414 / 2.0) * (float)(1 << 6) + 0.5;
  198     t2_ub = (1.4 * 1.77200 / 2.0) * (float)(1 << 6) + 0.5;
  199     t2_vr = (1.4 * 1.40200 / 2.0) * (float)(1 << 6) + 0.5;
  200     t2_ug = (1.4 * 0.34414 / 2.0) * (float)(1 << 6) + 0.5;
  201     t2_vg = (1.4 * 0.71414 / 2.0) * (float)(1 << 6) + 0.5;
  202 
  203     for(i = 0; i < 256; i++)
  204     {
  205         float x = (float)(2 * i - 255);
  206 
  207         jpeg_info->yuvtabs.YUV_UB_tab[i] = (long)(( t_ub * x) + (1 << 5));
  208         jpeg_info->yuvtabs.YUV_VR_tab[i] = (long)(( t_vr * x) + (1 << 5));
  209         jpeg_info->yuvtabs.YUV_UG_tab[i] = (long)((-t_ug * x));
  210         jpeg_info->yuvtabs.YUV_VG_tab[i] = (long)((-t_vg * x) + (1 << 5));
  211         jpeg_info->yuvtabs.YUV_Y_tab[i]  = (long)((i << 6) | (i >> 2));
  212     }
  213     return 0;
  214 }
  215 
  216 int quicktime_fastjpg_init(quicktime_jpeg_t *jpeg_info)
  217 {
  218     int i;
  219     for(i = 0; i < TOTAL_QUANT_TBLS; i++) jpeg_info->quant_tables[i] = 0;
  220     quicktime_fastjpg_init_limittable(jpeg_info);
  221     jpeg_info->mjpg_kludge = 0;
  222     jpeg_info->jpg_std_DHT_flag = 0;
  223     jpeg_info->mjpa_info.valid = 0;
  224     jpeg_info->yuvbufs.allocated = 0;
  225     jpeg_info->yuvbufs.ybuf = 0;
  226     jpeg_info->yuvbufs.ubuf = 0;
  227     jpeg_info->yuvbufs.vbuf = 0;
  228     quicktime_fastjpg_init_yuv(jpeg_info);
  229     return 0;
  230 }
  231 
  232 int quicktime_fastjpg_deleteMCU(quicktime_jpeg_t *jpeg_info)
  233 {
  234     if(jpeg_info->yuvbufs.allocated)
  235     {
  236         free(jpeg_info->yuvbufs.ybuf);
  237         free(jpeg_info->yuvbufs.ubuf);
  238         free(jpeg_info->yuvbufs.vbuf);
  239     }
  240     jpeg_info->yuvbufs.ybuf = 0;
  241     jpeg_info->yuvbufs.ubuf = 0;
  242     jpeg_info->yuvbufs.vbuf = 0;
  243 
  244     return 0;
  245 }
  246 
  247 
  248 int quicktime_fastjpg_delete(quicktime_jpeg_t *jpeg_info)
  249 {
  250     int i;
  251     for(i = 0; i < TOTAL_QUANT_TBLS; i++)
  252         if(jpeg_info->quant_tables[i])
  253         {
  254             free(jpeg_info->quant_tables[i]); 
  255             jpeg_info->quant_tables[i] = 0; 
  256         }
  257 
  258     if(jpeg_info->jpg_samp_limit)
  259     {
  260         free(jpeg_info->jpg_samp_limit); 
  261         jpeg_info->jpg_samp_limit = 0; 
  262     }
  263 
  264     quicktime_fastjpg_deleteMCU(jpeg_info);
  265 
  266     free(jpeg_info->yuvtabs.YUV_Y_tab);
  267     free(jpeg_info->yuvtabs.YUV_UB_tab);
  268     free(jpeg_info->yuvtabs.YUV_VR_tab);
  269     free(jpeg_info->yuvtabs.YUV_UG_tab);
  270     free(jpeg_info->yuvtabs.YUV_VG_tab);
  271 }
  272 
  273 int quicktime_fastjpg_resethuffman(quicktime_jpeg_t *jpeg_info)
  274 {
  275     jpeg_info->jpg_comps[0].dc = 0;
  276     jpeg_info->jpg_comps[1].dc = 0;
  277     jpeg_info->jpg_comps[2].dc = 0;
  278     jpeg_info->jpg_h_bbuf = 0;  /* clear huffman bit buffer */
  279     jpeg_info->jpg_h_bnum = 0;
  280 }
  281 
  282 int quicktime_fastjpg_buildhuffman(quicktime_jpeg_t *jpeg_info, 
  283     quicktime_jpeg_huffman *htable, 
  284     unsigned char *hbits, 
  285     unsigned char *hvals)
  286 {
  287     unsigned long clen, num_syms, p, i, si, code, lookbits;
  288     unsigned long l, ctr;
  289     unsigned char huffsize[257];
  290     unsigned long huffcode[257];
  291 
  292 /*** generate code lengths for each symbol */
  293     num_syms = 0;
  294     for(clen = 1; clen <= 16; clen++)
  295     {
  296         for(i = 1; i <= (unsigned long)(hbits[clen]); i++) 
  297             huffsize[num_syms++] = (unsigned char)(clen);
  298     }
  299     huffsize[num_syms] = 0;
  300 
  301 /*** generate codes */
  302     code = 0;
  303     si = huffsize[0];
  304     p = 0;
  305     while(huffsize[p])
  306     {
  307         while(((unsigned long)huffsize[p]) == si) 
  308         {
  309             huffcode[p++] = code;
  310             code++;
  311         }
  312         code <<= 1;
  313         si++;
  314     }
  315 
  316 /* Init mincode/maxcode/valptr arrays */
  317     p = 0;
  318     for(l = 1; l <= 16; l++) 
  319     {
  320         if (htable->bits[l]) 
  321         {
  322             htable->valptr[l] = p; /* huffval[] index of 1st symbol of code length l */
  323             htable->mincode[l] = huffcode[p]; /* minimum code of length l */
  324             p += (unsigned long)(htable->bits[l]);
  325             htable->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
  326         } 
  327         else
  328         {
  329                 htable->valptr[l] = 0;  /* not needed */
  330                 htable->mincode[l] = 0; /* not needed */
  331                 htable->maxcode[l] = 0; /* WAS -1; */   /* -1 if no codes of this length */
  332         }
  333     }
  334     htable->maxcode[17] = 0xFFFFFL; /* ensures huff_DECODE terminates */
  335 
  336 
  337 /* Init huffman cache */
  338     memset((char *)htable->cache, 0, ((1 << HUFF_LOOKAHEAD) * sizeof(unsigned int16_t)));
  339     p = 0;
  340     for (l = 1; l <= HUFF_LOOKAHEAD; l++) 
  341     {
  342         for (i = 1; i <= (unsigned long) htable->bits[l]; i++, p++) 
  343         {
  344             int16_t the_code = (unsigned int16_t)((l << 8) | htable->vals[p]);
  345 
  346 /* l = current code's length, p = its index in huffcode[] & huffval[]. */
  347 /* Generate left-justified code followed by all possible bit sequences */
  348 
  349             lookbits = huffcode[p] << (HUFF_LOOKAHEAD - l);
  350             for(ctr = 1 << (HUFF_LOOKAHEAD - l); ctr > 0; ctr--) 
  351             {
  352                 htable->cache[lookbits] = the_code;
  353                 lookbits++;
  354             }
  355         }
  356     }
  357 }
  358 
  359 int quicktime_fastjpg_buildstdhuffman(quicktime_jpeg_t *jpeg_info)
  360 {
  361     long ttt, len;
  362     quicktime_jpeg_huffman *htable;
  363     unsigned char *hbits, *Sbits;
  364     unsigned char *hvals, *Svals;
  365 
  366     static unsigned char dc_luminance_bits[] =
  367     { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  368     static unsigned char dc_luminance_vals[] =
  369     { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  370 
  371     static unsigned char dc_chrominance_bits[] =
  372     { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  373     static unsigned char dc_chrominance_vals[] =
  374     { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  375 
  376     static unsigned char ac_luminance_bits[] =
  377     { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  378     static unsigned char ac_luminance_vals[] =
  379     { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  380       0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  381       0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  382       0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  383       0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  384       0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  385       0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  386       0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  387       0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  388       0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  389       0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  390       0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  391       0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  392       0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  393       0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  394       0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  395       0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  396       0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  397       0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  398       0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  399       0xf9, 0xfa };
  400 
  401     static unsigned char ac_chrominance_bits[] =
  402     { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  403     static unsigned char ac_chrominance_vals[] =
  404     { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  405       0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  406       0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  407       0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  408       0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  409       0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  410       0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  411       0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  412       0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  413       0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  414       0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  415       0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  416       0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  417       0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  418       0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  419       0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  420       0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  421       0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  422       0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  423       0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  424       0xf9, 0xfa };
  425 
  426     for(ttt = 0; ttt < 4; ttt++)
  427     { 
  428         unsigned long index = ttt & 1;
  429         unsigned long i, count;
  430 
  431         if (ttt <= 1)  /* DC tables */ 
  432         {
  433             htable = &(jpeg_info->jpg_dc_huff[index]);
  434             hbits = jpeg_info->jpg_dc_huff[index].bits;
  435             hvals = jpeg_info->jpg_dc_huff[index].vals;
  436             if(index == 0) 
  437             { 
  438                 Sbits = dc_luminance_bits; 
  439                 Svals = dc_luminance_vals; 
  440             }
  441             else 
  442             { 
  443                 Sbits = dc_chrominance_bits; 
  444                 Svals = dc_chrominance_vals; 
  445             }
  446         }
  447         else /* AC tables */
  448         {
  449             htable = &(jpeg_info->jpg_ac_huff[index]);
  450             hbits  = jpeg_info->jpg_ac_huff[index].bits;
  451             hvals  = jpeg_info->jpg_ac_huff[index].vals;
  452             if(index == 0) 
  453             { 
  454                 Sbits = ac_luminance_bits; 
  455                 Svals = ac_luminance_vals; 
  456             }
  457             else 
  458             { 
  459                 Sbits = ac_chrominance_bits; 
  460                 Svals = ac_chrominance_vals; 
  461             }
  462         }
  463         hbits[0] = 0;
  464         count = 0;
  465         for(i = 1; i <= 16; i++)
  466         {
  467             hbits[i] = Sbits[i];
  468             count += hbits[i];
  469         }
  470         len -= 17;
  471         if(count > 256)
  472         { 
  473             printf("quicktime_fastjpg_buildstdhuffman: STD DHT bad count %d\n", count); 
  474             return 1; 
  475         }
  476 
  477         for(i = 0; i < count; i++) hvals[i] = Svals[i];
  478         len -= count;
  479 
  480         quicktime_fastjpg_buildhuffman(jpeg_info, 
  481             htable, 
  482             hbits, 
  483             hvals);
  484     }
  485     jpeg_info->jpg_std_DHT_flag = 1;
  486     return 0;
  487 }
  488 
  489 int quicktime_fastjpg_buildstdDQT(quicktime_jpeg_t *jpeg_info, long scale)
  490 {
  491     long i, tbl_num;
  492     long *quant_table;
  493     unsigned int *table;
  494     unsigned int std_luminance_quant_tbl[DCTSIZE2] = 
  495     {
  496         16,  11,  10,  16,  24,  40,  51,  61,
  497         12,  12,  14,  19,  26,  58,  60,  55,
  498         14,  13,  16,  24,  40,  57,  69,  56,
  499         14,  17,  22,  29,  51,  87,  80,  62,
  500         18,  22,  37,  56,  68, 109, 103,  77,
  501         24,  35,  55,  64,  81, 104, 113,  92,
  502         49,  64,  78,  87, 103, 121, 120, 101,
  503         72,  92,  95,  98, 112, 100, 103,  99
  504     };
  505     unsigned int std_chrominance_quant_tbl[DCTSIZE2] = 
  506     {
  507         17,  18,  24,  47,  99,  99,  99,  99,
  508         18,  21,  26,  66,  99,  99,  99,  99,
  509         24,  26,  56,  99,  99,  99,  99,  99,
  510         47,  66,  99,  99,  99,  99,  99,  99,
  511         99,  99,  99,  99,  99,  99,  99,  99,
  512         99,  99,  99,  99,  99,  99,  99,  99,
  513         99,  99,  99,  99,  99,  99,  99,  99,
  514         99,  99,  99,  99,  99,  99,  99,  99
  515     };
  516 
  517     tbl_num = 0;
  518     for(tbl_num = 0; tbl_num <= 1; tbl_num++)
  519     {
  520         if(jpeg_info->quant_tables[tbl_num] == 0)
  521         { 
  522             jpeg_info->quant_tables[tbl_num] = (long*)malloc(64 * sizeof(long));
  523         }
  524 
  525         if (tbl_num == 0) 
  526             table = std_luminance_quant_tbl;
  527         else 
  528             table = std_chrominance_quant_tbl;
  529         quant_table = jpeg_info->quant_tables[tbl_num];
  530 
  531         for (i = 0; i < DCTSIZE2; i++)
  532         { 
  533             long tmp;
  534             tmp = ((long)table[i] * scale + 50L) / 100L;
  535             if(tmp <= 0) tmp = 1;
  536             if(tmp > 255) tmp = 255;
  537             quant_table[i] = (long)tmp;
  538         }
  539     }
  540     return 0;
  541 }
  542 
  543 int quicktime_fastjpg_get_marker(quicktime_jpeg_t *jpeg_info)
  544 {
  545     int c, done = 0;  /* 1 - completion    2 - error */
  546 
  547     while(!done)
  548     {
  549         c = quicktime_fastjpg_readbyte(jpeg_info);
  550 /* look for FF */
  551         while(c != 0xFF)
  552         {
  553             if(quicktime_fastjpg_eof(jpeg_info)) done = 2;
  554             c = quicktime_fastjpg_readbyte(jpeg_info);
  555         }
  556 
  557 /* now we've got 1 0xFF, keep reading until not 0xFF */
  558         do
  559         {
  560             if(quicktime_fastjpg_eof(jpeg_info)) done = 2;
  561             c = quicktime_fastjpg_readbyte(jpeg_info);
  562         }while (c == 0xFF);
  563         
  564 /* not a 00 or FF */
  565         if (c != 0) done = 1; 
  566     }
  567 
  568     if(done == 1) 
  569     return c;
  570     else
  571     return 0;
  572 }
  573 
  574 int quicktime_fastjpg_skip_marker(quicktime_jpeg_t *jpeg_info)
  575 {
  576     long len, tmp;
  577     len = quicktime_fastjpg_readint16(jpeg_info);
  578     len -= 2;
  579     if(len <= 0) return 1;
  580     if(quicktime_fastjpg_eof(jpeg_info)) return 1;
  581     while(len--) quicktime_fastjpg_readbyte(jpeg_info);
  582     return 0;
  583 }
  584 
  585 int quicktime_fastjpg_check_for_marker(quicktime_jpeg_t *jpeg_info)
  586 {
  587     if(jpeg_info->marker) return(jpeg_info->marker);
  588     if(jpeg_info->chunk_size < 2) return(0);
  589     if((jpeg_info->chunk[0] == 0xff) && (jpeg_info->chunk[1] != 0x00))
  590     {
  591         jpeg_info->marker = jpeg_info->chunk[1];
  592         {
  593             if(jpeg_info->jpg_h_bnum)
  594             {
  595                 printf("quicktime_fastjpg_check_for_marker: check marker positive - lost %d bits\n",
  596                                 jpeg_info->jpg_h_bnum);
  597             }
  598         }
  599         jpeg_info->jpg_h_bnum = 0;
  600         jpeg_info->jpg_h_bbuf = 0;
  601         jpeg_info->chunk += 2;
  602         jpeg_info->chunk_size -= 2;
  603     }
  604     return(jpeg_info->marker);
  605 }
  606 
  607 int quicktime_fastjpg_readSOI(quicktime_jpeg_t *jpeg_info)
  608 {
  609     jpeg_info->jpg_rst_interval = 0;
  610     return 0;
  611 }
  612 
  613 int quicktime_fastjpg_readSOF(quicktime_jpeg_t *jpeg_info)
  614 {
  615     int result = 0;
  616     int len, i, c;
  617     quicktime_jpeg_comp_header *comp;
  618 
  619     len = quicktime_fastjpg_readint16(jpeg_info);
  620     if(jpeg_info->mjpg_kludge) 
  621         len -= 6;
  622     else 
  623         len -= 8;
  624 
  625     jpeg_info->jpg_dprec = quicktime_fastjpg_readbyte(jpeg_info);
  626     jpeg_info->jpg_height = quicktime_fastjpg_readint16(jpeg_info);
  627     jpeg_info->jpg_width = quicktime_fastjpg_readint16(jpeg_info);
  628     jpeg_info->jpg_num_comps = quicktime_fastjpg_readbyte(jpeg_info);
  629 
  630     for(i = 0; i < jpeg_info->jpg_num_comps; i++)
  631     {
  632         if(i > MAX_COMPS) 
  633             comp = &(jpeg_info->jpg_comps[DUMMY_COMP]);
  634         else 
  635             comp = &(jpeg_info->jpg_comps[i]);
  636 
  637         comp->id = quicktime_fastjpg_readbyte(jpeg_info);
  638         comp->hvsample = quicktime_fastjpg_readbyte(jpeg_info);
  639         comp->qtbl_num = quicktime_fastjpg_readbyte(jpeg_info);
  640     }
  641     return(quicktime_fastjpg_eof(jpeg_info));
  642 }
  643 
  644 int quicktime_fastjpg_readSOS(quicktime_jpeg_t *jpeg_info)
  645 {
  646     int len, i, j;
  647     int comp_id, htbl_num;
  648     int jpg_Ss, jpg_Se, jpg_AhAl;
  649 
  650     len = quicktime_fastjpg_readint16(jpeg_info);
  651     jpeg_info->jpg_comps_in_scan = quicktime_fastjpg_readbyte(jpeg_info);
  652 
  653     for(i = 0; i < jpeg_info->jpg_comps_in_scan; i++)
  654     { 
  655         quicktime_jpeg_comp_header *comp = 0;
  656         comp_id = quicktime_fastjpg_readbyte(jpeg_info);
  657         for(j = 0; j < jpeg_info->jpg_num_comps; )
  658         {
  659             comp = &(jpeg_info->jpg_comps[j]);
  660             if(comp->id == comp_id) break;
  661             j++;
  662         }
  663 
  664         if (j > jpeg_info->jpg_num_comps) 
  665         {
  666             printf("quicktime_fastjpg_readSOS: bad id %x", comp_id);
  667             return 1;
  668         }
  669 
  670         htbl_num = quicktime_fastjpg_readbyte(jpeg_info);
  671         comp->dc_htbl_num = (htbl_num >> 4) & 0x0f;
  672         comp->ac_htbl_num = (htbl_num     ) & 0x0f;
  673     }
  674     jpg_Ss = quicktime_fastjpg_readbyte(jpeg_info);
  675     jpg_Se = quicktime_fastjpg_readbyte(jpeg_info);
  676     jpg_AhAl = quicktime_fastjpg_readbyte(jpeg_info);
  677     return(quicktime_fastjpg_eof(jpeg_info));
  678 }
  679 
  680 int quicktime_fastjpg_readDHT(quicktime_jpeg_t *jpeg_info)
  681 {
  682     int len, i, index, count;
  683     unsigned long result = 1;
  684     quicktime_jpeg_huffman *htable;
  685     unsigned char *hbits;
  686     unsigned char *hvals;
  687 
  688     jpeg_info->jpg_std_DHT_flag = 0;
  689     len = quicktime_fastjpg_readint16(jpeg_info);
  690 
  691     if(jpeg_info->mjpg_kludge) len += 2;
  692 
  693     len -= 2;
  694 
  695     if(quicktime_fastjpg_eof(jpeg_info)) return 1;
  696 
  697     while(len > 0)
  698     {
  699         index = quicktime_fastjpg_readbyte(jpeg_info);
  700         len--;
  701 /* Test indexes */
  702         if (index & 0x10)               /* AC Table */
  703         {
  704             index &= 0x0f;
  705             if (index >= TOTAL_HUFF_TBLS) break;
  706             htable = &(jpeg_info->jpg_ac_huff[index]);
  707             hbits  = jpeg_info->jpg_ac_huff[index].bits;
  708             hvals  = jpeg_info->jpg_ac_huff[index].vals;
  709         }
  710         else                            /* DC Table */
  711         {
  712             index &= 0x0f;
  713             if (index >= TOTAL_HUFF_TBLS) break;
  714             htable = &(jpeg_info->jpg_dc_huff[index]);
  715             hbits  = jpeg_info->jpg_dc_huff[index].bits;
  716             hvals  = jpeg_info->jpg_dc_huff[index].vals;
  717         }
  718 
  719         hbits[0] = 0;
  720         count = 0;
  721 
  722         if(len < 16) break;
  723         for (i = 1; i <= 16; i++)
  724         {
  725             hbits[i] = quicktime_fastjpg_readbyte(jpeg_info);
  726             count += hbits[i];
  727         }
  728         len -= 16;
  729 
  730         if(count > 256)
  731         { 
  732             printf("quicktime_fastjpg_readDHT: DHT bad count %d using default.\n", count);
  733             break;
  734         }
  735 
  736         if(len < count)
  737         {
  738             printf("quicktime_fastjpg_readDHT: DHT count(%d) > len(%d).\n", count, len);
  739             break;
  740         }
  741 
  742         for(i = 0; i < count; i++) hvals[i] = quicktime_fastjpg_readbyte(jpeg_info);
  743         len -= count;
  744 
  745         quicktime_fastjpg_buildhuffman(jpeg_info, htable, hbits, hvals);
  746         result = 0;
  747     }
  748 
  749     if(result)
  750     {
  751 /* Something is roached, but what the heck, try default DHT instead */
  752         while(len > 0)
  753         {
  754             len--;
  755             quicktime_fastjpg_readbyte(jpeg_info);
  756         }
  757         quicktime_fastjpg_buildstdhuffman(jpeg_info);
  758         result = 0; 
  759     }
  760 
  761     return result;
  762 }
  763 
  764 int quicktime_fastjpg_readDQT(quicktime_jpeg_t *jpeg_info)
  765 {
  766     long len;
  767     len = quicktime_fastjpg_readint16(jpeg_info);
  768     if(!jpeg_info->mjpg_kludge) len -= 2;
  769 
  770     while(len > 0)
  771     { 
  772         long i, tbl_num, prec;
  773         long *quant_table;
  774 
  775         tbl_num = quicktime_fastjpg_readbyte(jpeg_info);
  776         len -= 1;
  777 
  778         prec = (tbl_num >> 4) & 0x0f;
  779         prec = (prec)?(2 * DCTSIZE2) : (DCTSIZE2);  /* 128 or 64 */
  780         tbl_num &= 0x0f;
  781         if (tbl_num > 4)
  782         { 
  783             printf("quicktime_fastjpg_readDQT: bad DQT tnum %x\n", tbl_num); 
  784             return 1; 
  785         }
  786 
  787         if(jpeg_info->quant_tables[tbl_num] == 0)
  788         {
  789             jpeg_info->quant_tables[tbl_num] = (long *)malloc(64 * sizeof(long));
  790         }
  791         len -= prec;
  792 
  793         if(quicktime_fastjpg_eof(jpeg_info)) return 1;
  794         quant_table = jpeg_info->quant_tables[tbl_num];
  795         if(prec == 128)
  796         { 
  797             unsigned long tmp; 
  798             for(i = 0; i < DCTSIZE2; i++)
  799             { 
  800                 tmp = quicktime_fastjpg_readint16(jpeg_info);
  801                 quant_table[JJ_ZAG[i]] = (long)tmp;
  802             }
  803         }
  804         else
  805         {
  806             unsigned long tmp; 
  807             for(i = 0; i < DCTSIZE2; i++)
  808             {
  809                 tmp = quicktime_fastjpg_readbyte(jpeg_info);
  810                 quant_table[JJ_ZAG[i]] = (long)tmp; 
  811             }
  812         }
  813     }
  814     return 0;
  815 }
  816 
  817 int quicktime_fastjpg_readAPPX(quicktime_jpeg_t *jpeg_info)
  818 {
  819     long len;
  820     len = quicktime_fastjpg_readint32(jpeg_info);
  821     len -= 2;
  822     if(len > 4)
  823     {
  824         unsigned long first;
  825         first = quicktime_fastjpg_readint32(jpeg_info);
  826         len -= 4;
  827 /*      if (first == 0x41564931)  /* AVI1 */ */
  828 /*      { */
  829 /*          int interleave; */
  830 /*          interleave = quicktime_fastjpg_readbyte(jpeg_info); */
  831 /*          len--; */
  832 /*          avi_jpeg_info.valid = 1; */
  833 /*          avi_jpeg_info.ileave = interleave; */
  834 /*      } */
  835 /*      else  */
  836         if(len > (0x28 - 4)) /* Maybe APPLE MJPEG A */
  837         { 
  838             unsigned long jid;
  839             jid = quicktime_fastjpg_readint32(jpeg_info);
  840             len -= 4;
  841             if(jid == JPEG_APP1_MJPA)
  842             {  
  843                 jpeg_info->mjpa_info.valid = 1;
  844                 jpeg_info->mjpa_info.field_sz = quicktime_fastjpg_readint32(jpeg_info);
  845                 jpeg_info->mjpa_info.pad_field_sz = quicktime_fastjpg_readint32(jpeg_info);
  846                 jpeg_info->mjpa_info.next_off = quicktime_fastjpg_readint32(jpeg_info);
  847                 jpeg_info->mjpa_info.quant_off = quicktime_fastjpg_readint32(jpeg_info);
  848                 jpeg_info->mjpa_info.huff_off = quicktime_fastjpg_readint32(jpeg_info);
  849                 jpeg_info->mjpa_info.image_off = quicktime_fastjpg_readint32(jpeg_info);
  850                 jpeg_info->mjpa_info.scan_off = quicktime_fastjpg_readint32(jpeg_info);
  851                 jpeg_info->mjpa_info.data_off = quicktime_fastjpg_readint32(jpeg_info);
  852                 len -= 32;
  853             }
  854         }
  855     }
  856     if(len) quicktime_fastjpg_skip(jpeg_info, len);
  857     return 0;
  858 }
  859 
  860 int quicktime_fastjpg_readDRI(quicktime_jpeg_t *jpeg_info)
  861 {
  862     long len;
  863     len = quicktime_fastjpg_readint16(jpeg_info);
  864     jpeg_info->jpg_rst_interval = quicktime_fastjpg_readint16(jpeg_info);
  865     return 0;
  866     
  867 }
  868 
  869 int quicktime_fastjpg_readEOI(quicktime_jpeg_t *jpeg_info)
  870 {
  871     while(jpeg_info->marker = quicktime_fastjpg_get_marker(jpeg_info))
  872     {
  873         if(jpeg_info->marker == M_EOI) 
  874         {
  875             jpeg_info->jpg_saw_EOI = 1; 
  876             return 1; 
  877         }
  878     }
  879     return 0;
  880 }
  881 
  882 
  883 
  884 
  885 int quicktime_fastjpg_read_markers(quicktime_jpeg_t *jpeg_info)
  886 {
  887     int done = 0;     /* 1 = completion    2 = error */
  888 
  889     while(!done)
  890     { 
  891         if(!(jpeg_info->marker = quicktime_fastjpg_get_marker(jpeg_info)))
  892             done = 2;
  893         else
  894         {
  895 /*printf("quicktime_fastjpg_read_markers %x\n", jpeg_info->marker); */
  896             switch(jpeg_info->marker)
  897             {
  898                 case M_SOI: 
  899                     if(quicktime_fastjpg_readSOI(jpeg_info)) done = 2;
  900                     else
  901                     jpeg_info->jpg_saw_SOI = 1;
  902                     break;
  903 
  904                 case M_SOF0: 
  905                 case M_SOF1: 
  906                 case M_SOF2: 
  907                     if(quicktime_fastjpg_readSOF(jpeg_info)) done = 2;
  908                     else
  909                     jpeg_info->jpg_saw_SOF = 1;
  910                     break;
  911 
  912 /* Not yet supported */
  913                 case M_SOF3:
  914                 case M_SOF5:
  915                 case M_SOF6:
  916                 case M_SOF7:
  917                 case M_SOF9:
  918                 case M_SOF10:
  919                 case M_SOF11:
  920                 case M_SOF13:
  921                 case M_SOF14:
  922                 case M_SOF15:
  923                     done = 2;
  924                     break;
  925                     
  926                 case M_SOS: 
  927                     if(quicktime_fastjpg_readSOS(jpeg_info)) done = 2;
  928                     else
  929                     {
  930                         jpeg_info->jpg_saw_SOS = 1;
  931                         jpeg_info->jpg_nxt_rst_num = 0;
  932                         done = 1;
  933                     }
  934                     break;
  935 
  936                 case M_DHT:
  937                     if(quicktime_fastjpg_readDHT(jpeg_info)) done = 2;
  938                     else
  939                         jpeg_info->jpg_saw_DHT = 1;
  940                     break;
  941                     
  942                 case M_DQT:
  943                     if(quicktime_fastjpg_readDQT(jpeg_info)) done = 2;
  944                     else
  945                         jpeg_info->jpg_saw_DQT = 1;
  946                     break;
  947 
  948                 case M_DRI:
  949                     if(quicktime_fastjpg_readDRI(jpeg_info)) done = 2;
  950                     break;
  951 
  952                 case M_COM:
  953                 {
  954 /* Comment */
  955                     int len;
  956                     len = quicktime_fastjpg_readint16(jpeg_info);
  957                     len -= 2;
  958 
  959                     while(len > 0)
  960                     {
  961                         quicktime_fastjpg_readbyte(jpeg_info); len--;
  962                     }
  963                 }
  964                 break;
  965 
  966                 case M_APP0:
  967                 case M_APP1:
  968                     if(quicktime_fastjpg_readAPPX(jpeg_info)) done = 2;
  969                     break;
  970 
  971                 case M_EOI:
  972                     printf("quicktime_fastjpg_read_markers: reached EOI without data\n");
  973                     done = 2;
  974                     break;
  975 
  976                 case M_RST0:                /* these are all parameterless */
  977                 case M_RST1:
  978                 case M_RST2:
  979                 case M_RST3:
  980                 case M_RST4:
  981                 case M_RST5:
  982                 case M_RST6:
  983                 case M_RST7:
  984                 case M_TEM:
  985                     break;
  986 
  987                 default:
  988                     printf("quicktime_fastjpg_read_markers: unknown marker %x\n", jpeg_info->marker);
  989                     if(quicktime_fastjpg_skip_marker(jpeg_info)) done = 2;
  990                     break;
  991             } /* end of switch */
  992         }
  993     }
  994     if(done == 2) return 1; else return 0;
  995 }
  996 
  997 int quicktime_fastjpg_initMCU(quicktime_jpeg_t *jpeg_info, 
  998                 int width, 
  999                 int height,
 1000                 int full_flag)
 1001 {
 1002     int twidth = (width + 15) / 16;
 1003     int theight = (height + 15) / 16;  
 1004     if(theight & 1) theight++;
 1005 
 1006     if(full_flag) 
 1007         twidth *= (theight << 2);
 1008     else
 1009         twidth <<= 2; /* four dct's deep */
 1010 
 1011     if(!jpeg_info->yuvbufs.allocated)
 1012     {
 1013         jpeg_info->yuvbufs.allocated = 1;
 1014         jpeg_info->yuvbufs.ybuf = (unsigned char*)malloc(twidth * DCTSIZE2);
 1015         jpeg_info->yuvbufs.ubuf = (unsigned char*)malloc(twidth * DCTSIZE2);
 1016         jpeg_info->yuvbufs.vbuf = (unsigned char*)malloc(twidth * DCTSIZE2);
 1017     }
 1018 }
 1019 
 1020 int quicktime_fastjpg_skip_to_next_rst(quicktime_jpeg_t *jpeg_info)
 1021 {
 1022     unsigned long d, last_ff = 0;
 1023     jpeg_info->jpg_h_bnum = 0;
 1024     jpeg_info->jpg_h_bbuf = 0;
 1025     while(jpeg_info->chunk_size)
 1026     {
 1027         d = *(jpeg_info->chunk++); 
 1028         jpeg_info->chunk_size--;
 1029         if(last_ff)
 1030         {
 1031            if((d != 0) && (d != 0xff)) return d;
 1032         }
 1033         last_ff = (d == 0xff) ? 1 : 0;
 1034     }
 1035     return M_EOI;
 1036 }
 1037 
 1038 /*  clears dctbuf to zeroes. 
 1039  *  fills from huffman encode stream
 1040  */
 1041 int quicktime_fastjpg_huffparse(quicktime_jpeg_t *jpeg_info, 
 1042                 quicktime_jpeg_comp_header *comp, 
 1043                 int16_t *dct_buf, 
 1044                 unsigned long *qtab, 
 1045                 unsigned char *OBuf)
 1046 {
 1047     unsigned long tmp_, tmp__, hcode_, t1_, shift_, minbits_;
 1048     long i, dcval, level;
 1049     unsigned long size, run, tmp, coeff;
 1050     quicktime_jpeg_huffman *huff_hdr = &(jpeg_info->jpg_dc_huff[comp->dc_htbl_num]);
 1051     unsigned int16_t *huff_tbl = huff_hdr->cache;
 1052     unsigned char *rnglimit = jpeg_info->jpg_samp_limit + (CENTERJSAMPLE + MAXJSAMPLE + 1);
 1053     unsigned long c_cnt, pos = 0;
 1054 
 1055     QUICKTIME_FASTJPG_HUFF_DECODE(huff_hdr, huff_tbl, jpeg_info->jpg_h_bnum, jpeg_info->jpg_h_bbuf, size);
 1056 
 1057     if(size)
 1058     { 
 1059         unsigned long bits;
 1060         QUICKTIME_FASTJPG_GET_BITS(size, jpeg_info->jpg_h_bnum, jpeg_info->jpg_h_bbuf, bits);
 1061         dcval = QUICKTIME_FASTJPG_HUFF_EXTEND(bits, size);
 1062         comp->dc += dcval;
 1063     }
 1064     dcval = comp->dc;
 1065 
 1066 /* clear rest of dct buffer */
 1067     memset((char *)(dct_buf), 0, (DCTSIZE2 * sizeof(int16_t)));
 1068     dcval *= (long)qtab[0];
 1069     dct_buf[0] = (int16_t)dcval;
 1070     c_cnt = 0;
 1071 
 1072     huff_hdr = &(jpeg_info->jpg_ac_huff[comp->ac_htbl_num]);
 1073     huff_tbl = huff_hdr->cache;
 1074     for(i = 1; i < 64; )
 1075     { 
 1076         QUICKTIME_FASTJPG_HUFF_DECODE(huff_hdr, huff_tbl, jpeg_info->jpg_h_bnum, jpeg_info->jpg_h_bbuf, tmp); 
 1077         size =  tmp & 0x0f;
 1078         run = (tmp >> 4) & 0x0f; /* leading zeroes */
 1079 
 1080         if(size)
 1081         {
 1082             i += run; /* skip zeroes */
 1083             QUICKTIME_FASTJPG_GET_BITS(size, jpeg_info->jpg_h_bnum, jpeg_info->jpg_h_bbuf, level);
 1084             coeff = (long)QUICKTIME_FASTJPG_HUFF_EXTEND(level, size);
 1085             pos = JJ_ZAG[i];
 1086             coeff *= (long)qtab[pos];
 1087             if(coeff)
 1088             { 
 1089                 c_cnt++;
 1090                 dct_buf[pos] = (int16_t)(coeff);
 1091             }
 1092             i++;
 1093         }
 1094         else
 1095         {
 1096             if(run != 15) break; /* EOB */
 1097             i += 16;
 1098         }
 1099     }
 1100 
 1101     if(c_cnt) quicktime_rev_dct(dct_buf, OBuf, rnglimit);
 1102     else
 1103     { 
 1104         register unsigned char *op = OBuf;
 1105         register int jj = 8;
 1106         int16_t v = *dct_buf;
 1107         register unsigned char dc;
 1108 
 1109         v = (v < 0) ? ((v - 3) >> 3) : ((v + 4) >> 3);
 1110         dc = rnglimit[(int)(v & RANGE_MASK)];
 1111         while(jj--)
 1112         {
 1113             op[0] = op[1] = op[2] = op[3] = op[4] = op[5] = op[6] = op[7] = dc;
 1114             op += 8;
 1115         }
 1116     }
 1117     return 0;
 1118 }
 1119 
 1120 
 1121 int quicktime_fastjpg_MCU411111_to_RGB(QUICKTIME_MCU_ARGS)
 1122 {
 1123     QUICKTIME_MCU_VARS
 1124     QUICKTIME_MCU111111_MID_VARS
 1125     QUICKTIME_MCU_INNER_VARS
 1126     
 1127     while(frame_height > 0)
 1128     { 
 1129         yptr = ybuf; 
 1130         uptr = ubuf; 
 1131         vptr = vbuf;
 1132         for(yi = 0; yi < 8; yi++)
 1133         { 
 1134             QUICKTIME_MCU111111_MID_DECL;
 1135             if(frame_height <= 0) return 0;
 1136             while(xi--)
 1137             { 
 1138                 QUICKTIME_MCU_INNER_INIT;
 1139                 QUICKTIME_MCU_YUV_TO_RGB(YTab[*yp++], cr, cg, cb, ip);
 1140                 QUICKTIME_MCU_YUV_TO_RGB(YTab[*yp++], cr, cg, cb, ip);
 1141                 QUICKTIME_MCU_YUV_TO_RGB(YTab[*yp++], cr, cg, cb, ip);
 1142                 QUICKTIME_MCU_YUV_TO_RGB(YTab[*yp++], cr, cg, cb, ip);
 1143                 QUICKTIME_MCU4H_INNER_TAIL(56, 56);
 1144             }
 1145             yptr += 8; 
 1146             uptr += 8; 
 1147             vptr += 8; 
 1148             frame_height--;  
 1149             row_pointers += interlaced ? 2 : 1;
 1150         }
 1151         ybuf += mcu_row_size << 2; 
 1152         ubuf += mcu_row_size; 
 1153         vbuf += mcu_row_size;
 1154     }
 1155 }
 1156 
 1157 int quicktime_fastjpg_decode_411111(quicktime_jpeg_t *jpeg_info, 
 1158                                 unsigned char **output_rows,
 1159                                 int jpeg_width,
 1160                                 int jpeg_height,
 1161                                 int interlaced,
 1162                                 int row_offset,
 1163                                 int frame_width,
 1164                                 int frame_height)
 1165 {
 1166     long x, mcu_cols, mcu_rows;
 1167     long *qtab0, *qtab1, *qtab2;
 1168     unsigned char *Ybuf, *Ubuf, *Vbuf;
 1169     unsigned long rst_count;
 1170     unsigned long rst_skip = 0;
 1171     unsigned long orow_size = frame_width * 3 * (interlaced ? 2 : 1);
 1172 
 1173     if(interlaced) frame_height >>= 1;
 1174     frame_width += 3; 
 1175     frame_width >>= 2; /* 4h */
 1176     qtab0 = jpeg_info->quant_tables[jpeg_info->jpg_comps[0].qtbl_num];
 1177     qtab1 = jpeg_info->quant_tables[jpeg_info->jpg_comps[1].qtbl_num];
 1178     qtab2 = jpeg_info->quant_tables[jpeg_info->jpg_comps[2].qtbl_num];
 1179 
 1180     mcu_cols = (jpeg_width + 31) / 32;
 1181     mcu_rows = (jpeg_height + 7) / 8;
 1182     jpeg_info->marker = 0x00;
 1183 
 1184     rst_count = jpeg_info->jpg_rst_interval;
 1185     output_rows += row_offset;
 1186     while(mcu_rows--)
 1187     { 
 1188         Ybuf = jpeg_info->yuvbufs.ybuf; 
 1189         Ubuf = jpeg_info->yuvbufs.ubuf; 
 1190         Vbuf = jpeg_info->yuvbufs.vbuf;
 1191         x = mcu_cols; 
 1192         while(x--)
 1193         {
 1194             if(rst_skip)
 1195             {
 1196                 rst_skip--;
 1197                 memset(Ybuf, 0, (DCTSIZE2 << 2));
 1198                 memset(Ubuf, 0x80, DCTSIZE2);
 1199                 memset(Vbuf, 0x80, DCTSIZE2);
 1200                 Ybuf += (DCTSIZE2 << 2);
 1201                 Ubuf += DCTSIZE2;
 1202                 Vbuf += DCTSIZE2;
 1203             }
 1204             else
 1205             {
 1206                 QUICKTIME_FASTJPG_HANDLE_RST(jpeg_info->jpg_rst_interval, rst_count);
 1207 
 1208 /* Y0 Y1 Y2 Y3 U V */
 1209                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[0]), jpeg_info->jpg_dct_buf, qtab0, Ybuf); Ybuf += DCTSIZE2;
 1210                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[0]), jpeg_info->jpg_dct_buf, qtab0, Ybuf); Ybuf += DCTSIZE2;
 1211                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[0]), jpeg_info->jpg_dct_buf, qtab0, Ybuf); Ybuf += DCTSIZE2;
 1212                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[0]), jpeg_info->jpg_dct_buf, qtab0, Ybuf); Ybuf += DCTSIZE2;
 1213                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[1]), jpeg_info->jpg_dct_buf, qtab1, Ubuf); Ubuf += DCTSIZE2;
 1214                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[2]), jpeg_info->jpg_dct_buf, qtab2, Vbuf); Vbuf += DCTSIZE2;
 1215 
 1216                 if(jpeg_info->marker == 0)
 1217                     jpeg_info->marker = quicktime_fastjpg_check_for_marker(jpeg_info);
 1218                 QUICKTIME_FASTJPG_TEST_MARKER;
 1219             }
 1220         } /* end of mcu_cols */
 1221 
 1222         quicktime_fastjpg_MCU411111_to_RGB(jpeg_info, 
 1223                 output_rows,
 1224                 frame_width,
 1225                 (frame_height < 8 ? frame_height : 8),
 1226                 (mcu_cols * DCTSIZE2),
 1227                 orow_size,
 1228                 &(jpeg_info->yuvbufs),
 1229                 interlaced);
 1230         frame_height -= 8;
 1231         output_rows += interlaced ? 16 : 8;
 1232     } /* end of mcu_rows */
 1233 
 1234     if(jpeg_info->marker) 
 1235     { 
 1236         jpeg_info->jpg_h_bbuf = 0; 
 1237         jpeg_info->jpg_h_bnum = 0; 
 1238     }
 1239     return 0;
 1240 }
 1241 
 1242 int quicktime_fastjpg_MCU221111_to_RGB(QUICKTIME_MCU_ARGS)
 1243 {
 1244     QUICKTIME_MCU_VARS
 1245     QUICKTIME_MCU221111_MID_VARS
 1246     QUICKTIME_MCU_INNER_VARS
 1247     
 1248     while(frame_height > 0)
 1249     {
 1250         yptr = ybuf;
 1251         uptr = ubuf;
 1252         vptr = vbuf;
 1253         for(yi = 0; yi < 8; yi++)
 1254         {
 1255             QUICKTIME_MCU221111_MID_DECL;
 1256             while(xi--)
 1257             {
 1258                 QUICKTIME_MCU_INNER_INIT;
 1259                 QUICKTIME_MCU_YUV_TO_RGB(YTab[yp[8]], cr, cg, cb, ip1);
 1260                 QUICKTIME_MCU_YUV_TO_RGB(YTab[*yp++], cr, cg, cb, ip0);
 1261                 QUICKTIME_MCU_YUV_TO_RGB(YTab[yp[8]], cr, cg, cb, ip1);
 1262                 QUICKTIME_MCU_YUV_TO_RGB(YTab[*yp++], cr, cg, cb, ip0);
 1263                 QUICKTIME_MCU2H_INNER_TAIL(56, 184);
 1264             }
 1265             yptr += 16; 
 1266             uptr += 8; 
 1267             vptr += 8; 
 1268             frame_height -= 2;  
 1269         }
 1270         ybuf += mcu_row_size << 2; 
 1271         ubuf += mcu_row_size; 
 1272         vbuf += mcu_row_size;
 1273     }
 1274 }
 1275 
 1276 int quicktime_fastjpg_decode_221111(quicktime_jpeg_t *jpeg_info, 
 1277                                 unsigned char **output_rows,
 1278                                 int jpeg_width,
 1279                                 int jpeg_height,
 1280                                 int interlaced,
 1281                                 int row_offset,
 1282                                 int frame_width,
 1283                                 int frame_height)
 1284 {
 1285     long x, mcu_cols, mcu_rows;
 1286     long *qtab0, *qtab1, *qtab2;
 1287     unsigned char *Ybuf, *Ubuf, *Vbuf;
 1288     unsigned long rst_count;
 1289     unsigned long rst_skip = 0;
 1290     unsigned long orow_size = frame_width * 3 * (interlaced ? 2 : 1);
 1291 
 1292     if(interlaced) frame_height >>= 1;
 1293     frame_width += 1; 
 1294     frame_width >>= 1; /* 2h */
 1295     qtab0 = jpeg_info->quant_tables[jpeg_info->jpg_comps[0].qtbl_num];
 1296     qtab1 = jpeg_info->quant_tables[jpeg_info->jpg_comps[1].qtbl_num];
 1297     qtab2 = jpeg_info->quant_tables[jpeg_info->jpg_comps[2].qtbl_num];
 1298 
 1299     mcu_cols = (jpeg_width + 15) / 16;
 1300     mcu_rows = (jpeg_height + 15) / 16;
 1301     jpeg_info->marker = 0x00;
 1302 
 1303     rst_count = jpeg_info->jpg_rst_interval;
 1304     output_rows += row_offset;
 1305     while(mcu_rows--)
 1306     {
 1307         Ybuf = jpeg_info->yuvbufs.ybuf; 
 1308         Ubuf = jpeg_info->yuvbufs.ubuf; 
 1309         Vbuf = jpeg_info->yuvbufs.vbuf;
 1310         x = mcu_cols; 
 1311         while(x--)
 1312         {
 1313             if(rst_skip)
 1314             {
 1315                 rst_skip--;
 1316                 memset(Ybuf, 0, (DCTSIZE2 << 2));
 1317                 memset(Ubuf, 0x80, DCTSIZE2);
 1318                 memset(Vbuf, 0x80, DCTSIZE2);
 1319                 Ybuf += (DCTSIZE2 << 2);
 1320                 Ubuf += DCTSIZE2;
 1321                 Vbuf += DCTSIZE2;
 1322             }
 1323             else
 1324             {
 1325                 QUICKTIME_FASTJPG_HANDLE_RST(jpeg_info->jpg_rst_interval, rst_count);
 1326 
 1327 /* Y0 Y1 Y2 Y3 U V */
 1328                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[0]), jpeg_info->jpg_dct_buf, qtab0, Ybuf); Ybuf += DCTSIZE2;
 1329                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[0]), jpeg_info->jpg_dct_buf, qtab0, Ybuf); Ybuf += DCTSIZE2;
 1330                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[0]), jpeg_info->jpg_dct_buf, qtab0, Ybuf); Ybuf += DCTSIZE2;
 1331                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[0]), jpeg_info->jpg_dct_buf, qtab0, Ybuf); Ybuf += DCTSIZE2;
 1332                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[1]), jpeg_info->jpg_dct_buf, qtab1, Ubuf); Ubuf += DCTSIZE2;
 1333                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[2]), jpeg_info->jpg_dct_buf, qtab2, Vbuf); Vbuf += DCTSIZE2;
 1334 
 1335                 if(jpeg_info->marker == 0)
 1336                     jpeg_info->marker = quicktime_fastjpg_check_for_marker(jpeg_info);
 1337                 QUICKTIME_FASTJPG_TEST_MARKER;
 1338             }
 1339         } /* end of mcu_cols */
 1340 
 1341         quicktime_fastjpg_MCU221111_to_RGB(jpeg_info, 
 1342                 output_rows,
 1343                 frame_width,
 1344                 (frame_height < 16 ? frame_height : 16),
 1345                 (mcu_cols * DCTSIZE2),
 1346                 orow_size,
 1347                 &(jpeg_info->yuvbufs),
 1348                 interlaced);
 1349         frame_height -= 16;
 1350         output_rows += interlaced ? 32 : 16;
 1351     } /* end of mcu_rows */
 1352 
 1353     if(jpeg_info->marker) 
 1354     { 
 1355         jpeg_info->jpg_h_bbuf = 0; 
 1356         jpeg_info->jpg_h_bnum = 0; 
 1357     }
 1358     return 0;
 1359 }
 1360 
 1361 int quicktime_fastjpg_double_mcu(unsigned char *ptr, int mcus)
 1362 {
 1363     unsigned char *sblk, *dblk;
 1364     int blks = mcus * 8;
 1365     int flag = 0;
 1366 
 1367     sblk = ptr + (blks * 8) - 8;
 1368     dblk = ptr + (blks * 16) - 8;
 1369     while(blks--)
 1370     { 
 1371         dblk[7] = dblk[6] = sblk[7];
 1372         dblk[5] = dblk[4] = sblk[6];
 1373         dblk[3] = dblk[2] = sblk[5];
 1374         dblk[1] = dblk[0] = sblk[4];
 1375         dblk -= 64;
 1376         dblk[7] = dblk[6] = sblk[3];
 1377         dblk[5] = dblk[4] = sblk[2];
 1378         dblk[3] = dblk[2] = sblk[1];
 1379         dblk[1] = dblk[0] = sblk[0];
 1380         flag++;
 1381         if(flag >= 8)
 1382         {
 1383             flag = 0;
 1384             dblk -= 8;
 1385         }
 1386         else
 1387         {
 1388             dblk += 56;
 1389         }
 1390         sblk -= 8;
 1391     }
 1392 }
 1393 
 1394 int quicktime_fastjpg_MCU211111_to_RGB(QUICKTIME_MCU_ARGS)
 1395 {
 1396     QUICKTIME_MCU_VARS
 1397     QUICKTIME_MCU111111_MID_VARS
 1398     QUICKTIME_MCU_INNER_VARS
 1399     
 1400     while(frame_height > 0)
 1401     { 
 1402         yptr = ybuf; 
 1403         uptr = ubuf; 
 1404         vptr = vbuf;
 1405         for(yi = 0; yi < 8; yi++)
 1406         { 
 1407             QUICKTIME_MCU111111_MID_DECL;
 1408             if(frame_height <= 0) return;
 1409             while(xi--)
 1410             { 
 1411                 QUICKTIME_MCU_INNER_INIT;
 1412                 QUICKTIME_MCU_YUV_TO_RGB(YTab[*yp++], cr, cg, cb, ip);
 1413                 QUICKTIME_MCU_YUV_TO_RGB(YTab[*yp++], cr, cg, cb, ip);
 1414                 QUICKTIME_MCU2H_INNER_TAIL(56, 56);
 1415             }
 1416             yptr += 8; 
 1417             uptr += 8; 
 1418             vptr += 8; 
 1419             frame_height -= 2;
 1420             row_pointers += interlaced ? 2 : 1;
 1421         }
 1422         ybuf += mcu_row_size << 1; 
 1423         ubuf += mcu_row_size; 
 1424         vbuf += mcu_row_size;
 1425     }
 1426 }
 1427 
 1428 int quicktime_fastjpg_decode_211111(quicktime_jpeg_t *jpeg_info, 
 1429                                 unsigned char **output_rows,
 1430                                 int jpeg_width,
 1431                                 int jpeg_height,
 1432                                 int interlaced,
 1433                                 int row_offset,
 1434                                 int frame_width,
 1435                                 int frame_height)
 1436 {
 1437     long x, mcu_cols, mcu_rows;
 1438     long *qtab0, *qtab1, *qtab2;
 1439     unsigned char *Ybuf, *Ubuf, *Vbuf;
 1440     unsigned long rst_count;
 1441     unsigned long rst_skip = 0;
 1442     unsigned long orow_size = frame_width * 3 * (interlaced ? 2 : 1);
 1443 
 1444     if(interlaced) frame_height >>= 1;
 1445     frame_width += 1; 
 1446     frame_width >>= 1; /* 2h */
 1447     qtab0 = jpeg_info->quant_tables[jpeg_info->jpg_comps[0].qtbl_num];
 1448     qtab1 = jpeg_info->quant_tables[jpeg_info->jpg_comps[1].qtbl_num];
 1449     qtab2 = jpeg_info->quant_tables[jpeg_info->jpg_comps[2].qtbl_num];
 1450 
 1451     mcu_cols = (jpeg_width + 15) / 16;
 1452     mcu_rows = (jpeg_height + 7) / 8;
 1453     jpeg_info->marker = 0x00;
 1454 
 1455     rst_count = jpeg_info->jpg_rst_interval;
 1456     output_rows += row_offset;
 1457     while(mcu_rows--)
 1458     { 
 1459         Ybuf = jpeg_info->yuvbufs.ybuf; 
 1460         Ubuf = jpeg_info->yuvbufs.ubuf; 
 1461         Vbuf = jpeg_info->yuvbufs.vbuf;
 1462         x = mcu_cols; 
 1463         while(x--)
 1464         {
 1465             if(rst_skip)
 1466             {
 1467                 rst_skip--;
 1468                 memset(Ybuf, 0, (DCTSIZE2 << 1));
 1469                 memset(Ubuf, 0x80, DCTSIZE2);
 1470                 memset(Vbuf, 0x80, DCTSIZE2);
 1471                 Ybuf += (DCTSIZE2 << 1);
 1472                 Ubuf += DCTSIZE2;
 1473                 Vbuf += DCTSIZE2;
 1474             }
 1475             else
 1476             {
 1477                 QUICKTIME_FASTJPG_HANDLE_RST(jpeg_info->jpg_rst_interval, rst_count);
 1478 
 1479                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[0]), jpeg_info->jpg_dct_buf, qtab0, Ybuf); Ybuf += DCTSIZE2;
 1480                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[0]), jpeg_info->jpg_dct_buf, qtab0, Ybuf); Ybuf += DCTSIZE2;
 1481                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[1]), jpeg_info->jpg_dct_buf, qtab1, Ubuf); Ubuf += DCTSIZE2;
 1482                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[2]), jpeg_info->jpg_dct_buf, qtab2, Vbuf); Vbuf += DCTSIZE2;
 1483 
 1484                 if(jpeg_info->marker == 0)
 1485                     jpeg_info->marker = quicktime_fastjpg_check_for_marker(jpeg_info);
 1486                 QUICKTIME_FASTJPG_TEST_MARKER;
 1487             }
 1488         } /* end of mcu_cols */
 1489 
 1490 /* NOTE: imagex already >> 1 above */
 1491         if(jpeg_width <= frame_width)
 1492         {
 1493             quicktime_fastjpg_double_mcu(jpeg_info->yuvbufs.ybuf, (mcu_cols << 1));
 1494             quicktime_fastjpg_double_mcu(jpeg_info->yuvbufs.ubuf, mcu_cols);
 1495             quicktime_fastjpg_double_mcu(jpeg_info->yuvbufs.vbuf, mcu_cols);
 1496         }
 1497 
 1498         quicktime_fastjpg_MCU211111_to_RGB(jpeg_info, 
 1499                 output_rows,
 1500                 frame_width,
 1501                 (frame_height < 8 ? frame_height : 8),
 1502                 ((mcu_cols << 1) * DCTSIZE2),
 1503                 orow_size,
 1504                 &(jpeg_info->yuvbufs),
 1505                 interlaced);
 1506 
 1507         frame_height -= 8;
 1508         output_rows += interlaced ? 16 : 8;
 1509     } /* end of mcu_rows */
 1510 
 1511     if(jpeg_info->marker) 
 1512     { 
 1513         jpeg_info->jpg_h_bbuf = 0; 
 1514         jpeg_info->jpg_h_bnum = 0; 
 1515     }
 1516     return 0;
 1517 }
 1518 
 1519 int quicktime_fastjpg_MCU111111_to_RGB(QUICKTIME_MCU_ARGS)
 1520 {
 1521     QUICKTIME_MCU_VARS
 1522     QUICKTIME_MCU111111_MID_VARS
 1523     QUICKTIME_MCU_INNER_VARS
 1524     
 1525     while(frame_height > 0)
 1526     { 
 1527         yptr = ybuf; 
 1528         uptr = ubuf; 
 1529         vptr = vbuf;
 1530         for(yi = 0; yi < 8; yi++)
 1531         { 
 1532             QUICKTIME_MCU111111_MID_DECL;
 1533             if(frame_height <= 0) return;
 1534             while(xi--)
 1535             { 
 1536                 QUICKTIME_MCU_INNER_INIT;
 1537                 QUICKTIME_MCU_YUV_TO_RGB(YTab[*yp++], cr, cg, cb, ip);
 1538                 QUICKTIME_MCU1H_INNER_TAIL(56);
 1539             }
 1540             yptr += 8; 
 1541             uptr += 8; 
 1542             vptr += 8; 
 1543             frame_height--;  
 1544             row_pointers += interlaced ? 2 : 1;
 1545         }
 1546         ybuf += mcu_row_size; 
 1547         ubuf += mcu_row_size; 
 1548         vbuf += mcu_row_size;
 1549     }
 1550 }
 1551 
 1552 int quicktime_fastjpg_decode_111111(quicktime_jpeg_t *jpeg_info, 
 1553                                 unsigned char **output_rows,
 1554                                 int jpeg_width,
 1555                                 int jpeg_height,
 1556                                 int interlaced,
 1557                                 int row_offset,
 1558                                 int frame_width,
 1559                                 int frame_height, 
 1560                                 int grey)
 1561 {
 1562     long x, mcu_cols, mcu_rows;
 1563     long *qtab0, *qtab1, *qtab2;
 1564     unsigned char *Ybuf, *Ubuf, *Vbuf;
 1565     unsigned long rst_count;
 1566     unsigned long rst_skip = 0;
 1567     unsigned long orow_size = frame_width * 3 * (interlaced ? 2 : 1);
 1568 
 1569     if(interlaced) frame_height >>= 1;
 1570     qtab0 = jpeg_info->quant_tables[jpeg_info->jpg_comps[0].qtbl_num];
 1571     qtab1 = jpeg_info->quant_tables[jpeg_info->jpg_comps[1].qtbl_num];
 1572     qtab2 = jpeg_info->quant_tables[jpeg_info->jpg_comps[2].qtbl_num];
 1573 
 1574     mcu_cols = (jpeg_width + 7) / 8;
 1575     mcu_rows = (jpeg_height + 7) / 8;
 1576     jpeg_info->marker = 0x00;
 1577 
 1578     rst_count = jpeg_info->jpg_rst_interval;
 1579     output_rows += row_offset;
 1580     while(mcu_rows--)
 1581     { 
 1582         Ybuf = jpeg_info->yuvbufs.ybuf; 
 1583         Ubuf = jpeg_info->yuvbufs.ubuf; 
 1584         Vbuf = jpeg_info->yuvbufs.vbuf;
 1585         x = mcu_cols; 
 1586         while(x--)
 1587         {
 1588             if(rst_skip)
 1589             {
 1590                 rst_skip--;
 1591                 
 1592                 if(Ybuf != jpeg_info->yuvbufs.ybuf)
 1593                 {
 1594                     unsigned char *prev;
 1595                     prev = Ybuf - DCTSIZE2;
 1596                     memcpy(Ybuf, prev, DCTSIZE2);
 1597                     Ybuf += DCTSIZE2;
 1598                     prev = Ubuf - DCTSIZE2; 
 1599                     memcpy(Ubuf, prev, DCTSIZE2);
 1600                     prev = Vbuf - DCTSIZE2; 
 1601                     memcpy(Vbuf, prev, DCTSIZE2);
 1602                     Ubuf += DCTSIZE2;
 1603                     Vbuf += DCTSIZE2;
 1604                 }
 1605                 else
 1606                 {
 1607                     memset(Ybuf, 0, DCTSIZE2);
 1608                     Ybuf += DCTSIZE2;
 1609                     memset(Ubuf, 0x80, DCTSIZE2);
 1610                     memset(Vbuf, 0x80, DCTSIZE2);
 1611                     Ubuf += DCTSIZE2;
 1612                     Vbuf += DCTSIZE2;
 1613                 }
 1614             }
 1615             else
 1616             {
 1617                 QUICKTIME_FASTJPG_HANDLE_RST(jpeg_info->jpg_rst_interval, rst_count);
 1618 
 1619                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[0]), jpeg_info->jpg_dct_buf, qtab0, Ybuf); Ybuf += DCTSIZE2;
 1620                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[1]), jpeg_info->jpg_dct_buf, qtab1, Ubuf); Ubuf += DCTSIZE2;
 1621                 quicktime_fastjpg_huffparse(jpeg_info, &(jpeg_info->jpg_comps[2]), jpeg_info->jpg_dct_buf, qtab2, Vbuf); Vbuf += DCTSIZE2;
 1622 
 1623                 if(jpeg_info->marker == 0)
 1624                     jpeg_info->marker = quicktime_fastjpg_check_for_marker(jpeg_info);
 1625                 QUICKTIME_FASTJPG_TEST_MARKER;
 1626             }
 1627         } /* end of mcu_cols */
 1628 
 1629 /* NOTE: imagex already >> 1 above */
 1630         quicktime_fastjpg_MCU111111_to_RGB(jpeg_info, 
 1631                 output_rows,
 1632                 frame_width,
 1633                 (frame_height < 8 ? frame_height : 8),
 1634                 (mcu_cols * DCTSIZE2),
 1635                 orow_size,
 1636                 &(jpeg_info->yuvbufs),
 1637                 interlaced);
 1638 
 1639         frame_height -= 8;
 1640         output_rows += interlaced ? 16 : 8;
 1641     } /* end of mcu_rows */
 1642 
 1643     if(jpeg_info->marker) 
 1644     { 
 1645         jpeg_info->jpg_h_bbuf = 0; 
 1646         jpeg_info->jpg_h_bnum = 0; 
 1647     }
 1648     return 0;
 1649 }
 1650 
 1651 int quicktime_fastjpg_decode(unsigned char *chunk, 
 1652                         long chunk_size, 
 1653                         unsigned char **output_rows, 
 1654                         quicktime_jpeg_t *jpeg_info,
 1655                         int frame_width,
 1656                         int frame_height,
 1657                         int interlaced)
 1658 {
 1659     int base_y, row_offset;
 1660     int ijpeg = 0;
 1661     int result = 0;
 1662     jpeg_info->mjpa_info.valid = 0;
 1663 
 1664     jpeg_info->chunk = chunk;
 1665     jpeg_info->chunk_size = chunk_size;
 1666 
 1667     for(base_y = 0; base_y < (interlaced ? 2 : 1); base_y++)
 1668     {
 1669 /* Reset structures */
 1670         jpeg_info->jpg_saw_EOI = 0;
 1671         jpeg_info->jpg_saw_SOI = jpeg_info->jpg_saw_SOF = jpeg_info->jpg_saw_SOS = jpeg_info->jpg_saw_DHT = jpeg_info->jpg_saw_DQT = 0;
 1672 
 1673         if(quicktime_fastjpg_read_markers(jpeg_info))
 1674         {
 1675             printf("quicktime_fastjpg_decode read markers failed\n");
 1676         }
 1677 
 1678         quicktime_fastjpg_resethuffman(jpeg_info);
 1679         if(interlaced)
 1680         {
 1681             row_offset = (base_y == 0) ? 0 : 1;
 1682         }
 1683         else
 1684             row_offset = 0;
 1685         
 1686         if((!jpeg_info->jpg_saw_DHT) && (!jpeg_info->jpg_std_DHT_flag))
 1687         {
 1688             quicktime_fastjpg_buildstdhuffman(jpeg_info);
 1689         }
 1690 
 1691         if(!jpeg_info->jpg_saw_DQT)
 1692         { 
 1693             quicktime_fastjpg_buildstdDQT(jpeg_info, 100);
 1694         }
 1695 
 1696         jpeg_info->marker = 0x00;
 1697         /*if(jpeg_info->jpg_width > frame_width) */
 1698             quicktime_fastjpg_initMCU(jpeg_info, jpeg_info->jpg_width, 0, 0);
 1699 
 1700 /* Perform the decompression */
 1701         if((jpeg_info->jpg_num_comps == 3) && (jpeg_info->jpg_comps_in_scan == 3) &&
 1702             (jpeg_info->jpg_comps[1].hvsample == 0x11) && (jpeg_info->jpg_comps[2].hvsample== 0x11))
 1703         {
 1704             if(jpeg_info->jpg_comps[0].hvsample == 0x41) /* 411 */
 1705             { 
 1706                 quicktime_fastjpg_decode_411111(jpeg_info, 
 1707                                 output_rows,
 1708                                 jpeg_info->jpg_width,
 1709                                 jpeg_info->jpg_height,
 1710                                 interlaced,
 1711                                 row_offset,
 1712                                 frame_width,
 1713                                 frame_height); 
 1714             }
 1715             else 
 1716             if(jpeg_info->jpg_comps[0].hvsample == 0x22) /* 411 */
 1717             { 
 1718                 quicktime_fastjpg_decode_221111(jpeg_info,
 1719                                 output_rows, 
 1720                                 jpeg_info->jpg_width,
 1721                                 jpeg_info->jpg_height,
 1722                                 interlaced,
 1723                                 row_offset,
 1724                                 frame_width,
 1725                                 frame_height); 
 1726             }
 1727             else 
 1728             if(jpeg_info->jpg_comps[0].hvsample == 0x21) /* 211 */
 1729             {
 1730                 quicktime_fastjpg_decode_211111(jpeg_info,
 1731                                 output_rows, 
 1732                                 jpeg_info->jpg_width,
 1733                                 jpeg_info->jpg_height,
 1734                                 interlaced,
 1735                                 row_offset,
 1736                                 frame_width,
 1737                                 frame_height); 
 1738             }
 1739             else if(jpeg_info->jpg_comps[0].hvsample == 0x11) /* 111 */
 1740             { 
 1741                 quicktime_fastjpg_decode_111111(jpeg_info,
 1742                                 output_rows, 
 1743                                 jpeg_info->jpg_width,
 1744                                 jpeg_info->jpg_height,
 1745                                 interlaced,
 1746                                 row_offset,
 1747                                 frame_width,
 1748                                 frame_height,
 1749                                 0); 
 1750             }
 1751             else 
 1752             {
 1753                 printf("quicktime_fastjpg_decode: cmps %d %d mcu %04x %04x %04x unsupported\n",
 1754                             jpeg_info->jpg_num_comps,
 1755                             jpeg_info->jpg_comps_in_scan,
 1756                             jpeg_info->jpg_comps[0].hvsample,
 1757                             jpeg_info->jpg_comps[1].hvsample,
 1758                             jpeg_info->jpg_comps[2].hvsample); 
 1759                 break;
 1760             }
 1761         }
 1762         else 
 1763         if((jpeg_info->jpg_num_comps == 1) || (jpeg_info->jpg_comps_in_scan == 1))
 1764         {
 1765 /* Grayscale not supported */
 1766             quicktime_fastjpg_decode_111111(jpeg_info,
 1767                                 output_rows, 
 1768                                 jpeg_info->jpg_width,
 1769                                 jpeg_info->jpg_height,
 1770                                 interlaced,
 1771                                 row_offset,
 1772                                 frame_width,
 1773                                 frame_height,
 1774                                 1);
 1775         }
 1776         else
 1777         {
 1778             printf("quicktime_fastjpg_decode: cmps %d %d mcu %04x %04x %04x unsupported.\n",
 1779                 jpeg_info->jpg_num_comps,
 1780                 jpeg_info->jpg_comps_in_scan,
 1781                 jpeg_info->jpg_comps[0].hvsample,
 1782                 jpeg_info->jpg_comps[1].hvsample,
 1783                 jpeg_info->jpg_comps[2].hvsample);
 1784             break;
 1785         }
 1786 
 1787         if(jpeg_info->marker == M_EOI) 
 1788         { 
 1789             jpeg_info->jpg_saw_EOI = 1; 
 1790             jpeg_info->marker = 0x00; 
 1791         }
 1792         else 
 1793         if(!jpeg_info->jpg_saw_EOI) 
 1794             if(quicktime_fastjpg_readEOI(jpeg_info)) 
 1795                 break;
 1796 
 1797     }
 1798     return result;
 1799 }