comparison mjpeg.c @ 354:167aa21aa250 libavcodec

patch by Alex Beregszaszi <alex@naxine.org> - AVID (AVRn) support (workaround) - print error instead of failing for unsupported SOF - fixed the 0<code<FF range checking
author arpi_esp
date Fri, 03 May 2002 16:34:40 +0000
parents 34f6c77ff01a
children ae179ec5fb47
comparison
equal deleted inserted replaced
353:386f430e93f4 354:167aa21aa250
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 * 18 *
19 * Support for external huffman table and various fixed by 19 * Support for external huffman table and various fixes (AVID workaround) by
20 * Alex Beregszaszi <alex@naxine.org> 20 * Alex Beregszaszi <alex@naxine.org>
21 */ 21 */
22 //#define DEBUG 22 //#define DEBUG
23 #include "avcodec.h" 23 #include "avcodec.h"
24 #include "dsputil.h" 24 #include "dsputil.h"
25 #include "mpegvideo.h" 25 #include "mpegvideo.h"
26
27 #ifdef USE_FASTMEMCPY
28 #include "fastmemcpy.h"
29 #endif
26 30
27 typedef struct MJpegContext { 31 typedef struct MJpegContext {
28 UINT8 huff_size_dc_luminance[12]; 32 UINT8 huff_size_dc_luminance[12];
29 UINT16 huff_code_dc_luminance[12]; 33 UINT16 huff_code_dc_luminance[12];
30 UINT8 huff_size_dc_chrominance[12]; 34 UINT8 huff_size_dc_chrominance[12];
531 int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */ 535 int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
532 UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */ 536 UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
533 int linesize[MAX_COMPONENTS]; 537 int linesize[MAX_COMPONENTS];
534 DCTELEM block[64] __align8; 538 DCTELEM block[64] __align8;
535 UINT8 buffer[PICTURE_BUFFER_SIZE]; 539 UINT8 buffer[PICTURE_BUFFER_SIZE];
540
541 int buggy_avid;
536 } MJpegDecodeContext; 542 } MJpegDecodeContext;
537 543
538 static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table, 544 static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
539 int nb_codes) 545 int nb_codes)
540 { 546 {
723 s->current_picture[i] = av_mallocz(w * h); 729 s->current_picture[i] = av_mallocz(w * h);
724 } 730 }
725 s->first_picture = 0; 731 s->first_picture = 0;
726 } 732 }
727 733
728 if (len != 8+(3*nb_components)) 734 if (len != (8+(3*nb_components)))
735 {
729 dprintf("decode_sof0: error, len(%d) mismatch\n", len); 736 dprintf("decode_sof0: error, len(%d) mismatch\n", len);
737 }
730 738
731 return 0; 739 return 0;
732 } 740 }
733 741
734 static inline int decode_dc(MJpegDecodeContext *s, int dc_index) 742 static inline int decode_dc(MJpegDecodeContext *s, int dc_index)
934 out_of_range: 942 out_of_range:
935 dprintf("decode_sos: ac/dc index out of range\n"); 943 dprintf("decode_sos: ac/dc index out of range\n");
936 return -1; 944 return -1;
937 } 945 }
938 946
947 #define FOURCC(a,b,c,d) ((a << 24) | (b << 16) | (c << 8) | d)
948 static int mjpeg_decode_app(MJpegDecodeContext *s,
949 UINT8 *buf, int buf_size)
950 {
951 int len, id;
952
953 init_get_bits(&s->gb, buf, buf_size);
954
955 /* XXX: verify len field validity */
956 len = get_bits(&s->gb, 16)-2;
957 if (len < 5)
958 return -1;
959 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
960 if (get_bits(&s->gb, 8) != 0)
961 return -1;
962
963 /* buggy avid, it puts EOI only at every 10th frame */
964 if (id == FOURCC('A','V','I','1'))
965 {
966 s->buggy_avid = 1;
967 if (s->first_picture)
968 printf("mjpeg: workarounding buggy AVID\n");
969 }
970
971 /* if id == JFIF, we can extract some infos (aspect ratio), others
972 are useless */
973
974 /* should check for further values.. */
975
976 return 0;
977 }
978 #undef FOURCC
979
939 /* return the 8 bit start code value and update the search 980 /* return the 8 bit start code value and update the search
940 state. Return -1 if no start code found */ 981 state. Return -1 if no start code found */
941 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end, 982 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end,
942 UINT32 *header_state) 983 UINT32 *header_state)
943 { 984 {
1003 } else { 1044 } else {
1004 memcpy(s->buf_ptr, buf_start, len); 1045 memcpy(s->buf_ptr, buf_start, len);
1005 s->buf_ptr += len; 1046 s->buf_ptr += len;
1006 /* if we got FF 00, we copy FF to the stream to unescape FF 00 */ 1047 /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
1007 /* valid marker code is between 00 and ff - alex */ 1048 /* valid marker code is between 00 and ff - alex */
1008 if (code == 0 || code == 0xff) { 1049 if (code <= 0 || code >= 0xff) {
1009 s->buf_ptr--; 1050 s->buf_ptr--;
1010 } else if (code > 0) { 1051 } else {
1011 /* prepare data for next start code */ 1052 /* prepare data for next start code */
1012 input_size = s->buf_ptr - s->buffer; 1053 input_size = s->buf_ptr - s->buffer;
1013 start_code = s->start_code; 1054 start_code = s->start_code;
1014 s->buf_ptr = s->buffer; 1055 s->buf_ptr = s->buffer;
1015 s->start_code = code; 1056 s->start_code = code;
1027 case SOF0: 1068 case SOF0:
1028 mjpeg_decode_sof0(s, s->buffer, input_size); 1069 mjpeg_decode_sof0(s, s->buffer, input_size);
1029 break; 1070 break;
1030 case SOS: 1071 case SOS:
1031 mjpeg_decode_sos(s, s->buffer, input_size); 1072 mjpeg_decode_sos(s, s->buffer, input_size);
1032 if (s->start_code == EOI) { 1073 if (s->start_code == EOI || s->buggy_avid) {
1033 int l; 1074 int l;
1034 if (s->interlaced) { 1075 if (s->interlaced) {
1035 s->bottom_field ^= 1; 1076 s->bottom_field ^= 1;
1036 /* if not bottom field, do not output image yet */ 1077 /* if not bottom field, do not output image yet */
1037 if (s->bottom_field) 1078 if (s->bottom_field)
1066 /* XXX: infer it with matrix */ 1107 /* XXX: infer it with matrix */
1067 avctx->quality = 3; 1108 avctx->quality = 3;
1068 goto the_end; 1109 goto the_end;
1069 } 1110 }
1070 break; 1111 break;
1112 case APP0:
1113 mjpeg_decode_app(s, s->buffer, input_size);
1114 break;
1115 case SOF1:
1116 case SOF2:
1117 case SOF3:
1118 case SOF5:
1119 case SOF6:
1120 case SOF7:
1121 case SOF9:
1122 case SOF10:
1123 case SOF11:
1124 case SOF13:
1125 case SOF14:
1126 case SOF15:
1127 case JPG:
1128 printf("mjpeg: unsupported coding type (%x)\n", start_code);
1129 return -1;
1071 } 1130 }
1072 } 1131 }
1073 } 1132 }
1074 } 1133 }
1075 the_end: 1134 the_end: