comparison mjpeg.c @ 349:34f6c77ff01a libavcodec

Support for external huffman table and various fixes by Alex Beregszaszi <alex@naxine.org>
author arpi_esp
date Wed, 01 May 2002 18:12:04 +0000
parents 9f6071a87e17
children 167aa21aa250
comparison
equal deleted inserted replaced
348:ae9e9af0b627 349:34f6c77ff01a
13 * GNU General Public License for more details. 13 * GNU General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU General Public License 15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software 16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * Support for external huffman table and various fixed by
20 * Alex Beregszaszi <alex@naxine.org>
18 */ 21 */
19 //#define DEBUG 22 //#define DEBUG
20 #include "avcodec.h" 23 #include "avcodec.h"
21 #include "dsputil.h" 24 #include "dsputil.h"
22 #include "mpegvideo.h" 25 #include "mpegvideo.h"
31 UINT16 huff_code_ac_luminance[256]; 34 UINT16 huff_code_ac_luminance[256];
32 UINT8 huff_size_ac_chrominance[256]; 35 UINT8 huff_size_ac_chrominance[256];
33 UINT16 huff_code_ac_chrominance[256]; 36 UINT16 huff_code_ac_chrominance[256];
34 } MJpegContext; 37 } MJpegContext;
35 38
36 #define SOF0 0xc0 39 /* JPEG marker codes */
37 #define SOI 0xd8 40 typedef enum {
38 #define EOI 0xd9 41 /* start of frame */
39 #define DQT 0xdb 42 SOF0 = 0xc0, /* baseline */
40 #define DHT 0xc4 43 SOF1 = 0xc1, /* extended sequential, huffman */
41 #define SOS 0xda 44 SOF2 = 0xc2, /* progressive, huffman */
45 SOF3 = 0xc3, /* lossless, huffman */
46
47 SOF5 = 0xc5, /* differential sequential, huffman */
48 SOF6 = 0xc6, /* differential progressive, huffman */
49 SOF7 = 0xc7, /* differential lossless, huffman */
50 JPG = 0xc8, /* reserved for JPEG extension */
51 SOF9 = 0xc9, /* extended sequential, arithmetic */
52 SOF10 = 0xca, /* progressive, arithmetic */
53 SOF11 = 0xcb, /* lossless, arithmetic */
54
55 SOF13 = 0xcd, /* differential sequential, arithmetic */
56 SOF14 = 0xce, /* differential progressive, arithmetic */
57 SOF15 = 0xcf, /* differential lossless, arithmetic */
58
59 DHT = 0xc4, /* define huffman tables */
60
61 DAC = 0xcc, /* define arithmetic-coding conditioning */
62
63 /* restart with modulo 8 count "m" */
64 RST0 = 0xd0,
65 RST1 = 0xd1,
66 RST2 = 0xd2,
67 RST3 = 0xd3,
68 RST4 = 0xd4,
69 RST5 = 0xd5,
70 RST6 = 0xd6,
71 RST7 = 0xd7,
72
73 SOI = 0xd8, /* start of image */
74 EOI = 0xd9, /* end of image */
75 SOS = 0xda, /* start of scan */
76 DQT = 0xdb, /* define quantization tables */
77 DNL = 0xdc, /* define number of lines */
78 DRI = 0xdd, /* define restart interval */
79 DHP = 0xde, /* define hierarchical progression */
80 EXP = 0xdf, /* expand reference components */
81
82 APP0 = 0xe0,
83 APP1 = 0xe1,
84 APP2 = 0xe2,
85 APP3 = 0xe3,
86 APP4 = 0xe4,
87 APP5 = 0xe5,
88 APP6 = 0xe6,
89 APP7 = 0xe7,
90 APP8 = 0xe8,
91 APP9 = 0xe9,
92 APP10 = 0xea,
93 APP11 = 0xeb,
94 APP12 = 0xec,
95 APP13 = 0xed,
96 APP14 = 0xee,
97 APP15 = 0xef,
98
99 JPG0 = 0xf0,
100 JPG1 = 0xf1,
101 JPG2 = 0xf2,
102 JPG3 = 0xf3,
103 JPG4 = 0xf4,
104 JPG5 = 0xf5,
105 JPG6 = 0xf6,
106 JPG7 = 0xf7,
107 JPG8 = 0xf8,
108 JPG9 = 0xf9,
109 JPG10 = 0xfa,
110 JPG11 = 0xfb,
111 JPG12 = 0xfc,
112 JPG13 = 0xfd,
113
114 COM = 0xfe, /* comment */
115
116 TEM = 0x01, /* temporary private use for arithmetic coding */
117
118 /* 0x02 -> 0xbf reserved */
119 } JPEG_MARKER;
42 120
43 #if 0 121 #if 0
44 /* These are the sample quantization tables given in JPEG spec section K.1. 122 /* These are the sample quantization tables given in JPEG spec section K.1.
45 * The spec says that the values given produce "good" quality, and 123 * The spec says that the values given produce "good" quality, and
46 * when divided by 2, "very good" quality. 124 * when divided by 2, "very good" quality.
485 563
486 build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12); 564 build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
487 build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12); 565 build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
488 build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251); 566 build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
489 build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251); 567 build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
568
569 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
570 {
571 printf("mjpeg: using external huffman table\n");
572 mjpeg_decode_dht(s, avctx->extradata, avctx->extradata_size);
573 /* should check for error - but dunno */
574 }
490 return 0; 575 return 0;
491 } 576 }
492 577
493 /* quantize tables */ 578 /* quantize tables */
494 static int mjpeg_decode_dqt(MJpegDecodeContext *s, 579 static int mjpeg_decode_dqt(MJpegDecodeContext *s,
501 len -= 2; 586 len -= 2;
502 587
503 while (len >= 65) { 588 while (len >= 65) {
504 /* only 8 bit precision handled */ 589 /* only 8 bit precision handled */
505 if (get_bits(&s->gb, 4) != 0) 590 if (get_bits(&s->gb, 4) != 0)
591 {
592 dprintf("dqt: 16bit precision\n");
506 return -1; 593 return -1;
594 }
507 index = get_bits(&s->gb, 4); 595 index = get_bits(&s->gb, 4);
508 if (index >= 4) 596 if (index >= 4)
509 return -1; 597 return -1;
510 dprintf("index=%d\n", index); 598 dprintf("index=%d\n", index);
511 /* read quant table */ 599 /* read quant table */
634 /* memory test is done in mjpeg_decode_sos() */ 722 /* memory test is done in mjpeg_decode_sos() */
635 s->current_picture[i] = av_mallocz(w * h); 723 s->current_picture[i] = av_mallocz(w * h);
636 } 724 }
637 s->first_picture = 0; 725 s->first_picture = 0;
638 } 726 }
727
728 if (len != 8+(3*nb_components))
729 dprintf("decode_sof0: error, len(%d) mismatch\n", len);
639 730
640 return 0; 731 return 0;
641 } 732 }
642 733
643 static inline int decode_dc(MJpegDecodeContext *s, int dc_index) 734 static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
644 { 735 {
645 VLC *dc_vlc;
646 int code, diff; 736 int code, diff;
647 737
648 dc_vlc = &s->vlcs[0][dc_index]; 738 code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);
649 code = get_vlc(&s->gb, dc_vlc);
650 if (code < 0) 739 if (code < 0)
740 {
741 dprintf("decode_dc: bad vlc: %d:%d\n", 0, dc_index);
651 return 0xffff; 742 return 0xffff;
743 }
652 if (code == 0) { 744 if (code == 0) {
653 diff = 0; 745 diff = 0;
746 // dprintf("decode_dc: bad code\n");
654 } else { 747 } else {
655 diff = get_bits(&s->gb, code); 748 diff = get_bits(&s->gb, code);
656 if ((diff & (1 << (code - 1))) == 0) 749 if ((diff & (1 << (code - 1))) == 0)
657 diff = (-1 << code) | (diff + 1); 750 diff = (-1 << code) | (diff + 1);
658 } 751 }
666 int nbits, code, i, j, level; 759 int nbits, code, i, j, level;
667 int run, val; 760 int run, val;
668 VLC *ac_vlc; 761 VLC *ac_vlc;
669 INT16 *quant_matrix; 762 INT16 *quant_matrix;
670 763
671 quant_matrix = s->quant_matrixes[quant_index];
672 /* DC coef */ 764 /* DC coef */
673 val = decode_dc(s, dc_index); 765 val = decode_dc(s, dc_index);
674 if (val == 0xffff) { 766 if (val == 0xffff) {
675 dprintf("error dc\n"); 767 dprintf("error dc\n");
676 return -1; 768 return -1;
677 } 769 }
770 quant_matrix = s->quant_matrixes[quant_index];
678 val = val * quant_matrix[0] + s->last_dc[component]; 771 val = val * quant_matrix[0] + s->last_dc[component];
679 s->last_dc[component] = val; 772 s->last_dc[component] = val;
680 block[0] = val; 773 block[0] = val;
681 /* AC coefs */ 774 /* AC coefs */
682 ac_vlc = &s->vlcs[1][ac_index]; 775 ac_vlc = &s->vlcs[1][ac_index];
729 /* XXX: verify len field validity */ 822 /* XXX: verify len field validity */
730 len = get_bits(&s->gb, 16); 823 len = get_bits(&s->gb, 16);
731 nb_components = get_bits(&s->gb, 8); 824 nb_components = get_bits(&s->gb, 8);
732 /* XXX: only interleaved scan accepted */ 825 /* XXX: only interleaved scan accepted */
733 if (nb_components != 3) 826 if (nb_components != 3)
827 {
828 dprintf("decode_sos: components(%d) mismatch\n", nb_components);
734 return -1; 829 return -1;
830 }
735 vmax = 0; 831 vmax = 0;
736 hmax = 0; 832 hmax = 0;
737 for(i=0;i<nb_components;i++) { 833 for(i=0;i<nb_components;i++) {
738 id = get_bits(&s->gb, 8) - 1; 834 id = get_bits(&s->gb, 8) - 1;
739 /* find component index */ 835 /* find component index */
740 for(index=0;index<s->nb_components;index++) 836 for(index=0;index<s->nb_components;index++)
741 if (id == s->component_id[index]) 837 if (id == s->component_id[index])
742 break; 838 break;
743 if (index == s->nb_components) 839 if (index == s->nb_components)
840 {
841 dprintf("decode_sos: index out of components\n");
744 return -1; 842 return -1;
843 }
745 844
746 comp_index[i] = index; 845 comp_index[i] = index;
747 nb_blocks[i] = s->h_count[index] * s->v_count[index]; 846 nb_blocks[i] = s->h_count[index] * s->v_count[index];
748 h_count[i] = s->h_count[index]; 847 h_count[i] = s->h_count[index];
749 v_count[i] = s->v_count[index]; 848 v_count[i] = s->v_count[index];
750 849
751 dc_index[i] = get_bits(&s->gb, 4); 850 dc_index[i] = get_bits(&s->gb, 4);
752 if (dc_index[i] >= 4)
753 return -1;
754 ac_index[i] = get_bits(&s->gb, 4); 851 ac_index[i] = get_bits(&s->gb, 4);
755 if (ac_index[i] >= 4) 852
756 return -1; 853 if (dc_index[i] < 0 || ac_index[i] < 0 ||
854 dc_index[i] >= 4 || ac_index[i] >= 4)
855 goto out_of_range;
856 switch(s->start_code)
857 {
858 case SOF0:
859 if (dc_index[i] > 1 || ac_index[i] > 1)
860 goto out_of_range;
861 break;
862 case SOF1:
863 case SOF2:
864 if (dc_index[i] > 3 || ac_index[i] > 3)
865 goto out_of_range;
866 break;
867 case SOF3:
868 if (dc_index[i] > 3 || ac_index[i] != 0)
869 goto out_of_range;
870 break;
871 }
757 } 872 }
758 get_bits(&s->gb, 8); /* Ss */ 873 get_bits(&s->gb, 8); /* Ss */
759 get_bits(&s->gb, 8); /* Se */ 874 get_bits(&s->gb, 8); /* Se */
760 get_bits(&s->gb, 8); /* not used */ 875 get_bits(&s->gb, 8); /* Ah and Al (each are 4 bits) */
761 876
762 for(i=0;i<nb_components;i++) 877 for(i=0;i<nb_components;i++)
763 s->last_dc[i] = 1024; 878 s->last_dc[i] = 1024;
764 879
765 if (nb_components > 1) { 880 if (nb_components > 1) {
794 s->quant_index[c]) < 0) { 909 s->quant_index[c]) < 0) {
795 dprintf("error %d %d\n", mb_y, mb_x); 910 dprintf("error %d %d\n", mb_y, mb_x);
796 ret = -1; 911 ret = -1;
797 goto the_end; 912 goto the_end;
798 } 913 }
914 // dprintf("mb: %d %d processed\n", mb_y, mb_x);
799 ff_idct (s->block); 915 ff_idct (s->block);
800 ptr = s->current_picture[c] + 916 ptr = s->current_picture[c] +
801 (s->linesize[c] * (v * mb_y + y) * 8) + 917 (s->linesize[c] * (v * mb_y + y) * 8) +
802 (h * mb_x + x) * 8; 918 (h * mb_x + x) * 8;
803 if (s->interlaced && s->bottom_field) 919 if (s->interlaced && s->bottom_field)
813 } 929 }
814 ret = 0; 930 ret = 0;
815 the_end: 931 the_end:
816 emms_c(); 932 emms_c();
817 return ret; 933 return ret;
934 out_of_range:
935 dprintf("decode_sos: ac/dc index out of range\n");
936 return -1;
818 } 937 }
819 938
820 /* return the 8 bit start code value and update the search 939 /* return the 8 bit start code value and update the search
821 state. Return -1 if no start code found */ 940 state. Return -1 if no start code found */
822 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end, 941 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
856 void *data, int *data_size, 975 void *data, int *data_size,
857 UINT8 *buf, int buf_size) 976 UINT8 *buf, int buf_size)
858 { 977 {
859 MJpegDecodeContext *s = avctx->priv_data; 978 MJpegDecodeContext *s = avctx->priv_data;
860 UINT8 *buf_end, *buf_ptr, *buf_start; 979 UINT8 *buf_end, *buf_ptr, *buf_start;
861 int len, code, start_code, input_size, i; 980 int len, code, input_size, i;
862 AVPicture *picture = data; 981 AVPicture *picture = data;
982 unsigned int start_code;
863 983
864 *data_size = 0; 984 *data_size = 0;
865 985
866 /* no supplementary picture */ 986 /* no supplementary picture */
867 if (buf_size == 0) 987 if (buf_size == 0)
882 s->start_code = code; 1002 s->start_code = code;
883 } else { 1003 } else {
884 memcpy(s->buf_ptr, buf_start, len); 1004 memcpy(s->buf_ptr, buf_start, len);
885 s->buf_ptr += len; 1005 s->buf_ptr += len;
886 /* if we got FF 00, we copy FF to the stream to unescape FF 00 */ 1006 /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
887 if (code == 0) { 1007 /* valid marker code is between 00 and ff - alex */
1008 if (code == 0 || code == 0xff) {
888 s->buf_ptr--; 1009 s->buf_ptr--;
889 } else if (code > 0) { 1010 } else if (code > 0) {
890 /* prepare data for next start code */ 1011 /* prepare data for next start code */
891 input_size = s->buf_ptr - s->buffer; 1012 input_size = s->buf_ptr - s->buffer;
892 start_code = s->start_code; 1013 start_code = s->start_code;