2 #include "fastjpgmacro.h"
4 /* JPEG decoder from XAnim which ended up 10% slower than libjpeg */
61 static long JJ_ZAG
[DCTSIZE2
+ 16] =
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
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
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
97 int quicktime_fastjpg_skip(quicktime_jpeg_t
*jpeg_info
, long len
)
99 if(len
> jpeg_info
->chunk_size
)
100 jpeg_info
->chunk
+= jpeg_info
->chunk_size
;
102 jpeg_info
->chunk
+= len
;
107 int quicktime_fastjpg_readbyte(quicktime_jpeg_t
*jpeg_info
)
109 if(jpeg_info
->chunk_size
> 0)
111 jpeg_info
->chunk_size
--;
112 return *(jpeg_info
->chunk
++);
118 int quicktime_fastjpg_readint16(quicktime_jpeg_t
*jpeg_info
)
120 if(jpeg_info
->chunk_size
> 1)
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];
130 int quicktime_fastjpg_readint32(quicktime_jpeg_t
*jpeg_info
)
132 if(jpeg_info
->chunk_size
> 3)
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
++)));
144 int quicktime_fastjpg_eof(quicktime_jpeg_t
*jpeg_info
)
146 if(jpeg_info
->chunk_size
> 0)
152 int quicktime_fastjpg_init_limittable(quicktime_jpeg_t
*jpeg_info
)
154 unsigned char *table
;
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;
160 /* create negative subscripts for simple table */
161 table
= jpeg_info
->jpg_samp_limit
+ MAXJSAMPLE
+ 1;
163 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
164 memset(table
- (MAXJSAMPLE
+ 1), 0, (MAXJSAMPLE
+ 1));
166 /* Main part of "simple" table: limit[x] = x */
167 for(i
= 0; i
<= MAXJSAMPLE
; i
++) table
[i
] = (unsigned char)i
;
169 /* Point to where post-IDCT table starts */
170 table
+= CENTERJSAMPLE
;
172 /* End of simple table, rest of first half of post-IDCT table */
174 for(i
= CENTERJSAMPLE
; i
< 2 * (MAXJSAMPLE
+ 1); i
++) table
[i
] = MAXJSAMPLE
;
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
);
182 int quicktime_fastjpg_init_yuv(quicktime_jpeg_t
*jpeg_info
)
185 float t_ub
, t_vr
, t_ug
, t_vg
;
186 float t2_ub
, t2_vr
, t2_ug
, t2_vg
;
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));
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;
203 for(i
= 0; i
< 256; i
++)
205 float x
= (float)(2 * i
- 255);
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));
216 int quicktime_fastjpg_init(quicktime_jpeg_t
*jpeg_info
)
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
);
232 int quicktime_fastjpg_deleteMCU(quicktime_jpeg_t
*jpeg_info
)
234 if(jpeg_info
->yuvbufs
.allocated
)
236 free(jpeg_info
->yuvbufs
.ybuf
);
237 free(jpeg_info
->yuvbufs
.ubuf
);
238 free(jpeg_info
->yuvbufs
.vbuf
);
240 jpeg_info
->yuvbufs
.ybuf
= 0;
241 jpeg_info
->yuvbufs
.ubuf
= 0;
242 jpeg_info
->yuvbufs
.vbuf
= 0;
248 int quicktime_fastjpg_delete(quicktime_jpeg_t
*jpeg_info
)
251 for(i
= 0; i
< TOTAL_QUANT_TBLS
; i
++)
252 if(jpeg_info
->quant_tables
[i
])
254 free(jpeg_info
->quant_tables
[i
]);
255 jpeg_info
->quant_tables
[i
] = 0;
258 if(jpeg_info
->jpg_samp_limit
)
260 free(jpeg_info
->jpg_samp_limit
);
261 jpeg_info
->jpg_samp_limit
= 0;
264 quicktime_fastjpg_deleteMCU(jpeg_info
);
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
);
273 int quicktime_fastjpg_resethuffman(quicktime_jpeg_t
*jpeg_info
)
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;
282 int quicktime_fastjpg_buildhuffman(quicktime_jpeg_t
*jpeg_info
,
283 quicktime_jpeg_huffman
*htable
,
284 unsigned char *hbits
,
285 unsigned char *hvals
)
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];
292 /*** generate code lengths for each symbol */
294 for(clen
= 1; clen
<= 16; clen
++)
296 for(i
= 1; i
<= (unsigned long)(hbits
[clen
]); i
++)
297 huffsize
[num_syms
++] = (unsigned char)(clen
);
299 huffsize
[num_syms
] = 0;
301 /*** generate codes */
307 while(((unsigned long)huffsize
[p
]) == si
)
309 huffcode
[p
++] = code
;
316 /* Init mincode/maxcode/valptr arrays */
318 for(l
= 1; l
<= 16; l
++)
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 */
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 */
334 htable
->maxcode
[17] = 0xFFFFFL
; /* ensures huff_DECODE terminates */
337 /* Init huffman cache */
338 memset((char *)htable
->cache
, 0, ((1 << HUFF_LOOKAHEAD
) * sizeof(unsigned int16_t)));
340 for (l
= 1; l
<= HUFF_LOOKAHEAD
; l
++)
342 for (i
= 1; i
<= (unsigned long) htable
->bits
[l
]; i
++, p
++)
344 int16_t the_code
= (unsigned int16_t)((l
<< 8) | htable
->vals
[p
]);
346 /* l = current code's length, p = its index in huffcode[] & huffval[]. */
347 /* Generate left-justified code followed by all possible bit sequences */
349 lookbits
= huffcode
[p
] << (HUFF_LOOKAHEAD
- l
);
350 for(ctr
= 1 << (HUFF_LOOKAHEAD
- l
); ctr
> 0; ctr
--)
352 htable
->cache
[lookbits
] = the_code
;
359 int quicktime_fastjpg_buildstdhuffman(quicktime_jpeg_t
*jpeg_info
)
362 quicktime_jpeg_huffman
*htable
;
363 unsigned char *hbits
, *Sbits
;
364 unsigned char *hvals
, *Svals
;
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 };
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 };
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,
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,
426 for(ttt
= 0; ttt
< 4; ttt
++)
428 unsigned long index
= ttt
& 1;
429 unsigned long i
, count
;
431 if (ttt
<= 1) /* DC tables */
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
;
438 Sbits
= dc_luminance_bits
;
439 Svals
= dc_luminance_vals
;
443 Sbits
= dc_chrominance_bits
;
444 Svals
= dc_chrominance_vals
;
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
;
454 Sbits
= ac_luminance_bits
;
455 Svals
= ac_luminance_vals
;
459 Sbits
= ac_chrominance_bits
;
460 Svals
= ac_chrominance_vals
;
465 for(i
= 1; i
<= 16; i
++)
473 printf("quicktime_fastjpg_buildstdhuffman: STD DHT bad count %d\n", count
);
477 for(i
= 0; i
< count
; i
++) hvals
[i
] = Svals
[i
];
480 quicktime_fastjpg_buildhuffman(jpeg_info
,
485 jpeg_info
->jpg_std_DHT_flag
= 1;
489 int quicktime_fastjpg_buildstdDQT(quicktime_jpeg_t
*jpeg_info
, long scale
)
494 unsigned int std_luminance_quant_tbl
[DCTSIZE2
] =
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
505 unsigned int std_chrominance_quant_tbl
[DCTSIZE2
] =
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
518 for(tbl_num
= 0; tbl_num
<= 1; tbl_num
++)
520 if(jpeg_info
->quant_tables
[tbl_num
] == 0)
522 jpeg_info
->quant_tables
[tbl_num
] = (long*)malloc(64 * sizeof(long));
526 table
= std_luminance_quant_tbl
;
528 table
= std_chrominance_quant_tbl
;
529 quant_table
= jpeg_info
->quant_tables
[tbl_num
];
531 for (i
= 0; i
< DCTSIZE2
; i
++)
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
;
543 int quicktime_fastjpg_get_marker(quicktime_jpeg_t
*jpeg_info
)
545 int c
, done
= 0; /* 1 - completion 2 - error */
549 c
= quicktime_fastjpg_readbyte(jpeg_info
);
553 if(quicktime_fastjpg_eof(jpeg_info
)) done
= 2;
554 c
= quicktime_fastjpg_readbyte(jpeg_info
);
557 /* now we've got 1 0xFF, keep reading until not 0xFF */
560 if(quicktime_fastjpg_eof(jpeg_info
)) done
= 2;
561 c
= quicktime_fastjpg_readbyte(jpeg_info
);
565 if (c
!= 0) done
= 1;
574 int quicktime_fastjpg_skip_marker(quicktime_jpeg_t
*jpeg_info
)
577 len
= quicktime_fastjpg_readint16(jpeg_info
);
579 if(len
<= 0) return 1;
580 if(quicktime_fastjpg_eof(jpeg_info
)) return 1;
581 while(len
--) quicktime_fastjpg_readbyte(jpeg_info
);
585 int quicktime_fastjpg_check_for_marker(quicktime_jpeg_t
*jpeg_info
)
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))
591 jpeg_info
->marker
= jpeg_info
->chunk
[1];
593 if(jpeg_info
->jpg_h_bnum
)
595 printf("quicktime_fastjpg_check_for_marker: check marker positive - lost %d bits\n",
596 jpeg_info
->jpg_h_bnum
);
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;
604 return(jpeg_info
->marker
);
607 int quicktime_fastjpg_readSOI(quicktime_jpeg_t
*jpeg_info
)
609 jpeg_info
->jpg_rst_interval
= 0;
613 int quicktime_fastjpg_readSOF(quicktime_jpeg_t
*jpeg_info
)
617 quicktime_jpeg_comp_header
*comp
;
619 len
= quicktime_fastjpg_readint16(jpeg_info
);
620 if(jpeg_info
->mjpg_kludge
)
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
);
630 for(i
= 0; i
< jpeg_info
->jpg_num_comps
; i
++)
633 comp
= &(jpeg_info
->jpg_comps
[DUMMY_COMP
]);
635 comp
= &(jpeg_info
->jpg_comps
[i
]);
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
);
641 return(quicktime_fastjpg_eof(jpeg_info
));
644 int quicktime_fastjpg_readSOS(quicktime_jpeg_t
*jpeg_info
)
647 int comp_id
, htbl_num
;
648 int jpg_Ss
, jpg_Se
, jpg_AhAl
;
650 len
= quicktime_fastjpg_readint16(jpeg_info
);
651 jpeg_info
->jpg_comps_in_scan
= quicktime_fastjpg_readbyte(jpeg_info
);
653 for(i
= 0; i
< jpeg_info
->jpg_comps_in_scan
; i
++)
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
; )
659 comp
= &(jpeg_info
->jpg_comps
[j
]);
660 if(comp
->id
== comp_id
) break;
664 if (j
> jpeg_info
->jpg_num_comps
)
666 printf("quicktime_fastjpg_readSOS: bad id %x", comp_id
);
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;
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
));
680 int quicktime_fastjpg_readDHT(quicktime_jpeg_t
*jpeg_info
)
682 int len
, i
, index
, count
;
683 unsigned long result
= 1;
684 quicktime_jpeg_huffman
*htable
;
685 unsigned char *hbits
;
686 unsigned char *hvals
;
688 jpeg_info
->jpg_std_DHT_flag
= 0;
689 len
= quicktime_fastjpg_readint16(jpeg_info
);
691 if(jpeg_info
->mjpg_kludge
) len
+= 2;
695 if(quicktime_fastjpg_eof(jpeg_info
)) return 1;
699 index
= quicktime_fastjpg_readbyte(jpeg_info
);
702 if (index
& 0x10) /* AC Table */
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
;
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
;
723 for (i
= 1; i
<= 16; i
++)
725 hbits
[i
] = quicktime_fastjpg_readbyte(jpeg_info
);
732 printf("quicktime_fastjpg_readDHT: DHT bad count %d using default.\n", count
);
738 printf("quicktime_fastjpg_readDHT: DHT count(%d) > len(%d).\n", count
, len
);
742 for(i
= 0; i
< count
; i
++) hvals
[i
] = quicktime_fastjpg_readbyte(jpeg_info
);
745 quicktime_fastjpg_buildhuffman(jpeg_info
, htable
, hbits
, hvals
);
751 /* Something is roached, but what the heck, try default DHT instead */
755 quicktime_fastjpg_readbyte(jpeg_info
);
757 quicktime_fastjpg_buildstdhuffman(jpeg_info
);
764 int quicktime_fastjpg_readDQT(quicktime_jpeg_t
*jpeg_info
)
767 len
= quicktime_fastjpg_readint16(jpeg_info
);
768 if(!jpeg_info
->mjpg_kludge
) len
-= 2;
772 long i
, tbl_num
, prec
;
775 tbl_num
= quicktime_fastjpg_readbyte(jpeg_info
);
778 prec
= (tbl_num
>> 4) & 0x0f;
779 prec
= (prec
)?(2 * DCTSIZE2
) : (DCTSIZE2
); /* 128 or 64 */
783 printf("quicktime_fastjpg_readDQT: bad DQT tnum %x\n", tbl_num
);
787 if(jpeg_info
->quant_tables
[tbl_num
] == 0)
789 jpeg_info
->quant_tables
[tbl_num
] = (long *)malloc(64 * sizeof(long));
793 if(quicktime_fastjpg_eof(jpeg_info
)) return 1;
794 quant_table
= jpeg_info
->quant_tables
[tbl_num
];
798 for(i
= 0; i
< DCTSIZE2
; i
++)
800 tmp
= quicktime_fastjpg_readint16(jpeg_info
);
801 quant_table
[JJ_ZAG
[i
]] = (long)tmp
;
807 for(i
= 0; i
< DCTSIZE2
; i
++)
809 tmp
= quicktime_fastjpg_readbyte(jpeg_info
);
810 quant_table
[JJ_ZAG
[i
]] = (long)tmp
;
817 int quicktime_fastjpg_readAPPX(quicktime_jpeg_t
*jpeg_info
)
820 len
= quicktime_fastjpg_readint32(jpeg_info
);
825 first
= quicktime_fastjpg_readint32(jpeg_info
);
827 /* if (first == 0x41564931) /* AVI1 */ */
829 /* int interleave; */
830 /* interleave = quicktime_fastjpg_readbyte(jpeg_info); */
832 /* avi_jpeg_info.valid = 1; */
833 /* avi_jpeg_info.ileave = interleave; */
836 if(len
> (0x28 - 4)) /* Maybe APPLE MJPEG A */
839 jid
= quicktime_fastjpg_readint32(jpeg_info
);
841 if(jid
== JPEG_APP1_MJPA
)
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
);
856 if(len
) quicktime_fastjpg_skip(jpeg_info
, len
);
860 int quicktime_fastjpg_readDRI(quicktime_jpeg_t
*jpeg_info
)
863 len
= quicktime_fastjpg_readint16(jpeg_info
);
864 jpeg_info
->jpg_rst_interval
= quicktime_fastjpg_readint16(jpeg_info
);
869 int quicktime_fastjpg_readEOI(quicktime_jpeg_t
*jpeg_info
)
871 while(jpeg_info
->marker
= quicktime_fastjpg_get_marker(jpeg_info
))
873 if(jpeg_info
->marker
== M_EOI
)
875 jpeg_info
->jpg_saw_EOI
= 1;
885 int quicktime_fastjpg_read_markers(quicktime_jpeg_t
*jpeg_info
)
887 int done
= 0; /* 1 = completion 2 = error */
891 if(!(jpeg_info
->marker
= quicktime_fastjpg_get_marker(jpeg_info
)))
895 /*printf("quicktime_fastjpg_read_markers %x\n", jpeg_info->marker); */
896 switch(jpeg_info
->marker
)
899 if(quicktime_fastjpg_readSOI(jpeg_info
)) done
= 2;
901 jpeg_info
->jpg_saw_SOI
= 1;
907 if(quicktime_fastjpg_readSOF(jpeg_info
)) done
= 2;
909 jpeg_info
->jpg_saw_SOF
= 1;
912 /* Not yet supported */
927 if(quicktime_fastjpg_readSOS(jpeg_info
)) done
= 2;
930 jpeg_info
->jpg_saw_SOS
= 1;
931 jpeg_info
->jpg_nxt_rst_num
= 0;
937 if(quicktime_fastjpg_readDHT(jpeg_info
)) done
= 2;
939 jpeg_info
->jpg_saw_DHT
= 1;
943 if(quicktime_fastjpg_readDQT(jpeg_info
)) done
= 2;
945 jpeg_info
->jpg_saw_DQT
= 1;
949 if(quicktime_fastjpg_readDRI(jpeg_info
)) done
= 2;
956 len
= quicktime_fastjpg_readint16(jpeg_info
);
961 quicktime_fastjpg_readbyte(jpeg_info
); len
--;
968 if(quicktime_fastjpg_readAPPX(jpeg_info
)) done
= 2;
972 printf("quicktime_fastjpg_read_markers: reached EOI without data\n");
976 case M_RST0
: /* these are all parameterless */
988 printf("quicktime_fastjpg_read_markers: unknown marker %x\n", jpeg_info
->marker
);
989 if(quicktime_fastjpg_skip_marker(jpeg_info
)) done
= 2;
991 } /* end of switch */
994 if(done
== 2) return 1; else return 0;
997 int quicktime_fastjpg_initMCU(quicktime_jpeg_t
*jpeg_info
,
1002 int twidth
= (width
+ 15) / 16;
1003 int theight
= (height
+ 15) / 16;
1004 if(theight
& 1) theight
++;
1007 twidth
*= (theight
<< 2);
1009 twidth
<<= 2; /* four dct's deep */
1011 if(!jpeg_info
->yuvbufs
.allocated
)
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
);
1020 int quicktime_fastjpg_skip_to_next_rst(quicktime_jpeg_t
*jpeg_info
)
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
)
1027 d
= *(jpeg_info
->chunk
++);
1028 jpeg_info
->chunk_size
--;
1031 if((d
!= 0) && (d
!= 0xff)) return d
;
1033 last_ff
= (d
== 0xff) ? 1 : 0;
1038 /* clears dctbuf to zeroes.
1039 * fills from huffman encode stream
1041 int quicktime_fastjpg_huffparse(quicktime_jpeg_t
*jpeg_info
,
1042 quicktime_jpeg_comp_header
*comp
,
1044 unsigned long *qtab
,
1045 unsigned char *OBuf
)
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;
1055 QUICKTIME_FASTJPG_HUFF_DECODE(huff_hdr
, huff_tbl
, jpeg_info
->jpg_h_bnum
, jpeg_info
->jpg_h_bbuf
, size
);
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
);
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
;
1072 huff_hdr
= &(jpeg_info
->jpg_ac_huff
[comp
->ac_htbl_num
]);
1073 huff_tbl
= huff_hdr
->cache
;
1074 for(i
= 1; i
< 64; )
1076 QUICKTIME_FASTJPG_HUFF_DECODE(huff_hdr
, huff_tbl
, jpeg_info
->jpg_h_bnum
, jpeg_info
->jpg_h_bbuf
, tmp
);
1078 run
= (tmp
>> 4) & 0x0f; /* leading zeroes */
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
);
1086 coeff
*= (long)qtab
[pos
];
1090 dct_buf
[pos
] = (int16_t)(coeff
);
1096 if(run
!= 15) break; /* EOB */
1101 if(c_cnt
) quicktime_rev_dct(dct_buf
, OBuf
, rnglimit
);
1104 register unsigned char *op
= OBuf
;
1105 register int jj
= 8;
1106 int16_t v
= *dct_buf
;
1107 register unsigned char dc
;
1109 v
= (v
< 0) ? ((v
- 3) >> 3) : ((v
+ 4) >> 3);
1110 dc
= rnglimit
[(int)(v
& RANGE_MASK
)];
1113 op
[0] = op
[1] = op
[2] = op
[3] = op
[4] = op
[5] = op
[6] = op
[7] = dc
;
1121 int quicktime_fastjpg_MCU411111_to_RGB(QUICKTIME_MCU_ARGS
)
1124 QUICKTIME_MCU111111_MID_VARS
1125 QUICKTIME_MCU_INNER_VARS
1127 while(frame_height
> 0)
1132 for(yi
= 0; yi
< 8; yi
++)
1134 QUICKTIME_MCU111111_MID_DECL
;
1135 if(frame_height
<= 0) return 0;
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);
1149 row_pointers
+= interlaced
? 2 : 1;
1151 ybuf
+= mcu_row_size
<< 2;
1152 ubuf
+= mcu_row_size
;
1153 vbuf
+= mcu_row_size
;
1157 int quicktime_fastjpg_decode_411111(quicktime_jpeg_t
*jpeg_info
,
1158 unsigned char **output_rows
,
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);
1173 if(interlaced
) frame_height
>>= 1;
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
];
1180 mcu_cols
= (jpeg_width
+ 31) / 32;
1181 mcu_rows
= (jpeg_height
+ 7) / 8;
1182 jpeg_info
->marker
= 0x00;
1184 rst_count
= jpeg_info
->jpg_rst_interval
;
1185 output_rows
+= row_offset
;
1188 Ybuf
= jpeg_info
->yuvbufs
.ybuf
;
1189 Ubuf
= jpeg_info
->yuvbufs
.ubuf
;
1190 Vbuf
= jpeg_info
->yuvbufs
.vbuf
;
1197 memset(Ybuf
, 0, (DCTSIZE2
<< 2));
1198 memset(Ubuf
, 0x80, DCTSIZE2
);
1199 memset(Vbuf
, 0x80, DCTSIZE2
);
1200 Ybuf
+= (DCTSIZE2
<< 2);
1206 QUICKTIME_FASTJPG_HANDLE_RST(jpeg_info
->jpg_rst_interval
, rst_count
);
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
;
1216 if(jpeg_info
->marker
== 0)
1217 jpeg_info
->marker
= quicktime_fastjpg_check_for_marker(jpeg_info
);
1218 QUICKTIME_FASTJPG_TEST_MARKER
;
1220 } /* end of mcu_cols */
1222 quicktime_fastjpg_MCU411111_to_RGB(jpeg_info
,
1225 (frame_height
< 8 ? frame_height
: 8),
1226 (mcu_cols
* DCTSIZE2
),
1228 &(jpeg_info
->yuvbufs
),
1231 output_rows
+= interlaced
? 16 : 8;
1232 } /* end of mcu_rows */
1234 if(jpeg_info
->marker
)
1236 jpeg_info
->jpg_h_bbuf
= 0;
1237 jpeg_info
->jpg_h_bnum
= 0;
1242 int quicktime_fastjpg_MCU221111_to_RGB(QUICKTIME_MCU_ARGS
)
1245 QUICKTIME_MCU221111_MID_VARS
1246 QUICKTIME_MCU_INNER_VARS
1248 while(frame_height
> 0)
1253 for(yi
= 0; yi
< 8; yi
++)
1255 QUICKTIME_MCU221111_MID_DECL
;
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);
1270 ybuf
+= mcu_row_size
<< 2;
1271 ubuf
+= mcu_row_size
;
1272 vbuf
+= mcu_row_size
;
1276 int quicktime_fastjpg_decode_221111(quicktime_jpeg_t
*jpeg_info
,
1277 unsigned char **output_rows
,
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);
1292 if(interlaced
) frame_height
>>= 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
];
1299 mcu_cols
= (jpeg_width
+ 15) / 16;
1300 mcu_rows
= (jpeg_height
+ 15) / 16;
1301 jpeg_info
->marker
= 0x00;
1303 rst_count
= jpeg_info
->jpg_rst_interval
;
1304 output_rows
+= row_offset
;
1307 Ybuf
= jpeg_info
->yuvbufs
.ybuf
;
1308 Ubuf
= jpeg_info
->yuvbufs
.ubuf
;
1309 Vbuf
= jpeg_info
->yuvbufs
.vbuf
;
1316 memset(Ybuf
, 0, (DCTSIZE2
<< 2));
1317 memset(Ubuf
, 0x80, DCTSIZE2
);
1318 memset(Vbuf
, 0x80, DCTSIZE2
);
1319 Ybuf
+= (DCTSIZE2
<< 2);
1325 QUICKTIME_FASTJPG_HANDLE_RST(jpeg_info
->jpg_rst_interval
, rst_count
);
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
;
1335 if(jpeg_info
->marker
== 0)
1336 jpeg_info
->marker
= quicktime_fastjpg_check_for_marker(jpeg_info
);
1337 QUICKTIME_FASTJPG_TEST_MARKER
;
1339 } /* end of mcu_cols */
1341 quicktime_fastjpg_MCU221111_to_RGB(jpeg_info
,
1344 (frame_height
< 16 ? frame_height
: 16),
1345 (mcu_cols
* DCTSIZE2
),
1347 &(jpeg_info
->yuvbufs
),
1350 output_rows
+= interlaced
? 32 : 16;
1351 } /* end of mcu_rows */
1353 if(jpeg_info
->marker
)
1355 jpeg_info
->jpg_h_bbuf
= 0;
1356 jpeg_info
->jpg_h_bnum
= 0;
1361 int quicktime_fastjpg_double_mcu(unsigned char *ptr
, int mcus
)
1363 unsigned char *sblk
, *dblk
;
1364 int blks
= mcus
* 8;
1367 sblk
= ptr
+ (blks
* 8) - 8;
1368 dblk
= ptr
+ (blks
* 16) - 8;
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];
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];
1394 int quicktime_fastjpg_MCU211111_to_RGB(QUICKTIME_MCU_ARGS
)
1397 QUICKTIME_MCU111111_MID_VARS
1398 QUICKTIME_MCU_INNER_VARS
1400 while(frame_height
> 0)
1405 for(yi
= 0; yi
< 8; yi
++)
1407 QUICKTIME_MCU111111_MID_DECL
;
1408 if(frame_height
<= 0) return;
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);
1420 row_pointers
+= interlaced
? 2 : 1;
1422 ybuf
+= mcu_row_size
<< 1;
1423 ubuf
+= mcu_row_size
;
1424 vbuf
+= mcu_row_size
;
1428 int quicktime_fastjpg_decode_211111(quicktime_jpeg_t
*jpeg_info
,
1429 unsigned char **output_rows
,
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);
1444 if(interlaced
) frame_height
>>= 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
];
1451 mcu_cols
= (jpeg_width
+ 15) / 16;
1452 mcu_rows
= (jpeg_height
+ 7) / 8;
1453 jpeg_info
->marker
= 0x00;
1455 rst_count
= jpeg_info
->jpg_rst_interval
;
1456 output_rows
+= row_offset
;
1459 Ybuf
= jpeg_info
->yuvbufs
.ybuf
;
1460 Ubuf
= jpeg_info
->yuvbufs
.ubuf
;
1461 Vbuf
= jpeg_info
->yuvbufs
.vbuf
;
1468 memset(Ybuf
, 0, (DCTSIZE2
<< 1));
1469 memset(Ubuf
, 0x80, DCTSIZE2
);
1470 memset(Vbuf
, 0x80, DCTSIZE2
);
1471 Ybuf
+= (DCTSIZE2
<< 1);
1477 QUICKTIME_FASTJPG_HANDLE_RST(jpeg_info
->jpg_rst_interval
, rst_count
);
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
;
1484 if(jpeg_info
->marker
== 0)
1485 jpeg_info
->marker
= quicktime_fastjpg_check_for_marker(jpeg_info
);
1486 QUICKTIME_FASTJPG_TEST_MARKER
;
1488 } /* end of mcu_cols */
1490 /* NOTE: imagex already >> 1 above */
1491 if(jpeg_width
<= frame_width
)
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
);
1498 quicktime_fastjpg_MCU211111_to_RGB(jpeg_info
,
1501 (frame_height
< 8 ? frame_height
: 8),
1502 ((mcu_cols
<< 1) * DCTSIZE2
),
1504 &(jpeg_info
->yuvbufs
),
1508 output_rows
+= interlaced
? 16 : 8;
1509 } /* end of mcu_rows */
1511 if(jpeg_info
->marker
)
1513 jpeg_info
->jpg_h_bbuf
= 0;
1514 jpeg_info
->jpg_h_bnum
= 0;
1519 int quicktime_fastjpg_MCU111111_to_RGB(QUICKTIME_MCU_ARGS
)
1522 QUICKTIME_MCU111111_MID_VARS
1523 QUICKTIME_MCU_INNER_VARS
1525 while(frame_height
> 0)
1530 for(yi
= 0; yi
< 8; yi
++)
1532 QUICKTIME_MCU111111_MID_DECL
;
1533 if(frame_height
<= 0) return;
1536 QUICKTIME_MCU_INNER_INIT
;
1537 QUICKTIME_MCU_YUV_TO_RGB(YTab
[*yp
++], cr
, cg
, cb
, ip
);
1538 QUICKTIME_MCU1H_INNER_TAIL(56);
1544 row_pointers
+= interlaced
? 2 : 1;
1546 ybuf
+= mcu_row_size
;
1547 ubuf
+= mcu_row_size
;
1548 vbuf
+= mcu_row_size
;
1552 int quicktime_fastjpg_decode_111111(quicktime_jpeg_t
*jpeg_info
,
1553 unsigned char **output_rows
,
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);
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
];
1574 mcu_cols
= (jpeg_width
+ 7) / 8;
1575 mcu_rows
= (jpeg_height
+ 7) / 8;
1576 jpeg_info
->marker
= 0x00;
1578 rst_count
= jpeg_info
->jpg_rst_interval
;
1579 output_rows
+= row_offset
;
1582 Ybuf
= jpeg_info
->yuvbufs
.ybuf
;
1583 Ubuf
= jpeg_info
->yuvbufs
.ubuf
;
1584 Vbuf
= jpeg_info
->yuvbufs
.vbuf
;
1592 if(Ybuf
!= jpeg_info
->yuvbufs
.ybuf
)
1594 unsigned char *prev
;
1595 prev
= Ybuf
- DCTSIZE2
;
1596 memcpy(Ybuf
, prev
, DCTSIZE2
);
1598 prev
= Ubuf
- DCTSIZE2
;
1599 memcpy(Ubuf
, prev
, DCTSIZE2
);
1600 prev
= Vbuf
- DCTSIZE2
;
1601 memcpy(Vbuf
, prev
, DCTSIZE2
);
1607 memset(Ybuf
, 0, DCTSIZE2
);
1609 memset(Ubuf
, 0x80, DCTSIZE2
);
1610 memset(Vbuf
, 0x80, DCTSIZE2
);
1617 QUICKTIME_FASTJPG_HANDLE_RST(jpeg_info
->jpg_rst_interval
, rst_count
);
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
;
1623 if(jpeg_info
->marker
== 0)
1624 jpeg_info
->marker
= quicktime_fastjpg_check_for_marker(jpeg_info
);
1625 QUICKTIME_FASTJPG_TEST_MARKER
;
1627 } /* end of mcu_cols */
1629 /* NOTE: imagex already >> 1 above */
1630 quicktime_fastjpg_MCU111111_to_RGB(jpeg_info
,
1633 (frame_height
< 8 ? frame_height
: 8),
1634 (mcu_cols
* DCTSIZE2
),
1636 &(jpeg_info
->yuvbufs
),
1640 output_rows
+= interlaced
? 16 : 8;
1641 } /* end of mcu_rows */
1643 if(jpeg_info
->marker
)
1645 jpeg_info
->jpg_h_bbuf
= 0;
1646 jpeg_info
->jpg_h_bnum
= 0;
1651 int quicktime_fastjpg_decode(unsigned char *chunk
,
1653 unsigned char **output_rows
,
1654 quicktime_jpeg_t
*jpeg_info
,
1659 int base_y
, row_offset
;
1662 jpeg_info
->mjpa_info
.valid
= 0;
1664 jpeg_info
->chunk
= chunk
;
1665 jpeg_info
->chunk_size
= chunk_size
;
1667 for(base_y
= 0; base_y
< (interlaced
? 2 : 1); base_y
++)
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;
1673 if(quicktime_fastjpg_read_markers(jpeg_info
))
1675 printf("quicktime_fastjpg_decode read markers failed\n");
1678 quicktime_fastjpg_resethuffman(jpeg_info
);
1681 row_offset
= (base_y
== 0) ? 0 : 1;
1686 if((!jpeg_info
->jpg_saw_DHT
) && (!jpeg_info
->jpg_std_DHT_flag
))
1688 quicktime_fastjpg_buildstdhuffman(jpeg_info
);
1691 if(!jpeg_info
->jpg_saw_DQT
)
1693 quicktime_fastjpg_buildstdDQT(jpeg_info
, 100);
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);
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))
1704 if(jpeg_info
->jpg_comps
[0].hvsample
== 0x41) /* 411 */
1706 quicktime_fastjpg_decode_411111(jpeg_info
,
1708 jpeg_info
->jpg_width
,
1709 jpeg_info
->jpg_height
,
1716 if(jpeg_info
->jpg_comps
[0].hvsample
== 0x22) /* 411 */
1718 quicktime_fastjpg_decode_221111(jpeg_info
,
1720 jpeg_info
->jpg_width
,
1721 jpeg_info
->jpg_height
,
1728 if(jpeg_info
->jpg_comps
[0].hvsample
== 0x21) /* 211 */
1730 quicktime_fastjpg_decode_211111(jpeg_info
,
1732 jpeg_info
->jpg_width
,
1733 jpeg_info
->jpg_height
,
1739 else if(jpeg_info
->jpg_comps
[0].hvsample
== 0x11) /* 111 */
1741 quicktime_fastjpg_decode_111111(jpeg_info
,
1743 jpeg_info
->jpg_width
,
1744 jpeg_info
->jpg_height
,
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
);
1763 if((jpeg_info
->jpg_num_comps
== 1) || (jpeg_info
->jpg_comps_in_scan
== 1))
1765 /* Grayscale not supported */
1766 quicktime_fastjpg_decode_111111(jpeg_info
,
1768 jpeg_info
->jpg_width
,
1769 jpeg_info
->jpg_height
,
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
);
1787 if(jpeg_info
->marker
== M_EOI
)
1789 jpeg_info
->jpg_saw_EOI
= 1;
1790 jpeg_info
->marker
= 0x00;
1793 if(!jpeg_info
->jpg_saw_EOI
)
1794 if(quicktime_fastjpg_readEOI(jpeg_info
))