comparison mjpeg.c @ 775:e96776e1d2ae libavcodec

reworked decode_frame marker searching, fixes many non-working samples
author al3x
date Sun, 27 Oct 2002 17:59:35 +0000
parents e65798d228ea
children 1c68acb163e2
comparison
equal deleted inserted replaced
774:baa66649df35 775:e96776e1d2ae
15 * You should have received a copy of the GNU Lesser General Public 15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software 16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * 18 *
19 * Support for external huffman table, various fixes (AVID workaround), 19 * Support for external huffman table, various fixes (AVID workaround),
20 * aspecting and various markers support 20 * aspecting and new decode_frame mechanism
21 * by Alex Beregszaszi <alex@naxine.org> 21 * by Alex Beregszaszi <alex@naxine.org>
22 */ 22 */
23 //#define DEBUG 23 #define DEBUG
24 #include "avcodec.h" 24 #include "avcodec.h"
25 #include "dsputil.h" 25 #include "dsputil.h"
26 #include "mpegvideo.h" 26 #include "mpegvideo.h"
27 27
28 #ifdef USE_FASTMEMCPY 28 #ifdef USE_FASTMEMCPY
29 #include "fastmemcpy.h" 29 #include "fastmemcpy.h"
30 #endif 30 #endif
31 31
32 /* use two quantizer table (one for luminance and one for chrominance) */ 32 /* use two quantizer tables (one for luminance and one for chrominance) */
33 /* not yet working */ 33 /* not yet working */
34 #undef TWOMATRIXES 34 #undef TWOMATRIXES
35 35
36 typedef struct MJpegContext { 36 typedef struct MJpegContext {
37 UINT8 huff_size_dc_luminance[12]; 37 UINT8 huff_size_dc_luminance[12];
588 #define MAX_COMPONENTS 4 588 #define MAX_COMPONENTS 4
589 589
590 typedef struct MJpegDecodeContext { 590 typedef struct MJpegDecodeContext {
591 AVCodecContext *avctx; 591 AVCodecContext *avctx;
592 GetBitContext gb; 592 GetBitContext gb;
593 UINT32 header_state; 593 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
594
594 int start_code; /* current start code */ 595 int start_code; /* current start code */
595 UINT8 *buf_ptr;
596 int buffer_size; 596 int buffer_size;
597 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ 597 UINT8 *buffer;
598
598 INT16 quant_matrixes[4][64]; 599 INT16 quant_matrixes[4][64];
599 VLC vlcs[2][4]; 600 VLC vlcs[2][4];
600 601
601 int org_width, org_height; /* size given at codec init */ 602 int org_width, org_height; /* size given at codec init */
602 int first_picture; /* true if decoding first picture */ 603 int first_picture; /* true if decoding first picture */
612 int quant_index[4]; /* quant table index for each component */ 613 int quant_index[4]; /* quant table index for each component */
613 int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */ 614 int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
614 UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */ 615 UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
615 int linesize[MAX_COMPONENTS]; 616 int linesize[MAX_COMPONENTS];
616 DCTELEM block[64] __align8; 617 DCTELEM block[64] __align8;
617 UINT8 buffer[PICTURE_BUFFER_SIZE];
618 618
619 int buggy_avid; 619 int buggy_avid;
620 int restart_interval; 620 int restart_interval;
621 int restart_count; 621 int restart_count;
622 int interleaved_rows; 622 int interleaved_rows;
623 ScanTable scantable; 623 ScanTable scantable;
624 void (*idct_put)(UINT8 *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/); 624 void (*idct_put)(UINT8 *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
625 } MJpegDecodeContext; 625 } MJpegDecodeContext;
626 626
627 #define SKIP_REMAINING(gb, len) { \ 627 static int mjpeg_decode_dht(MJpegDecodeContext *s);
628 dprintf("reamining %d bytes in marker\n", len); \
629 if (len) while (--len) \
630 skip_bits(gb, 8); \
631 }
632
633 static int mjpeg_decode_dht(MJpegDecodeContext *s, UINT8 *buf, int buf_size);
634 628
635 static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table, 629 static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
636 int nb_codes) 630 int nb_codes)
637 { 631 {
638 UINT8 huff_size[256]; 632 UINT8 huff_size[256];
662 return -1; 656 return -1;
663 s->scantable= s2.intra_scantable; 657 s->scantable= s2.intra_scantable;
664 s->idct_put= s2.idct_put; 658 s->idct_put= s2.idct_put;
665 MPV_common_end(&s2); 659 MPV_common_end(&s2);
666 660
667 s->header_state = 0;
668 s->mpeg_enc_ctx_allocated = 0; 661 s->mpeg_enc_ctx_allocated = 0;
669 s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into 662 s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into
670 account FF 00 case */ 663 account FF 00 case */
664 s->buffer = av_malloc(s->buffer_size);
671 s->start_code = -1; 665 s->start_code = -1;
672 s->buf_ptr = s->buffer;
673 s->first_picture = 1; 666 s->first_picture = 1;
674 s->org_width = avctx->width; 667 s->org_width = avctx->width;
675 s->org_height = avctx->height; 668 s->org_height = avctx->height;
676 669
677 build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12); 670 build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
678 build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12); 671 build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
679 build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251); 672 build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
680 build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251); 673 build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
681 674
682 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF) 675 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
683 { 676 {
684 printf("mjpeg: using external huffman table\n"); 677 printf("mjpeg: using external huffman table\n");
685 mjpeg_decode_dht(s, avctx->extradata, avctx->extradata_size); 678 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size);
679 mjpeg_decode_dht(s);
686 /* should check for error - but dunno */ 680 /* should check for error - but dunno */
687 } 681 }
682
688 return 0; 683 return 0;
689 } 684 }
690 685
691 /* quantize tables */ 686 /* quantize tables */
692 static int mjpeg_decode_dqt(MJpegDecodeContext *s, 687 static int mjpeg_decode_dqt(MJpegDecodeContext *s)
693 UINT8 *buf, int buf_size)
694 { 688 {
695 int len, index, i, j; 689 int len, index, i, j;
696 init_get_bits(&s->gb, buf, buf_size);
697 690
698 len = get_bits(&s->gb, 16) - 2; 691 len = get_bits(&s->gb, 16) - 2;
699 692
700 while (len >= 65) { 693 while (len >= 65) {
701 /* only 8 bit precision handled */ 694 /* only 8 bit precision handled */
714 s->quant_matrixes[index][j] = get_bits(&s->gb, 8); 707 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
715 } 708 }
716 len -= 65; 709 len -= 65;
717 } 710 }
718 711
719 SKIP_REMAINING(&s->gb, len);
720
721 return 0; 712 return 0;
722 } 713 }
723 714
724 /* decode huffman tables and build VLC decoders */ 715 /* decode huffman tables and build VLC decoders */
725 static int mjpeg_decode_dht(MJpegDecodeContext *s, 716 static int mjpeg_decode_dht(MJpegDecodeContext *s)
726 UINT8 *buf, int buf_size)
727 { 717 {
728 int len, index, i, class, n, v, code_max; 718 int len, index, i, class, n, v, code_max;
729 UINT8 bits_table[17]; 719 UINT8 bits_table[17];
730 UINT8 val_table[256]; 720 UINT8 val_table[256];
731 721
732 init_get_bits(&s->gb, buf, buf_size); 722 len = get_bits(&s->gb, 16) - 2;
733
734 len = get_bits(&s->gb, 16);
735 len -= 2;
736 723
737 while (len > 0) { 724 while (len > 0) {
738 if (len < 17) 725 if (len < 17)
739 return -1; 726 return -1;
740 class = get_bits(&s->gb, 4); 727 class = get_bits(&s->gb, 4);
768 build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1); 755 build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
769 } 756 }
770 return 0; 757 return 0;
771 } 758 }
772 759
773 static int mjpeg_decode_sof0(MJpegDecodeContext *s, 760 static int mjpeg_decode_sof0(MJpegDecodeContext *s)
774 UINT8 *buf, int buf_size)
775 { 761 {
776 int len, nb_components, i, width, height; 762 int len, nb_components, i, width, height;
777
778 init_get_bits(&s->gb, buf, buf_size);
779 763
780 /* XXX: verify len field validity */ 764 /* XXX: verify len field validity */
781 len = get_bits(&s->gb, 16); 765 len = get_bits(&s->gb, 16);
782 /* only 8 bits/component accepted */ 766 /* only 8 bits/component accepted */
783 if (get_bits(&s->gb, 8) != 8) 767 if (get_bits(&s->gb, 8) != 8)
849 } 833 }
850 834
851 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index) 835 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
852 { 836 {
853 int code, diff; 837 int code, diff;
854 838 #if 0
839 code = get_vlc2(&s->gb, s->vlc[0][dc_index].table,
840 s->vlc[0][dc_index].bits, 1);
841 #else
855 code = get_vlc(&s->gb, &s->vlcs[0][dc_index]); 842 code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);
843 #endif
856 if (code < 0) 844 if (code < 0)
857 { 845 {
858 dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index, 846 dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
859 &s->vlcs[0][dc_index]); 847 &s->vlcs[0][dc_index]);
860 return 0xffff; 848 return 0xffff;
890 block[0] = val; 878 block[0] = val;
891 /* AC coefs */ 879 /* AC coefs */
892 ac_vlc = &s->vlcs[1][ac_index]; 880 ac_vlc = &s->vlcs[1][ac_index];
893 i = 1; 881 i = 1;
894 for(;;) { 882 for(;;) {
883 #if 0
884 code = get_vlc2(&s->gb, s->vlcs[1][ac_index].table,
885 s->vlcs[1][ac_index].bits, 2);
886 #else
895 code = get_vlc(&s->gb, ac_vlc); 887 code = get_vlc(&s->gb, ac_vlc);
888 #endif
896 if (code < 0) { 889 if (code < 0) {
897 dprintf("error ac\n"); 890 dprintf("error ac\n");
898 return -1; 891 return -1;
899 } 892 }
900 /* EOB */ 893 /* EOB */
921 } 914 }
922 } 915 }
923 return 0; 916 return 0;
924 } 917 }
925 918
926 static int mjpeg_decode_sos(MJpegDecodeContext *s, 919 static int mjpeg_decode_sos(MJpegDecodeContext *s)
927 UINT8 *buf, int buf_size)
928 { 920 {
929 int len, nb_components, i, j, n, h, v, ret; 921 int len, nb_components, i, j, n, h, v, ret;
930 int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id; 922 int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
931 int comp_index[4]; 923 int comp_index[4];
932 int dc_index[4]; 924 int dc_index[4];
933 int ac_index[4]; 925 int ac_index[4];
934 int nb_blocks[4]; 926 int nb_blocks[4];
935 int h_count[4]; 927 int h_count[4];
936 int v_count[4]; 928 int v_count[4];
937 929
938 init_get_bits(&s->gb, buf, buf_size);
939 /* XXX: verify len field validity */ 930 /* XXX: verify len field validity */
940 len = get_bits(&s->gb, 16); 931 len = get_bits(&s->gb, 16);
941 nb_components = get_bits(&s->gb, 8); 932 nb_components = get_bits(&s->gb, 8);
933 if (len != 6+2*nb_components)
934 {
935 dprintf("decode_sos: invalid len (%d)\n", len);
936 return -1;
937 }
942 /* XXX: only interleaved scan accepted */ 938 /* XXX: only interleaved scan accepted */
943 if (nb_components != 3) 939 if (nb_components != 3)
944 { 940 {
945 dprintf("decode_sos: components(%d) mismatch\n", nb_components); 941 dprintf("decode_sos: components(%d) mismatch\n", nb_components);
946 return -1; 942 return -1;
1059 out_of_range: 1055 out_of_range:
1060 dprintf("decode_sos: ac/dc index out of range\n"); 1056 dprintf("decode_sos: ac/dc index out of range\n");
1061 return -1; 1057 return -1;
1062 } 1058 }
1063 1059
1064 static int mjpeg_decode_dri(MJpegDecodeContext *s, 1060 static int mjpeg_decode_dri(MJpegDecodeContext *s)
1065 UINT8 *buf, int buf_size) 1061 {
1066 {
1067 init_get_bits(&s->gb, buf, buf_size);
1068
1069 if (get_bits(&s->gb, 16) != 4) 1062 if (get_bits(&s->gb, 16) != 4)
1070 return -1; 1063 return -1;
1071 s->restart_interval = get_bits(&s->gb, 16); 1064 s->restart_interval = get_bits(&s->gb, 16);
1072 dprintf("restart interval: %d\n", s->restart_interval); 1065 dprintf("restart interval: %d\n", s->restart_interval);
1073 1066
1074 return 0; 1067 return 0;
1075 } 1068 }
1076 1069
1077 #define FOURCC(a,b,c,d) ((a << 24) | (b << 16) | (c << 8) | d) 1070 #define FOURCC(a,b,c,d) ((a << 24) | (b << 16) | (c << 8) | d)
1078 static int mjpeg_decode_app(MJpegDecodeContext *s, 1071 static int mjpeg_decode_app(MJpegDecodeContext *s, int start_code)
1079 UINT8 *buf, int buf_size, int start_code)
1080 { 1072 {
1081 int len, id; 1073 int len, id;
1082
1083 init_get_bits(&s->gb, buf, buf_size);
1084 1074
1085 /* XXX: verify len field validity */ 1075 /* XXX: verify len field validity */
1086 len = get_bits(&s->gb, 16); 1076 len = get_bits(&s->gb, 16);
1087 if (len < 5) 1077 if (len < 5)
1088 return -1; 1078 return -1;
1162 } 1152 }
1163 } 1153 }
1164 1154
1165 out: 1155 out:
1166 /* should check for further values.. */ 1156 /* should check for further values.. */
1167 SKIP_REMAINING(&s->gb, len);
1168 1157
1169 return 0; 1158 return 0;
1170 } 1159 }
1171 #undef FOURCC 1160 #undef FOURCC
1172 1161
1173 static int mjpeg_decode_com(MJpegDecodeContext *s, 1162 static int mjpeg_decode_com(MJpegDecodeContext *s)
1174 UINT8 *buf, int buf_size)
1175 { 1163 {
1176 int len, i; 1164 int len, i;
1177 UINT8 *cbuf; 1165 UINT8 *cbuf;
1178
1179 init_get_bits(&s->gb, buf, buf_size);
1180 1166
1181 /* XXX: verify len field validity */ 1167 /* XXX: verify len field validity */
1182 len = get_bits(&s->gb, 16)-2; 1168 len = get_bits(&s->gb, 16)-2;
1183 cbuf = av_malloc(len+1); 1169 cbuf = av_malloc(len+1);
1184 1170
1193 1179
1194 /* buggy avid, it puts EOI only at every 10th frame */ 1180 /* buggy avid, it puts EOI only at every 10th frame */
1195 if (!strcmp(cbuf, "AVID")) 1181 if (!strcmp(cbuf, "AVID"))
1196 { 1182 {
1197 s->buggy_avid = 1; 1183 s->buggy_avid = 1;
1198 if (s->first_picture) 1184 // if (s->first_picture)
1199 printf("mjpeg: workarounding buggy AVID\n"); 1185 // printf("mjpeg: workarounding buggy AVID\n");
1200 } 1186 }
1201 1187
1202 av_free(cbuf); 1188 av_free(cbuf);
1203 1189
1204 return 0; 1190 return 0;
1205 } 1191 }
1192
1193 #if 0
1194 static int valid_marker_list[] =
1195 {
1196 /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
1197 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1198 /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1199 /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1200 /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1201 /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1202 /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1203 /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1204 /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1205 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1206 /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1207 /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1208 /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1209 /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1210 /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1211 /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1212 /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1213 }
1214 #endif
1206 1215
1207 /* return the 8 bit start code value and update the search 1216 /* return the 8 bit start code value and update the search
1208 state. Return -1 if no start code found */ 1217 state. Return -1 if no start code found */
1209 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end, 1218 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end)
1210 UINT32 *header_state)
1211 { 1219 {
1212 UINT8 *buf_ptr; 1220 UINT8 *buf_ptr;
1213 unsigned int state, v; 1221 unsigned int v, v2;
1214 int val; 1222 int val;
1215 1223 #ifdef DEBUG
1216 state = *header_state; 1224 int skipped=0;
1225 #endif
1226
1217 buf_ptr = *pbuf_ptr; 1227 buf_ptr = *pbuf_ptr;
1218 retry: 1228 while (buf_ptr < buf_end) {
1219 if (state) { 1229 v = *buf_ptr++;
1220 /* get marker */ 1230 v2 = *buf_ptr;
1221 found: 1231 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe)) {
1222 if (buf_ptr < buf_end) { 1232 val = *buf_ptr++;
1223 val = *buf_ptr++; 1233 goto found;
1224 state = 0;
1225 if ((val >= RST0) && (val <= RST7))
1226 goto retry;
1227 } else {
1228 val = -1;
1229 } 1234 }
1230 } else { 1235 #ifdef DEBUG
1231 while (buf_ptr < buf_end) { 1236 skipped++;
1232 v = *buf_ptr++; 1237 #endif
1233 if (v == 0xff) { 1238 }
1234 state = 1; 1239 val = -1;
1235 goto found; 1240 found:
1236 } 1241 #ifdef DEBUG
1237 } 1242 dprintf("find_marker skipped %d bytes\n", skipped);
1238 val = -1; 1243 #endif
1239 }
1240 *pbuf_ptr = buf_ptr; 1244 *pbuf_ptr = buf_ptr;
1241 *header_state = state;
1242 return val; 1245 return val;
1243 } 1246 }
1244 1247
1245 static int mjpeg_decode_frame(AVCodecContext *avctx, 1248 static int mjpeg_decode_frame(AVCodecContext *avctx,
1246 void *data, int *data_size, 1249 void *data, int *data_size,
1247 UINT8 *buf, int buf_size) 1250 UINT8 *buf, int buf_size)
1248 { 1251 {
1249 MJpegDecodeContext *s = avctx->priv_data; 1252 MJpegDecodeContext *s = avctx->priv_data;
1250 UINT8 *buf_end, *buf_ptr, *buf_start; 1253 UINT8 *buf_end, *buf_ptr;
1251 int len, code, input_size, i; 1254 int i, start_code;
1252 AVPicture *picture = data; 1255 AVPicture *picture = data;
1253 unsigned int start_code;
1254 1256
1255 *data_size = 0; 1257 *data_size = 0;
1256 1258
1257 /* no supplementary picture */ 1259 /* no supplementary picture */
1258 if (buf_size == 0) 1260 if (buf_size == 0)
1259 return 0; 1261 return 0;
1260 1262
1261 buf_ptr = buf; 1263 buf_ptr = buf;
1262 buf_end = buf + buf_size; 1264 buf_end = buf + buf_size;
1263 while (buf_ptr < buf_end) { 1265 while (buf_ptr < buf_end) {
1264 buf_start = buf_ptr;
1265 /* find start next marker */ 1266 /* find start next marker */
1266 code = find_marker(&buf_ptr, buf_end, &s->header_state); 1267 start_code = find_marker(&buf_ptr, buf_end);
1267 /* copy to buffer */ 1268 {
1268 len = buf_ptr - buf_start; 1269 /* EOF */
1269 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) { 1270 if (start_code < 0) {
1270 /* data too big : flush */ 1271 goto the_end;
1271 s->buf_ptr = s->buffer;
1272 if (code > 0)
1273 s->start_code = code;
1274 } else {
1275 memcpy(s->buf_ptr, buf_start, len);
1276 s->buf_ptr += len;
1277 if (code < 0) {
1278 /* nothing to do: wait next marker */
1279 } else if (code == 0 || code == 0xff) {
1280 /* if we got FF 00, we copy FF to the stream to unescape FF 00 */
1281 /* valid marker code is between 00 and ff - alex */
1282 s->buf_ptr--;
1283 } else { 1272 } else {
1284 /* prepare data for next start code */ 1273 dprintf("marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr);
1285 input_size = s->buf_ptr - s->buffer; 1274
1286 start_code = s->start_code; 1275 if ((buf_end - buf_ptr) > s->buffer_size)
1287 s->buf_ptr = s->buffer; 1276 {
1288 s->start_code = code; 1277 av_free(s->buffer);
1289 dprintf("marker=%x\n", start_code); 1278 s->buffer_size = buf_end-buf_ptr;
1279 s->buffer = av_malloc(s->buffer_size);
1280 }
1281
1282 /* unescape buffer of SOS */
1283 if (start_code == SOS)
1284 {
1285 UINT8 *src = buf_ptr;
1286 UINT8 *dst = s->buffer;
1287
1288 while (src<buf_end)
1289 {
1290 unsigned char *x = *(src++);
1291
1292 *(dst++) = x;
1293 if (x == 0xff)
1294 {
1295 x = *(src++);
1296 if (x >= 0xd0 && x <= 0xd7)
1297 *(dst++) = x;
1298 else if (x)
1299 break;
1300 }
1301 }
1302 }
1303 else
1304 memcpy(s->buffer, buf_ptr, buf_end - buf_ptr);
1305 init_get_bits(&s->gb, s->buffer, s->buffer_size);
1306
1307 s->start_code = start_code;
1308
1309 /* process markers */
1310 if (start_code >= 0xd0 && start_code <= 0xd7) {
1311 dprintf("restart marker: %d\n", start_code&0x0f);
1312 } else if (s->first_picture) {
1313 /* APP fields */
1314 if (start_code >= 0xe0 && start_code <= 0xef)
1315 mjpeg_decode_app(s, start_code);
1316 /* Comment */
1317 else if (start_code == COM)
1318 mjpeg_decode_com(s);
1319 }
1320
1290 switch(start_code) { 1321 switch(start_code) {
1291 case SOI: 1322 case SOI:
1292 s->restart_interval = 0; 1323 s->restart_interval = 0;
1293 /* nothing to do on SOI */ 1324 /* nothing to do on SOI */
1294 break; 1325 break;
1295 case DQT: 1326 case DQT:
1296 mjpeg_decode_dqt(s, s->buffer, input_size); 1327 mjpeg_decode_dqt(s);
1297 break; 1328 break;
1298 case DHT: 1329 case DHT:
1299 mjpeg_decode_dht(s, s->buffer, input_size); 1330 mjpeg_decode_dht(s);
1300 break; 1331 break;
1301 case SOF0: 1332 case SOF0:
1302 mjpeg_decode_sof0(s, s->buffer, input_size); 1333 mjpeg_decode_sof0(s);
1303 break; 1334 break;
1304 case SOS: 1335 case EOI:
1305 mjpeg_decode_sos(s, s->buffer, input_size); 1336 eoi_parser:
1306 if (s->start_code == EOI || s->buggy_avid || s->restart_interval) { 1337 {
1307 int l; 1338 int l;
1308 if (s->interlaced) { 1339 if (s->interlaced) {
1309 s->bottom_field ^= 1; 1340 s->bottom_field ^= 1;
1310 /* if not bottom field, do not output image yet */ 1341 /* if not bottom field, do not output image yet */
1311 if (s->bottom_field) 1342 if (s->bottom_field)
1312 goto not_the_end; 1343 goto not_the_end;
1313 } 1344 }
1314 for(i=0;i<3;i++) { 1345 for(i=0;i<3;i++) {
1315 picture->data[i] = s->current_picture[i]; 1346 picture->data[i] = s->current_picture[i];
1347 #if 1
1316 l = s->linesize[i]; 1348 l = s->linesize[i];
1317 if (s->interlaced) 1349 if (s->interlaced)
1318 l >>= 1; 1350 l >>= 1;
1319 picture->linesize[i] = l; 1351 picture->linesize[i] = l;
1352 #else
1353 picture->linesize[i] = (s->interlaced) ?
1354 s->linesize[i] >> 1 : s->linesize[i];
1355 #endif
1320 } 1356 }
1321 *data_size = sizeof(AVPicture); 1357 *data_size = sizeof(AVPicture);
1322 avctx->height = s->height; 1358 avctx->height = s->height;
1323 if (s->interlaced) 1359 if (s->interlaced)
1324 avctx->height *= 2; 1360 avctx->height *= 2;
1339 /* dummy quality */ 1375 /* dummy quality */
1340 /* XXX: infer it with matrix */ 1376 /* XXX: infer it with matrix */
1341 avctx->quality = 3; 1377 avctx->quality = 3;
1342 goto the_end; 1378 goto the_end;
1343 } 1379 }
1380 break;
1381 case SOS:
1382 mjpeg_decode_sos(s);
1383 /* buggy avid puts EOI every 10-20th frame */
1384 /* if restart period is over process EOI */
1385 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1386 goto eoi_parser;
1344 break; 1387 break;
1345 case DRI: 1388 case DRI:
1346 mjpeg_decode_dri(s, s->buffer, input_size); 1389 mjpeg_decode_dri(s);
1347 break; 1390 break;
1348 case SOF1: 1391 case SOF1:
1349 case SOF2: 1392 case SOF2:
1350 case SOF3: 1393 case SOF3:
1351 case SOF5: 1394 case SOF5:
1357 case SOF13: 1400 case SOF13:
1358 case SOF14: 1401 case SOF14:
1359 case SOF15: 1402 case SOF15:
1360 case JPG: 1403 case JPG:
1361 printf("mjpeg: unsupported coding type (%x)\n", start_code); 1404 printf("mjpeg: unsupported coding type (%x)\n", start_code);
1362 return -1; 1405 break;
1406 // default:
1407 // printf("mjpeg: unsupported marker (%x)\n", start_code);
1408 // break;
1363 } 1409 }
1364 #if 1 1410
1365 if (start_code >= 0xd0 && start_code <= 0xd7) { 1411 not_the_end:
1366 dprintf("restart marker: %d\n", start_code&0x0f); 1412 /* eof process start code */
1367 } else if (s->first_picture) { 1413 buf_ptr += (get_bits_count(&s->gb)+7)/8;
1368 /* APP fields */ 1414 dprintf("marker parser used %d bytes (%d bits)\n",
1369 if (start_code >= 0xe0 && start_code <= 0xef) 1415 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
1370 mjpeg_decode_app(s, s->buffer, input_size, start_code);
1371 /* Comment */
1372 else if (start_code == COM)
1373 mjpeg_decode_com(s, s->buffer, input_size);
1374 }
1375 #endif
1376 } 1416 }
1377 } 1417 }
1378 not_the_end: 1418 }
1379 ; 1419 the_end:
1380 } 1420
1381 the_end: 1421 dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
1422 // return buf_end - buf_ptr;
1382 return buf_ptr - buf; 1423 return buf_ptr - buf;
1383 } 1424 }
1384 1425
1385 static int mjpeg_decode_end(AVCodecContext *avctx) 1426 static int mjpeg_decode_end(AVCodecContext *avctx)
1386 { 1427 {
1387 MJpegDecodeContext *s = avctx->priv_data; 1428 MJpegDecodeContext *s = avctx->priv_data;
1388 int i, j; 1429 int i, j;
1389 1430
1431 av_free(s->buffer);
1390 for(i=0;i<MAX_COMPONENTS;i++) 1432 for(i=0;i<MAX_COMPONENTS;i++)
1391 av_free(s->current_picture[i]); 1433 av_free(s->current_picture[i]);
1392 for(i=0;i<2;i++) { 1434 for(i=0;i<2;i++) {
1393 for(j=0;j<4;j++) 1435 for(j=0;j<4;j++)
1394 free_vlc(&s->vlcs[i][j]); 1436 free_vlc(&s->vlcs[i][j]);