oops, I shifted the wrong value.
[swfdec.git] / swfdec / jpeg / jpeg.c
blobef895e44d2847f4f45abd50829f5dba6e8e9e04c
2 #ifdef HAVE_CONFIG_H
3 #include "config.h"
4 #endif
6 #include <liboil/liboil.h>
7 #include <liboil/liboil-stdint.h>
8 #include <cogcompat.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
14 #include "jpeg.h"
17 extern uint8_t jpeg_standard_tables[];
18 extern int jpeg_standard_tables_size;
20 void jpeg_decoder_define_huffman_tables (JpegDecoder * dec);
21 void jpeg_decoder_define_arithmetic_conditioning (JpegDecoder *dec);
22 void jpeg_decoder_define_quantization_tables (JpegDecoder *dec);
23 void jpeg_decoder_define_restart_interval (JpegDecoder *dec);
24 void jpeg_decoder_start_of_frame (JpegDecoder * dec, int marker);
25 void jpeg_decoder_start_of_scan (JpegDecoder * dec);
28 /* misc helper function declarations */
30 static void jpeg_load_standard_huffman_tables (JpegDecoder * dec);
32 static void jpeg_decoder_verify_header (JpegDecoder *dec);
33 static void jpeg_decoder_init_decoder (JpegDecoder *dec);
36 void
37 jpeg_decoder_error(JpegDecoder *dec, const char *fmt, ...)
39 va_list varargs;
41 if (dec->error) return;
43 va_start (varargs, fmt);
44 #ifdef HAVE_VASPRINTF
45 vasprintf(&dec->error_message, fmt, varargs);
46 #else
47 dec->error_message = malloc(250);
48 vsnprintf(dec->error_message, 250 - 1, fmt, varargs);
49 dec->error_message[250 - 1] = 0;
50 #endif
51 va_end (varargs);
53 dec->error = TRUE;
56 #define jpeg_decoder_error(dec, ...) { \
57 COG_ERROR("decoder error: "__VA_ARGS__); \
58 jpeg_decoder_error (dec, __VA_ARGS__); \
61 static void
62 jpeg_decoder_verify_header (JpegDecoder *dec)
64 int max_quant_table = 0;
65 int i;
67 if (dec->sof_type != JPEG_MARKER_SOF_0) {
68 COG_ERROR("only handle baseline DCT");
69 dec->error = TRUE;
72 if (dec->width < 1) {
73 COG_ERROR("width can't be 0");
74 dec->error = TRUE;
77 if (dec->height < 1) {
78 COG_ERROR("height can't be 0");
79 dec->error = TRUE;
82 switch (dec->sof_type) {
83 case JPEG_MARKER_SOF_0:
84 /* baseline DCT */
85 max_quant_table = 3;
86 if (dec->depth != 8) {
87 COG_ERROR("depth must be 8 (%d)", dec->depth);
88 dec->error = TRUE;
90 break;
91 case JPEG_MARKER_SOF_1:
92 /* extended DCT */
93 max_quant_table = 3;
94 if (dec->depth != 8 && dec->depth != 12) {
95 COG_ERROR("depth must be 8 or 12 (%d)", dec->depth);
96 dec->error = TRUE;
98 break;
99 case JPEG_MARKER_SOF_2:
100 /* progressive DCT */
101 max_quant_table = 3;
102 if (dec->depth != 8 && dec->depth != 12) {
103 COG_ERROR("depth must be 8 or 12 (%d)", dec->depth);
104 dec->error = TRUE;
106 break;
107 case JPEG_MARKER_SOF_3:
108 /* lossless DCT */
109 max_quant_table = 0;
110 if (dec->depth < 2 || dec->depth > 16) {
111 COG_ERROR("depth must be between 2 and 16 (%d)", dec->depth);
112 dec->error = TRUE;
114 break;
115 default:
116 break;
119 if (dec->n_components < 0 || dec->n_components > 255) {
120 COG_ERROR("n_components must be in the range 0-255 (%d)",
121 dec->n_components);
122 dec->error = TRUE;
124 if (dec->sof_type == JPEG_MARKER_SOF_2 && dec->n_components > 4) {
125 COG_ERROR("n_components must be <= 4 for progressive DCT (%d)",
126 dec->n_components);
127 dec->error = TRUE;
130 for (i = 0; i < dec->n_components; i++) {
131 if (dec->components[i].id < 0 || dec->components[i].id > 255) {
132 COG_ERROR("component ID out of range");
133 dec->error = TRUE;
134 break;
136 if (dec->components[i].h_sample < 1 || dec->components[i].h_sample > 4 ||
137 dec->components[i].v_sample < 1 || dec->components[i].v_sample > 4) {
138 COG_ERROR("sample factor(s) for component %d out of range %d %d",
139 i, dec->components[i].h_sample, dec->components[i].v_sample);
140 dec->error = TRUE;
141 break;
143 if (dec->components[i].quant_table < 0 ||
144 dec->components[i].quant_table > max_quant_table) {
145 COG_ERROR("quant table for component %d out of range (%d)",
146 i, dec->components[i].quant_table);
147 dec->error = TRUE;
148 break;
152 if ((1 << 30) / dec->width / dec->height < dec->n_components) {
153 COG_ERROR ("image is too big (width %d, height %d, %d components)",
154 dec->width, dec->height, dec->n_components);
155 dec->error = TRUE;
159 static void
160 jpeg_decoder_init_decoder (JpegDecoder *dec)
162 int max_h_sample = 0;
163 int max_v_sample = 0;
164 int i;
166 /* decoder limitations */
167 if (dec->n_components != 3) {
168 jpeg_decoder_error(dec, "wrong number of components %d", dec->n_components);
169 return;
171 if (dec->sof_type != JPEG_MARKER_SOF_0) {
172 jpeg_decoder_error(dec, "only handle baseline DCT");
173 return;
176 for (i=0; i < dec->n_components; i++) {
177 max_h_sample = MAX (max_h_sample, dec->components[i].h_sample);
178 max_v_sample = MAX (max_v_sample, dec->components[i].v_sample);
181 dec->width_blocks =
182 (dec->width + 8 * max_h_sample - 1) / (8 * max_h_sample);
183 dec->height_blocks =
184 (dec->height + 8 * max_v_sample - 1) / (8 * max_v_sample);
185 for (i = 0; i < dec->n_components; i++) {
186 int rowstride;
187 int image_size;
189 dec->components[i].h_subsample = max_h_sample /
190 dec->components[i].h_sample;
191 dec->components[i].v_subsample = max_v_sample /
192 dec->components[i].v_sample;
194 rowstride = dec->width_blocks * 8 * max_h_sample /
195 dec->components[i].h_subsample;
196 image_size = rowstride *
197 (dec->height_blocks * 8 * max_v_sample /
198 dec->components[i].v_subsample);
199 dec->components[i].rowstride = rowstride;
200 dec->components[i].image = malloc (image_size);
204 #if 0
205 static char *
206 sprintbits (char *str, unsigned int bits, int n)
208 int i;
209 int bit = 1 << (n - 1);
211 for (i = 0; i < n; i++) {
212 str[i] = (bits & bit) ? '1' : '0';
213 bit >>= 1;
215 str[i] = 0;
217 return str;
220 static void
221 generate_code_table (int *huffsize)
223 int code;
224 int i;
225 int j;
226 int k;
227 char str[33];
229 //int l;
231 code = 0;
232 k = 0;
233 for (i = 0; i < 16; i++) {
234 for (j = 0; j < huffsize[i]; j++) {
235 COG_DEBUG ("huffcode[%d] = %s", k,
236 sprintbits (str, code >> (15 - i), i + 1));
237 code++;
238 k++;
240 code <<= 1;
244 #endif
246 static int
247 huffman_table_init_jpeg (JpegDecoder *decoder, HuffmanTable *table, JpegBits * bits)
249 int n_symbols;
250 int huffsize[16];
251 int i, j, k;
252 unsigned int symbol;
253 int n = 0;
255 huffman_table_init (table);
257 /* huffsize[i] is the number of symbols that have length
258 * (i+1) bits. Maximum bit length is 16 bits, so there are
259 * 16 entries. */
260 n_symbols = 0;
261 for (i = 0; i < 16; i++) {
262 huffsize[i] = jpeg_bits_get_u8 (bits);
263 n++;
264 n_symbols += huffsize[i];
267 /* Build up the symbol table. The first symbol is all 0's, with
268 * the number of bits determined by the first non-zero entry in
269 * huffsize[]. Subsequent symbols with the same bit length are
270 * incremented by 1. Increasing the bit length shifts the
271 * symbol 1 bit to the left. */
272 symbol = 0;
273 k = 0;
274 for (i = 0; i < 16; i++) {
275 for (j = 0; j < huffsize[i]; j++) {
276 huffman_table_add (table, symbol, i + 1, jpeg_bits_get_u8 (bits));
277 n++;
278 symbol++;
279 k++;
281 /* This checks that our symbol is actually less than the
282 * number of bits we think it is. This is only triggered
283 * for bad huffsize[] arrays. */
284 if (symbol >= (1U << (i + 1))) {
285 jpeg_decoder_error (decoder, "bad huffsize[] array");
286 return -1;
289 symbol <<= 1;
292 huffman_table_dump (table);
294 return n;
297 static int
298 jpeg_decoder_find_component_by_id (JpegDecoder * dec, int id)
300 int i;
302 for (i = 0; i < dec->n_components; i++) {
303 if (dec->components[i].id == id)
304 return i;
306 COG_DEBUG ("undefined component id %d", id);
307 return 0;
311 jpeg_decoder_application0 (JpegDecoder * dec, JpegBits * bits)
313 int length;
315 COG_DEBUG ("app0");
317 length = get_be_u16 (bits);
318 COG_DEBUG ("length=%d", length);
320 if (memcmp (bits->ptr, "JFIF", 4) == 0 && bits->ptr[4] == 0) {
321 int version;
322 int units;
323 int x_density;
324 int y_density;
325 int x_thumbnail;
326 int y_thumbnail;
328 COG_DEBUG ("JFIF");
329 bits->ptr += 5;
331 version = get_be_u16 (bits);
332 units = get_u8 (bits);
333 x_density = get_be_u16 (bits);
334 y_density = get_be_u16 (bits);
335 x_thumbnail = get_u8 (bits);
336 y_thumbnail = get_u8 (bits);
338 COG_DEBUG ("version = %04x", version);
339 COG_DEBUG ("units = %d", units);
340 COG_DEBUG ("x_density = %d", x_density);
341 COG_DEBUG ("y_density = %d", y_density);
342 COG_DEBUG ("x_thumbnail = %d", x_thumbnail);
343 COG_DEBUG ("y_thumbnail = %d", y_thumbnail);
347 if (memcmp (bits->ptr, "JFXX", 4) == 0 && bits->ptr[4] == 0) {
348 COG_DEBUG ("JFIF extension (not handled)");
349 bits->ptr += length - 2;
352 return length;
356 jpeg_decoder_application_misc (JpegDecoder * dec, JpegBits * bits)
358 int length;
360 COG_DEBUG ("appX");
362 length = get_be_u16 (bits);
363 COG_DEBUG ("length=%d", length);
365 COG_DEBUG ("JPEG application tag X ignored");
366 //dumpbits (bits);
368 bits->ptr += length - 2;
370 return length;
374 jpeg_decoder_comment (JpegDecoder * dec, JpegBits * bits)
376 int length;
378 COG_DEBUG ("comment");
380 length = get_be_u16 (bits);
381 COG_DEBUG ("length=%d", length);
383 //dumpbits (bits);
385 bits->ptr += length - 2;
387 return length;
391 jpeg_decoder_restart_interval (JpegDecoder * dec, JpegBits * bits)
393 int length;
395 COG_DEBUG ("comment");
397 length = get_be_u16 (bits);
398 COG_DEBUG ("length=%d", length);
400 dec->restart_interval = get_be_u16 (bits);
401 COG_DEBUG ("restart_interval=%d", dec->restart_interval);
403 return length;
407 jpeg_decoder_restart (JpegDecoder * dec, JpegBits * bits)
409 COG_DEBUG ("restart");
411 return 0;
414 void
415 jpeg_decoder_decode_entropy_segment (JpegDecoder * dec)
417 JpegBits * bits = &dec->bits;
418 JpegBits b2, *bits2 = &b2;
419 short block[64];
420 short block2[64];
421 unsigned char *newptr;
422 int len;
423 int maxlen;
424 int j;
425 int i;
426 int go;
427 int x, y;
428 int n;
429 int ret;
431 len = 0;
432 maxlen = jpeg_bits_available (bits) - 1;
433 j = 0;
434 while (len < maxlen) {
435 if (bits->ptr[len] == 0xff && bits->ptr[len + 1] != 0x00) {
436 break;
438 len++;
440 COG_DEBUG ("entropy length = %d", len);
442 /* we allocate extra space, since the getbits() code can
443 * potentially read past the end of the buffer */
444 newptr = malloc (len + 2);
445 for (i = 0; i < len; i++) {
446 newptr[j] = bits->ptr[i];
447 j++;
448 if (bits->ptr[i] == 0xff)
449 i++;
451 bits->ptr += len;
453 bits2->ptr = newptr;
454 bits2->idx = 0;
455 bits2->end = newptr + j;
456 newptr[j] = 0;
457 newptr[j + 1] = 0;
459 dec->dc[0] = dec->dc[1] = dec->dc[2] = dec->dc[3] = 128 * 8;
460 go = 1;
461 x = dec->x;
462 y = dec->y;
463 n = dec->restart_interval;
464 if (n == 0) n = (1<<26); /* max number of blocks */
465 while (go && n-- > 0) {
466 for (i = 0; i < dec->scan_list_length; i++) {
467 int dc_table_index;
468 int ac_table_index;
469 int quant_index;
470 unsigned char *ptr;
471 int component_index;
473 COG_DEBUG ("%d,%d: component=%d dc_table=%d ac_table=%d",
474 x, y,
475 dec->scan_list[i].component_index,
476 dec->scan_list[i].dc_table, dec->scan_list[i].ac_table);
478 component_index = dec->scan_list[i].component_index;
479 dc_table_index = dec->scan_list[i].dc_table;
480 ac_table_index = dec->scan_list[i].ac_table;
481 quant_index = dec->scan_list[i].quant_table;
483 ret = huffman_table_decode_macroblock (dec, block,
484 &dec->dc_huff_table[dc_table_index],
485 &dec->ac_huff_table[ac_table_index], bits2);
486 if (ret < 0) {
487 COG_DEBUG ("%d,%d: component=%d dc_table=%d ac_table=%d",
488 x, y,
489 dec->scan_list[i].component_index,
490 dec->scan_list[i].dc_table, dec->scan_list[i].ac_table);
491 n = 0;
492 break;
495 COG_DEBUG ("using quant table %d", quant_index);
496 oil_mult8x8_s16 (block2, block, dec->quant_tables[quant_index].quantizer,
497 sizeof (short) * 8, sizeof(short) * 8, sizeof (short) * 8);
498 dec->dc[component_index] += block2[0];
499 block2[0] = dec->dc[component_index];
500 oil_unzigzag8x8_s16 (block, sizeof (short) * 8, block2,
501 sizeof (short) * 8);
502 oil_idct8x8_s16 (block2, sizeof (short) * 8, block, sizeof (short) * 8);
503 oil_trans8x8_s16 (block, sizeof (short) * 8, block2, sizeof (short) * 8);
505 ptr = dec->components[component_index].image +
506 x * dec->components[component_index].h_sample +
507 dec->scan_list[i].offset +
508 dec->components[component_index].rowstride * y *
509 dec->components[component_index].v_sample;
511 oil_clipconv8x8_u8_s16 (ptr,
512 dec->components[component_index].rowstride,
513 block, sizeof (short) * 8);
515 x += 8;
516 if (x * dec->scan_h_subsample >= dec->width) {
517 x = 0;
518 y += 8;
520 if (y * dec->scan_v_subsample >= dec->height) {
521 go = 0;
524 dec->x = x;
525 dec->y = y;
526 free (newptr);
531 JpegDecoder *
532 jpeg_decoder_new (void)
534 JpegDecoder *dec;
536 oil_init ();
538 dec = malloc (sizeof(JpegDecoder));
539 memset (dec, 0, sizeof(JpegDecoder));
541 jpeg_load_standard_huffman_tables (dec);
543 return dec;
546 void
547 jpeg_decoder_free (JpegDecoder * dec)
549 int i;
551 for (i = 0; i < JPEG_LIMIT_COMPONENTS; i++) {
552 if (dec->components[i].image)
553 free (dec->components[i].image);
556 if (dec->data)
557 free (dec->data);
559 free (dec);
562 static int
563 jpeg_decoder_get_marker (JpegDecoder *dec, int *marker)
565 int a,b;
566 JpegBits *bits = &dec->bits;
568 if (jpeg_bits_available(bits) < 2) {
569 return FALSE;
572 a = jpeg_bits_get_u8(bits);
573 if (a != 0xff) {
574 jpeg_decoder_error(dec, "expected marker, not 0x%02x", a);
575 return FALSE;
578 do {
579 b = jpeg_bits_get_u8 (bits);
580 } while (b == 0xff && jpeg_bits_error(bits));
582 *marker = b;
583 return TRUE;
586 static void
587 jpeg_decoder_skip (JpegDecoder *dec)
589 int length;
591 length = jpeg_bits_get_u16_be (&dec->bits);
592 jpeg_bits_skip (&dec->bits, length - 2);
596 jpeg_decoder_decode (JpegDecoder *dec)
598 JpegBits *bits;
599 int marker;
601 dec->error = FALSE;
603 bits = &dec->bits;
605 /* Note: The spec is ambiguous as to whether fill bytes can come
606 * before the first marker. We'll assume yes. */
607 if (!jpeg_decoder_get_marker (dec, &marker)) {
608 return FALSE;
610 if (dec->strict && marker != JPEG_MARKER_SOI) {
611 jpeg_decoder_error(dec, "not a JPEG image");
612 return FALSE;
615 /* Interpret markers up to the start of frame */
616 while (!dec->error) {
617 if (!jpeg_decoder_get_marker (dec, &marker)) {
618 return FALSE;
621 if (marker == JPEG_MARKER_DEFINE_HUFFMAN_TABLES) {
622 jpeg_decoder_define_huffman_tables (dec);
623 } else if (marker == JPEG_MARKER_DEFINE_ARITHMETIC_CONDITIONING) {
624 jpeg_decoder_define_arithmetic_conditioning (dec);
625 } else if (marker == JPEG_MARKER_DEFINE_QUANTIZATION_TABLES) {
626 jpeg_decoder_define_quantization_tables (dec);
627 } else if (marker == JPEG_MARKER_DEFINE_RESTART_INTERVAL) {
628 jpeg_decoder_define_restart_interval (dec);
629 } else if (JPEG_MARKER_IS_APP(marker)) {
630 /* FIXME decode app segment */
631 jpeg_decoder_skip (dec);
632 } else if (marker == JPEG_MARKER_COMMENT) {
633 jpeg_decoder_skip (dec);
634 } else if (JPEG_MARKER_IS_START_OF_FRAME(marker)) {
635 break;
636 } else if (marker == JPEG_MARKER_SOI) {
637 if (dec->strict) {
638 jpeg_decoder_error (dec, "unexpected SOI");
639 return FALSE;
641 } else if (marker == JPEG_MARKER_EOI) {
642 if (dec->strict) {
643 jpeg_decoder_error (dec, "unexpected EOI");
644 return FALSE;
646 } else {
647 jpeg_decoder_error(dec, "unexpected marker 0x%02x", marker);
648 return FALSE;
651 if (dec->error) {
652 return FALSE;
655 jpeg_decoder_start_of_frame(dec, marker);
657 jpeg_decoder_verify_header (dec);
658 if (dec->error) {
659 return FALSE;
662 jpeg_decoder_init_decoder (dec);
663 if (dec->error) {
664 return FALSE;
667 /* In this section, we loop over parse units until we reach the end
668 * of the image. */
669 while (!dec->error) {
670 if (!jpeg_decoder_get_marker (dec, &marker)) {
671 return FALSE;
674 if (marker == JPEG_MARKER_DEFINE_HUFFMAN_TABLES) {
675 jpeg_decoder_define_huffman_tables (dec);
676 } else if (marker == JPEG_MARKER_DEFINE_ARITHMETIC_CONDITIONING) {
677 jpeg_decoder_define_arithmetic_conditioning (dec);
678 } else if (marker == JPEG_MARKER_DEFINE_QUANTIZATION_TABLES) {
679 jpeg_decoder_define_quantization_tables (dec);
680 } else if (marker == JPEG_MARKER_DEFINE_RESTART_INTERVAL) {
681 jpeg_decoder_define_restart_interval (dec);
682 } else if (JPEG_MARKER_IS_APP(marker)) {
683 jpeg_decoder_skip (dec);
684 } else if (marker == JPEG_MARKER_COMMENT) {
685 jpeg_decoder_skip (dec);
686 } else if (marker == JPEG_MARKER_SOS) {
687 jpeg_decoder_start_of_scan (dec);
688 if (dec->error) {
689 return FALSE;
691 jpeg_decoder_decode_entropy_segment (dec);
692 } else if (JPEG_MARKER_IS_RESET(marker)) {
693 jpeg_decoder_decode_entropy_segment (dec);
694 } else if (marker == JPEG_MARKER_SOI) {
695 if (dec->strict) {
696 jpeg_decoder_error (dec, "unexpected SOI");
697 return FALSE;
699 } else if (marker == JPEG_MARKER_EOI) {
700 if (dec->strict) {
701 jpeg_decoder_error (dec, "unexpected EOI");
702 return FALSE;
703 } else {
704 break;
706 } else {
707 jpeg_decoder_error(dec, "unexpected marker 0x%02x", marker);
708 return FALSE;
711 if (dec->error) {
712 return FALSE;
715 return TRUE;
718 /* handle markers */
720 void
721 jpeg_decoder_define_huffman_tables (JpegDecoder * dec)
723 JpegBits *bits = &dec->bits;
724 int length;
725 int tc;
726 int th;
727 int x;
728 HuffmanTable *hufftab;
730 COG_DEBUG ("define huffman tables");
732 length = jpeg_bits_get_u16_be (bits);
733 if (length < 2) {
734 jpeg_decoder_error(dec, "length too short");
735 return;
737 length -= 2;
739 while (length > 0) {
740 x = jpeg_bits_get_u8 (bits);
741 length--;
743 tc = x >> 4;
744 th = x & 0xf;
746 COG_DEBUG ("huff table type %d (%s) idx %d", tc, tc ? "ac" : "dc", th);
747 if (tc > 1 || th > 3) {
748 jpeg_decoder_error(dec, "huffman table type or idx out of range");
749 return;
752 if (tc) {
753 hufftab = &dec->ac_huff_table[th];
754 length -= huffman_table_init_jpeg (dec, hufftab, bits);
755 } else {
756 hufftab = &dec->dc_huff_table[th];
757 length -= huffman_table_init_jpeg (dec, hufftab, bits);
759 if (dec->error) {
760 return;
763 if (length < 0) {
764 jpeg_decoder_error(dec, "huffman table overran available bytes");
765 return;
769 void
770 jpeg_decoder_define_quantization_tables (JpegDecoder *dec)
772 JpegBits *bits = &dec->bits;
773 JpegQuantTable *table;
774 int length;
775 int pq;
776 int tq;
777 int i;
779 COG_INFO ("define quantization table");
781 length = jpeg_bits_get_u16_be (bits);
782 if (length < 2) {
783 jpeg_decoder_error(dec, "length too short");
784 return;
786 length -= 2;
788 while (length > 0) {
789 int x;
791 x = jpeg_bits_get_u8 (bits);
792 length--;
793 pq = x >> 4;
794 tq = x & 0xf;
796 if (pq > 1) {
797 jpeg_decoder_error (dec, "bad pq value");
798 return;
800 if (tq > 3) {
801 jpeg_decoder_error (dec, "bad tq value");
802 return;
805 table = &dec->quant_tables[tq];
806 if (pq) {
807 for (i = 0; i < 64; i++) {
808 table->quantizer[i] = jpeg_bits_get_u16_be (bits);
809 length -= 2;
811 } else {
812 for (i = 0; i < 64; i++) {
813 table->quantizer[i] = jpeg_bits_get_u8 (bits);
814 length -= 1;
818 if (length < 0) {
819 jpeg_decoder_error(dec, "quantization table overran available bytes");
820 return;
824 void
825 jpeg_decoder_define_restart_interval (JpegDecoder *dec)
827 JpegBits *bits = &dec->bits;
828 int length;
830 length = jpeg_bits_get_u16_be (bits);
831 if (length != 4) {
832 jpeg_decoder_error(dec, "length supposed to be 4 (%d)", length);
833 return;
836 /* FIXME this needs to be checked somewhere */
837 dec->restart_interval = jpeg_bits_get_u16_be (bits);
840 void
841 jpeg_decoder_define_arithmetic_conditioning (JpegDecoder *dec)
843 /* we don't handle arithmetic coding, so skip it */
844 jpeg_decoder_skip (dec);
847 void
848 jpeg_decoder_start_of_frame (JpegDecoder * dec, int marker)
850 JpegBits *bits = &dec->bits;
851 int i;
852 int length;
854 COG_INFO ("start of frame");
856 dec->sof_type = marker;
858 length = jpeg_bits_get_u16_be (bits);
860 if (jpeg_bits_available(bits) < length) {
861 jpeg_decoder_error(dec, "not enough data for start_of_frame (%d < %d)",
862 length, jpeg_bits_available(bits));
863 return;
866 dec->depth = jpeg_bits_get_u8 (bits);
867 dec->height = jpeg_bits_get_u16_be (bits);
868 dec->width = jpeg_bits_get_u16_be (bits);
869 dec->n_components = jpeg_bits_get_u8 (bits);
871 COG_DEBUG (
872 "frame_length=%d depth=%d height=%d width=%d n_components=%d", length,
873 dec->depth, dec->height, dec->width, dec->n_components);
875 if (dec->n_components * 3 + 8 != length) {
876 jpeg_decoder_error(dec, "inconsistent header");
877 return;
880 for (i = 0; i < dec->n_components; i++) {
881 dec->components[i].id = get_u8 (bits);
882 dec->components[i].h_sample = getbits (bits, 4);
883 dec->components[i].v_sample = getbits (bits, 4);
884 dec->components[i].quant_table = get_u8 (bits);
886 COG_DEBUG (
887 "[%d] id=%d h_sample=%d v_sample=%d quant_table=%d", i,
888 dec->components[i].id, dec->components[i].h_sample,
889 dec->components[i].v_sample, dec->components[i].quant_table);
893 void
894 jpeg_decoder_start_of_scan (JpegDecoder * dec)
896 JpegBits *bits = &dec->bits;
897 int length;
898 int i;
899 int spectral_start;
900 int spectral_end;
901 int approx_high;
902 int approx_low;
903 int n;
904 int tmp;
905 int n_components;
907 COG_DEBUG ("start of scan");
909 length = jpeg_bits_get_u16_be (bits);
910 COG_DEBUG ("length=%d", length);
912 n_components = jpeg_bits_get_u8 (bits);
913 n = 0;
914 dec->scan_h_subsample = 0;
915 dec->scan_v_subsample = 0;
916 for (i = 0; i < n_components; i++) {
917 int component_id;
918 int dc_table;
919 int ac_table;
920 int x;
921 int y;
922 int idx;
923 int h_subsample;
924 int v_subsample;
925 int quant_index;
927 component_id = jpeg_bits_get_u8 (bits);
928 tmp = jpeg_bits_get_u8 (bits);
929 dc_table = tmp >> 4;
930 ac_table = tmp & 0xf;
931 idx = jpeg_decoder_find_component_by_id (dec, component_id);
933 h_subsample = dec->components[idx].h_sample;
934 v_subsample = dec->components[idx].v_sample;
935 quant_index = dec->components[idx].quant_table;
937 for (y = 0; y < v_subsample; y++) {
938 for (x = 0; x < h_subsample; x++) {
939 dec->scan_list[n].component_index = idx;
940 dec->scan_list[n].dc_table = dc_table;
941 dec->scan_list[n].ac_table = ac_table;
942 dec->scan_list[n].quant_table = quant_index;
943 dec->scan_list[n].x = x;
944 dec->scan_list[n].y = y;
945 dec->scan_list[n].offset =
946 y * 8 * dec->components[idx].rowstride + x * 8;
947 n++;
948 if (n > JPEG_LIMIT_SCAN_LIST_LENGTH) {
949 jpeg_decoder_error(dec, "scan list too long");
950 return;
955 dec->scan_h_subsample = MAX (dec->scan_h_subsample, h_subsample);
956 dec->scan_v_subsample = MAX (dec->scan_v_subsample, v_subsample);
958 COG_DEBUG ("component %d: idx=%d dc_table=%d ac_table=%d n=%d",
959 component_id, idx, dc_table, ac_table, n);
961 dec->scan_list_length = n;
964 spectral_start = jpeg_bits_get_u8 (bits);
965 spectral_end = jpeg_bits_get_u8 (bits);
966 COG_DEBUG ("spectral range [%d,%d]", spectral_start, spectral_end);
967 tmp = jpeg_bits_get_u8 (bits);
968 approx_high = tmp >> 4;
969 approx_low = tmp & 0xf;
970 COG_DEBUG ("approx range [%d,%d]", approx_low, approx_high);
972 dec->x = 0;
973 dec->y = 0;
974 dec->dc[0] = dec->dc[1] = dec->dc[2] = dec->dc[3] = 128 * 8;
989 jpeg_decoder_addbits (JpegDecoder * dec, unsigned char *data, unsigned int len)
991 unsigned int offset;
993 offset = dec->bits.ptr - dec->data;
995 dec->data = realloc (dec->data, dec->data_len + len);
996 memcpy (dec->data + dec->data_len, data, len);
997 dec->data_len += len;
999 dec->bits.ptr = dec->data + offset;
1000 dec->bits.end = dec->data + dec->data_len;
1002 return 0;
1006 jpeg_decoder_get_image_size (JpegDecoder * dec, unsigned int *width, unsigned int *height)
1008 if (width)
1009 *width = (unsigned) dec->width;
1010 if (height)
1011 *height = (unsigned) dec->height;
1013 return 0;
1017 jpeg_decoder_get_component_ptr (JpegDecoder * dec, int id,
1018 unsigned char **image, int *rowstride)
1020 int i;
1022 i = jpeg_decoder_find_component_by_id (dec, id);
1023 if (image)
1024 *image = dec->components[i].image;
1025 if (rowstride)
1026 *rowstride = dec->components[i].rowstride;
1028 return 0;
1032 jpeg_decoder_get_component_size (JpegDecoder * dec, int id,
1033 int *width, int *height)
1035 int i;
1037 /* subsampling sizes are rounded up */
1039 i = jpeg_decoder_find_component_by_id (dec, id);
1040 if (width)
1041 *width = (dec->width - 1) / dec->components[i].h_subsample + 1;
1042 if (height)
1043 *height = (dec->height - 1) / dec->components[i].v_subsample + 1;
1045 return 0;
1049 jpeg_decoder_get_component_subsampling (JpegDecoder * dec, int id,
1050 int *h_subsample, int *v_subsample)
1052 int i;
1054 i = jpeg_decoder_find_component_by_id (dec, id);
1055 if (h_subsample)
1056 *h_subsample = dec->components[i].h_subsample;
1057 if (v_subsample)
1058 *v_subsample = dec->components[i].v_subsample;
1060 return 0;
1063 #if 0
1065 jpeg_decoder_parse (JpegDecoder * dec)
1067 JpegBits *bits = &dec->bits;
1068 JpegBits b2;
1069 unsigned int x;
1070 unsigned int tag;
1071 int i;
1073 while (bits->ptr < bits->end) {
1074 x = get_u8 (bits);
1075 if (x != 0xff) {
1076 int n = 0;
1078 while (x != 0xff) {
1079 x = get_u8 (bits);
1080 n++;
1082 OIL_DEBUG ("lost sync, skipped %d bytes", n);
1084 while (x == 0xff) {
1085 x = get_u8 (bits);
1087 tag = x;
1088 OIL_DEBUG ("tag %02x", tag);
1090 b2 = *bits;
1092 for (i = 0; i < n_jpeg_markers - 1; i++) {
1093 if (tag == jpeg_markers[i].tag) {
1094 break;
1097 OIL_DEBUG ("tag: %s", jpeg_markers[i].name);
1098 if (jpeg_markers[i].func) {
1099 jpeg_markers[i].func (dec, &b2);
1100 } else {
1101 OIL_DEBUG ("unhandled or illegal JPEG marker (0x%02x)", tag);
1102 dumpbits (&b2);
1104 if (jpeg_markers[i].flags & JPEG_ENTROPY_SEGMENT) {
1105 jpeg_decoder_decode_entropy_segment (dec, &b2);
1107 syncbits (&b2);
1108 bits->ptr = b2.ptr;
1111 return 0;
1113 #endif
1116 /* misc helper functins */
1118 static void
1119 jpeg_load_standard_huffman_tables (JpegDecoder * dec)
1121 JpegBits b, *bits = &b;
1123 bits->ptr = jpeg_standard_tables;
1124 bits->idx = 0;
1125 bits->end = jpeg_standard_tables + jpeg_standard_tables_size;
1127 huffman_table_init_jpeg (dec, &dec->dc_huff_table[0], bits);
1128 huffman_table_init_jpeg (dec, &dec->ac_huff_table[0], bits);
1129 huffman_table_init_jpeg (dec, &dec->dc_huff_table[1], bits);
1130 huffman_table_init_jpeg (dec, &dec->ac_huff_table[1], bits);