comparison mjpeg.c @ 2979:bfabfdf9ce55 libavcodec

COSMETICS: tabs --> spaces, some prettyprinting
author diego
date Thu, 22 Dec 2005 01:10:11 +0000
parents b5e8b6821274
children 0b546eab515d
comparison
equal deleted inserted replaced
2978:403183bbb505 2979:bfabfdf9ce55
52 } MJpegContext; 52 } MJpegContext;
53 53
54 /* JPEG marker codes */ 54 /* JPEG marker codes */
55 typedef enum { 55 typedef enum {
56 /* start of frame */ 56 /* start of frame */
57 SOF0 = 0xc0, /* baseline */ 57 SOF0 = 0xc0, /* baseline */
58 SOF1 = 0xc1, /* extended sequential, huffman */ 58 SOF1 = 0xc1, /* extended sequential, huffman */
59 SOF2 = 0xc2, /* progressive, huffman */ 59 SOF2 = 0xc2, /* progressive, huffman */
60 SOF3 = 0xc3, /* lossless, huffman */ 60 SOF3 = 0xc3, /* lossless, huffman */
61 61
62 SOF5 = 0xc5, /* differential sequential, huffman */ 62 SOF5 = 0xc5, /* differential sequential, huffman */
63 SOF6 = 0xc6, /* differential progressive, huffman */ 63 SOF6 = 0xc6, /* differential progressive, huffman */
64 SOF7 = 0xc7, /* differential lossless, huffman */ 64 SOF7 = 0xc7, /* differential lossless, huffman */
65 JPG = 0xc8, /* reserved for JPEG extension */ 65 JPG = 0xc8, /* reserved for JPEG extension */
66 SOF9 = 0xc9, /* extended sequential, arithmetic */ 66 SOF9 = 0xc9, /* extended sequential, arithmetic */
67 SOF10 = 0xca, /* progressive, arithmetic */ 67 SOF10 = 0xca, /* progressive, arithmetic */
68 SOF11 = 0xcb, /* lossless, arithmetic */ 68 SOF11 = 0xcb, /* lossless, arithmetic */
69 69
70 SOF13 = 0xcd, /* differential sequential, arithmetic */ 70 SOF13 = 0xcd, /* differential sequential, arithmetic */
71 SOF14 = 0xce, /* differential progressive, arithmetic */ 71 SOF14 = 0xce, /* differential progressive, arithmetic */
72 SOF15 = 0xcf, /* differential lossless, arithmetic */ 72 SOF15 = 0xcf, /* differential lossless, arithmetic */
73 73
74 DHT = 0xc4, /* define huffman tables */ 74 DHT = 0xc4, /* define huffman tables */
75 75
76 DAC = 0xcc, /* define arithmetic-coding conditioning */ 76 DAC = 0xcc, /* define arithmetic-coding conditioning */
77 77
78 /* restart with modulo 8 count "m" */ 78 /* restart with modulo 8 count "m" */
79 RST0 = 0xd0, 79 RST0 = 0xd0,
80 RST1 = 0xd1, 80 RST1 = 0xd1,
81 RST2 = 0xd2, 81 RST2 = 0xd2,
83 RST4 = 0xd4, 83 RST4 = 0xd4,
84 RST5 = 0xd5, 84 RST5 = 0xd5,
85 RST6 = 0xd6, 85 RST6 = 0xd6,
86 RST7 = 0xd7, 86 RST7 = 0xd7,
87 87
88 SOI = 0xd8, /* start of image */ 88 SOI = 0xd8, /* start of image */
89 EOI = 0xd9, /* end of image */ 89 EOI = 0xd9, /* end of image */
90 SOS = 0xda, /* start of scan */ 90 SOS = 0xda, /* start of scan */
91 DQT = 0xdb, /* define quantization tables */ 91 DQT = 0xdb, /* define quantization tables */
92 DNL = 0xdc, /* define number of lines */ 92 DNL = 0xdc, /* define number of lines */
93 DRI = 0xdd, /* define restart interval */ 93 DRI = 0xdd, /* define restart interval */
94 DHP = 0xde, /* define hierarchical progression */ 94 DHP = 0xde, /* define hierarchical progression */
95 EXP = 0xdf, /* expand reference components */ 95 EXP = 0xdf, /* expand reference components */
96 96
97 APP0 = 0xe0, 97 APP0 = 0xe0,
98 APP1 = 0xe1, 98 APP1 = 0xe1,
99 APP2 = 0xe2, 99 APP2 = 0xe2,
100 APP3 = 0xe3, 100 APP3 = 0xe3,
116 JPG2 = 0xf2, 116 JPG2 = 0xf2,
117 JPG3 = 0xf3, 117 JPG3 = 0xf3,
118 JPG4 = 0xf4, 118 JPG4 = 0xf4,
119 JPG5 = 0xf5, 119 JPG5 = 0xf5,
120 JPG6 = 0xf6, 120 JPG6 = 0xf6,
121 SOF48 = 0xf7, ///< JPEG-LS 121 SOF48 = 0xf7, ///< JPEG-LS
122 LSE = 0xf8, ///< JPEG-LS extension parameters 122 LSE = 0xf8, ///< JPEG-LS extension parameters
123 JPG9 = 0xf9, 123 JPG9 = 0xf9,
124 JPG10 = 0xfa, 124 JPG10 = 0xfa,
125 JPG11 = 0xfb, 125 JPG11 = 0xfb,
126 JPG12 = 0xfc, 126 JPG12 = 0xfc,
127 JPG13 = 0xfd, 127 JPG13 = 0xfd,
128 128
129 COM = 0xfe, /* comment */ 129 COM = 0xfe, /* comment */
130 130
131 TEM = 0x01, /* temporary private use for arithmetic coding */ 131 TEM = 0x01, /* temporary private use for arithmetic coding */
132 132
133 /* 0x02 -> 0xbf reserved */ 133 /* 0x02 -> 0xbf reserved */
134 } JPEG_MARKER; 134 } JPEG_MARKER;
135 135
136 #if 0 136 #if 0
581 581
582 put_marker(&s->pb, EOI); 582 put_marker(&s->pb, EOI);
583 } 583 }
584 584
585 static inline void mjpeg_encode_dc(MpegEncContext *s, int val, 585 static inline void mjpeg_encode_dc(MpegEncContext *s, int val,
586 uint8_t *huff_size, uint16_t *huff_code) 586 uint8_t *huff_size, uint16_t *huff_code)
587 { 587 {
588 int mant, nbits; 588 int mant, nbits;
589 589
590 if (val == 0) { 590 if (val == 0) {
591 put_bits(&s->pb, huff_size[0], huff_code[0]); 591 put_bits(&s->pb, huff_size[0], huff_code[0]);
933 build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251, 0); 933 build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251, 0);
934 build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251, 0); 934 build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251, 0);
935 935
936 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF) 936 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
937 { 937 {
938 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n"); 938 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
939 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8); 939 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
940 mjpeg_decode_dht(s); 940 mjpeg_decode_dht(s);
941 /* should check for error - but dunno */ 941 /* should check for error - but dunno */
942 } 942 }
943 943
944 return 0; 944 return 0;
945 } 945 }
946 946
1015 len = get_bits(&s->gb, 16) - 2; 1015 len = get_bits(&s->gb, 16) - 2;
1016 1016
1017 while (len >= 65) { 1017 while (len >= 65) {
1018 /* only 8 bit precision handled */ 1018 /* only 8 bit precision handled */
1019 if (get_bits(&s->gb, 4) != 0) 1019 if (get_bits(&s->gb, 4) != 0)
1020 { 1020 {
1021 dprintf("dqt: 16bit precision\n"); 1021 dprintf("dqt: 16bit precision\n");
1022 return -1; 1022 return -1;
1023 } 1023 }
1024 index = get_bits(&s->gb, 4); 1024 index = get_bits(&s->gb, 4);
1025 if (index >= 4) 1025 if (index >= 4)
1026 return -1; 1026 return -1;
1027 dprintf("index=%d\n", index); 1027 dprintf("index=%d\n", index);
1028 /* read quant table */ 1028 /* read quant table */
1029 for(i=0;i<64;i++) { 1029 for(i=0;i<64;i++) {
1030 j = s->scantable.permutated[i]; 1030 j = s->scantable.permutated[i];
1031 s->quant_matrixes[index][j] = get_bits(&s->gb, 8); 1031 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
1032 } 1032 }
1033 1033
1034 //XXX FIXME finetune, and perhaps add dc too 1034 //XXX FIXME finetune, and perhaps add dc too
1035 s->qscale[index]= FFMAX( 1035 s->qscale[index]= FFMAX(
1036 s->quant_matrixes[index][s->scantable.permutated[1]], 1036 s->quant_matrixes[index][s->scantable.permutated[1]],
1037 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1; 1037 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
1038 dprintf("qscale[%d]: %d\n", index, s->qscale[index]); 1038 dprintf("qscale[%d]: %d\n", index, s->qscale[index]);
1039 len -= 65; 1039 len -= 65;
1040 } 1040 }
1041 1041
1042 return 0; 1042 return 0;
1043 } 1043 }
1130 s->v_max = s->v_count[i]; 1130 s->v_max = s->v_count[i];
1131 s->quant_index[i] = get_bits(&s->gb, 8); 1131 s->quant_index[i] = get_bits(&s->gb, 8);
1132 if (s->quant_index[i] >= 4) 1132 if (s->quant_index[i] >= 4)
1133 return -1; 1133 return -1;
1134 dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i], 1134 dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
1135 s->v_count[i], s->component_id[i], s->quant_index[i]); 1135 s->v_count[i], s->component_id[i], s->quant_index[i]);
1136 } 1136 }
1137 1137
1138 if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1; 1138 if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
1139 1139
1140 /* if different size, realloc/alloc picture */ 1140 /* if different size, realloc/alloc picture */
1149 /* test interlaced mode */ 1149 /* test interlaced mode */
1150 if (s->first_picture && 1150 if (s->first_picture &&
1151 s->org_height != 0 && 1151 s->org_height != 0 &&
1152 s->height < ((s->org_height * 3) / 4)) { 1152 s->height < ((s->org_height * 3) / 4)) {
1153 s->interlaced = 1; 1153 s->interlaced = 1;
1154 // s->bottom_field = (s->interlace_polarity) ? 1 : 0; 1154 // s->bottom_field = (s->interlace_polarity) ? 1 : 0;
1155 s->bottom_field = 0; 1155 s->bottom_field = 0;
1156 s->avctx->height *= 2; 1156 s->avctx->height *= 2;
1157 } 1157 }
1158 1158
1159 s->qscale_table= av_mallocz((s->width+15)/16); 1159 s->qscale_table= av_mallocz((s->width+15)/16);
1200 1200
1201 // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height); 1201 // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height);
1202 1202
1203 if (len != (8+(3*nb_components))) 1203 if (len != (8+(3*nb_components)))
1204 { 1204 {
1205 dprintf("decode_sof0: error, len(%d) mismatch\n", len); 1205 dprintf("decode_sof0: error, len(%d) mismatch\n", len);
1206 } 1206 }
1207 1207
1208 return 0; 1208 return 0;
1209 } 1209 }
1210 1210
1212 { 1212 {
1213 int code; 1213 int code;
1214 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2); 1214 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
1215 if (code < 0) 1215 if (code < 0)
1216 { 1216 {
1217 dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index, 1217 dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
1218 &s->vlcs[0][dc_index]); 1218 &s->vlcs[0][dc_index]);
1219 return 0xffff; 1219 return 0xffff;
1220 } 1220 }
1221 1221
1222 if(code) 1222 if(code)
1245 block[0] = val; 1245 block[0] = val;
1246 /* AC coefs */ 1246 /* AC coefs */
1247 ac_vlc = &s->vlcs[1][ac_index]; 1247 ac_vlc = &s->vlcs[1][ac_index];
1248 i = 1; 1248 i = 1;
1249 for(;;) { 1249 for(;;) {
1250 code = get_vlc2(&s->gb, s->vlcs[1][ac_index].table, 9, 2); 1250 code = get_vlc2(&s->gb, s->vlcs[1][ac_index].table, 9, 2);
1251 1251
1252 if (code < 0) { 1252 if (code < 0) {
1253 dprintf("error ac\n"); 1253 dprintf("error ac\n");
1254 return -1; 1254 return -1;
1255 } 1255 }
1450 s->dc_index[i], s->ac_index[i], 1450 s->dc_index[i], s->ac_index[i],
1451 s->quant_index[c]) < 0) { 1451 s->quant_index[c]) < 0) {
1452 dprintf("error y=%d x=%d\n", mb_y, mb_x); 1452 dprintf("error y=%d x=%d\n", mb_y, mb_x);
1453 return -1; 1453 return -1;
1454 } 1454 }
1455 // dprintf("mb: %d %d processed\n", mb_y, mb_x); 1455 // dprintf("mb: %d %d processed\n", mb_y, mb_x);
1456 ptr = s->picture.data[c] + 1456 ptr = s->picture.data[c] +
1457 (((s->linesize[c] * (v * mb_y + y) * 8) + 1457 (((s->linesize[c] * (v * mb_y + y) * 8) +
1458 (h * mb_x + x) * 8) >> s->avctx->lowres); 1458 (h * mb_x + x) * 8) >> s->avctx->lowres);
1459 if (s->interlaced && s->bottom_field) 1459 if (s->interlaced && s->bottom_field)
1460 ptr += s->linesize[c] >> 1; 1460 ptr += s->linesize[c] >> 1;
1489 /* XXX: verify len field validity */ 1489 /* XXX: verify len field validity */
1490 len = get_bits(&s->gb, 16); 1490 len = get_bits(&s->gb, 16);
1491 nb_components = get_bits(&s->gb, 8); 1491 nb_components = get_bits(&s->gb, 8);
1492 if (len != 6+2*nb_components) 1492 if (len != 6+2*nb_components)
1493 { 1493 {
1494 dprintf("decode_sos: invalid len (%d)\n", len); 1494 dprintf("decode_sos: invalid len (%d)\n", len);
1495 return -1; 1495 return -1;
1496 } 1496 }
1497 /* XXX: only interleaved scan accepted */ 1497 /* XXX: only interleaved scan accepted */
1498 if (nb_components != s->nb_components) 1498 if (nb_components != s->nb_components)
1499 { 1499 {
1500 dprintf("decode_sos: components(%d) mismatch\n", nb_components); 1500 dprintf("decode_sos: components(%d) mismatch\n", nb_components);
1501 return -1; 1501 return -1;
1502 } 1502 }
1503 vmax = 0; 1503 vmax = 0;
1504 hmax = 0; 1504 hmax = 0;
1505 for(i=0;i<nb_components;i++) { 1505 for(i=0;i<nb_components;i++) {
1506 id = get_bits(&s->gb, 8) - 1; 1506 id = get_bits(&s->gb, 8) - 1;
1507 dprintf("component: %d\n", id); 1507 dprintf("component: %d\n", id);
1508 /* find component index */ 1508 /* find component index */
1509 for(index=0;index<s->nb_components;index++) 1509 for(index=0;index<s->nb_components;index++)
1510 if (id == s->component_id[index]) 1510 if (id == s->component_id[index])
1511 break; 1511 break;
1512 if (index == s->nb_components) 1512 if (index == s->nb_components)
1513 { 1513 {
1514 dprintf("decode_sos: index(%d) out of components\n", index); 1514 dprintf("decode_sos: index(%d) out of components\n", index);
1515 return -1; 1515 return -1;
1516 } 1516 }
1517 1517
1518 s->comp_index[i] = index; 1518 s->comp_index[i] = index;
1519 1519
1520 s->nb_blocks[i] = s->h_count[index] * s->v_count[index]; 1520 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1521 s->h_scount[i] = s->h_count[index]; 1521 s->h_scount[i] = s->h_count[index];
1522 s->v_scount[i] = s->v_count[index]; 1522 s->v_scount[i] = s->v_count[index];
1523 1523
1524 s->dc_index[i] = get_bits(&s->gb, 4); 1524 s->dc_index[i] = get_bits(&s->gb, 4);
1525 s->ac_index[i] = get_bits(&s->gb, 4); 1525 s->ac_index[i] = get_bits(&s->gb, 4);
1526 1526
1527 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 || 1527 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1528 s->dc_index[i] >= 4 || s->ac_index[i] >= 4) 1528 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1529 goto out_of_range; 1529 goto out_of_range;
1530 #if 0 //buggy 1530 #if 0 //buggy
1531 switch(s->start_code) 1531 switch(s->start_code)
1532 { 1532 {
1533 case SOF0: 1533 case SOF0:
1534 if (dc_index[i] > 1 || ac_index[i] > 1) 1534 if (dc_index[i] > 1 || ac_index[i] > 1)
1535 goto out_of_range; 1535 goto out_of_range;
1536 break; 1536 break;
1537 case SOF1: 1537 case SOF1:
1538 case SOF2: 1538 case SOF2:
1539 if (dc_index[i] > 3 || ac_index[i] > 3) 1539 if (dc_index[i] > 3 || ac_index[i] > 3)
1540 goto out_of_range; 1540 goto out_of_range;
1541 break; 1541 break;
1542 case SOF3: 1542 case SOF3:
1543 if (dc_index[i] > 3 || ac_index[i] != 0) 1543 if (dc_index[i] > 3 || ac_index[i] != 0)
1544 goto out_of_range; 1544 goto out_of_range;
1545 break; 1545 break;
1546 } 1546 }
1547 #endif 1547 #endif
1548 } 1548 }
1549 1549
1550 predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */ 1550 predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1551 ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */ 1551 ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1603 } 1603 }
1604 1604
1605 static int mjpeg_decode_dri(MJpegDecodeContext *s) 1605 static int mjpeg_decode_dri(MJpegDecodeContext *s)
1606 { 1606 {
1607 if (get_bits(&s->gb, 16) != 4) 1607 if (get_bits(&s->gb, 16) != 4)
1608 return -1; 1608 return -1;
1609 s->restart_interval = get_bits(&s->gb, 16); 1609 s->restart_interval = get_bits(&s->gb, 16);
1610 s->restart_count = 0; 1610 s->restart_count = 0;
1611 dprintf("restart interval: %d\n", s->restart_interval); 1611 dprintf("restart interval: %d\n", s->restart_interval);
1612 1612
1613 return 0; 1613 return 0;
1617 { 1617 {
1618 int len, id; 1618 int len, id;
1619 1619
1620 len = get_bits(&s->gb, 16); 1620 len = get_bits(&s->gb, 16);
1621 if (len < 5) 1621 if (len < 5)
1622 return -1; 1622 return -1;
1623 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits) 1623 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
1624 return -1; 1624 return -1;
1625 1625
1626 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); 1626 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1627 id = be2me_32(id); 1627 id = be2me_32(id);
1634 /* buggy AVID, it puts EOI only at every 10th frame */ 1634 /* buggy AVID, it puts EOI only at every 10th frame */
1635 /* also this fourcc is used by non-avid files too, it holds some 1635 /* also this fourcc is used by non-avid files too, it holds some
1636 informations, but it's always present in AVID creates files */ 1636 informations, but it's always present in AVID creates files */
1637 if (id == ff_get_fourcc("AVI1")) 1637 if (id == ff_get_fourcc("AVI1"))
1638 { 1638 {
1639 /* structure: 1639 /* structure:
1640 4bytes AVI1 1640 4bytes AVI1
1641 1bytes polarity 1641 1bytes polarity
1642 1bytes always zero 1642 1bytes always zero
1643 4bytes field_size 1643 4bytes field_size
1644 4bytes field_size_less_padding 1644 4bytes field_size_less_padding
1645 */ 1645 */
1646 s->buggy_avid = 1; 1646 s->buggy_avid = 1;
1647 // if (s->first_picture) 1647 // if (s->first_picture)
1648 // printf("mjpeg: workarounding buggy AVID\n"); 1648 // printf("mjpeg: workarounding buggy AVID\n");
1649 s->interlace_polarity = get_bits(&s->gb, 8); 1649 s->interlace_polarity = get_bits(&s->gb, 8);
1650 #if 0 1650 #if 0
1651 skip_bits(&s->gb, 8); 1651 skip_bits(&s->gb, 8);
1652 skip_bits(&s->gb, 32); 1652 skip_bits(&s->gb, 32);
1653 skip_bits(&s->gb, 32); 1653 skip_bits(&s->gb, 32);
1654 len -= 10; 1654 len -= 10;
1655 #endif 1655 #endif
1656 // if (s->interlace_polarity) 1656 // if (s->interlace_polarity)
1657 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity); 1657 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
1658 goto out; 1658 goto out;
1659 } 1659 }
1660 1660
1661 // len -= 2; 1661 // len -= 2;
1662 1662
1663 if (id == ff_get_fourcc("JFIF")) 1663 if (id == ff_get_fourcc("JFIF"))
1664 { 1664 {
1665 int t_w, t_h, v1, v2; 1665 int t_w, t_h, v1, v2;
1666 skip_bits(&s->gb, 8); /* the trailing zero-byte */ 1666 skip_bits(&s->gb, 8); /* the trailing zero-byte */
1667 v1= get_bits(&s->gb, 8); 1667 v1= get_bits(&s->gb, 8);
1668 v2= get_bits(&s->gb, 8); 1668 v2= get_bits(&s->gb, 8);
1669 skip_bits(&s->gb, 8); 1669 skip_bits(&s->gb, 8);
1670 1670
1671 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16); 1671 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
1672 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16); 1672 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
1676 v1, v2, 1676 v1, v2,
1677 s->avctx->sample_aspect_ratio.num, 1677 s->avctx->sample_aspect_ratio.num,
1678 s->avctx->sample_aspect_ratio.den 1678 s->avctx->sample_aspect_ratio.den
1679 ); 1679 );
1680 1680
1681 t_w = get_bits(&s->gb, 8); 1681 t_w = get_bits(&s->gb, 8);
1682 t_h = get_bits(&s->gb, 8); 1682 t_h = get_bits(&s->gb, 8);
1683 if (t_w && t_h) 1683 if (t_w && t_h)
1684 { 1684 {
1685 /* skip thumbnail */ 1685 /* skip thumbnail */
1686 if (len-10-(t_w*t_h*3) > 0) 1686 if (len-10-(t_w*t_h*3) > 0)
1687 len -= t_w*t_h*3; 1687 len -= t_w*t_h*3;
1688 } 1688 }
1689 len -= 10; 1689 len -= 10;
1690 goto out; 1690 goto out;
1691 } 1691 }
1692 1692
1693 if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e')) 1693 if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e'))
1694 { 1694 {
1695 if (s->avctx->debug & FF_DEBUG_PICT_INFO) 1695 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1696 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n"); 1696 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
1697 skip_bits(&s->gb, 16); /* version */ 1697 skip_bits(&s->gb, 16); /* version */
1698 skip_bits(&s->gb, 16); /* flags0 */ 1698 skip_bits(&s->gb, 16); /* flags0 */
1699 skip_bits(&s->gb, 16); /* flags1 */ 1699 skip_bits(&s->gb, 16); /* flags1 */
1700 skip_bits(&s->gb, 8); /* transform */ 1700 skip_bits(&s->gb, 8); /* transform */
1701 len -= 7; 1701 len -= 7;
1702 goto out; 1702 goto out;
1703 } 1703 }
1704 1704
1705 if (id == ff_get_fourcc("LJIF")){ 1705 if (id == ff_get_fourcc("LJIF")){
1706 if (s->avctx->debug & FF_DEBUG_PICT_INFO) 1706 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1707 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n"); 1707 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
1708 skip_bits(&s->gb, 16); /* version ? */ 1708 skip_bits(&s->gb, 16); /* version ? */
1709 skip_bits(&s->gb, 16); /* unknwon always 0? */ 1709 skip_bits(&s->gb, 16); /* unknwon always 0? */
1710 skip_bits(&s->gb, 16); /* unknwon always 0? */ 1710 skip_bits(&s->gb, 16); /* unknwon always 0? */
1711 skip_bits(&s->gb, 16); /* unknwon always 0? */ 1711 skip_bits(&s->gb, 16); /* unknwon always 0? */
1712 switch( get_bits(&s->gb, 8)){ 1712 switch( get_bits(&s->gb, 8)){
1713 case 1: 1713 case 1:
1714 s->rgb= 1; 1714 s->rgb= 1;
1715 s->pegasus_rct=0; 1715 s->pegasus_rct=0;
1716 break; 1716 break;
1726 } 1726 }
1727 1727
1728 /* Apple MJPEG-A */ 1728 /* Apple MJPEG-A */
1729 if ((s->start_code == APP1) && (len > (0x28 - 8))) 1729 if ((s->start_code == APP1) && (len > (0x28 - 8)))
1730 { 1730 {
1731 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); 1731 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
1732 id = be2me_32(id); 1732 id = be2me_32(id);
1733 len -= 4; 1733 len -= 4;
1734 if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */ 1734 if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */
1735 { 1735 {
1736 #if 0 1736 #if 0
1737 skip_bits(&s->gb, 32); /* field size */ 1737 skip_bits(&s->gb, 32); /* field size */
1738 skip_bits(&s->gb, 32); /* pad field size */ 1738 skip_bits(&s->gb, 32); /* pad field size */
1739 skip_bits(&s->gb, 32); /* next off */ 1739 skip_bits(&s->gb, 32); /* next off */
1740 skip_bits(&s->gb, 32); /* quant off */ 1740 skip_bits(&s->gb, 32); /* quant off */
1741 skip_bits(&s->gb, 32); /* huff off */ 1741 skip_bits(&s->gb, 32); /* huff off */
1742 skip_bits(&s->gb, 32); /* image off */ 1742 skip_bits(&s->gb, 32); /* image off */
1743 skip_bits(&s->gb, 32); /* scan off */ 1743 skip_bits(&s->gb, 32); /* scan off */
1744 skip_bits(&s->gb, 32); /* data off */ 1744 skip_bits(&s->gb, 32); /* data off */
1745 #endif 1745 #endif
1746 if (s->avctx->debug & FF_DEBUG_PICT_INFO) 1746 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1747 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n"); 1747 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1748 } 1748 }
1749 } 1749 }
1750 1750
1751 out: 1751 out:
1752 /* slow but needed for extreme adobe jpegs */ 1752 /* slow but needed for extreme adobe jpegs */
1753 if (len < 0) 1753 if (len < 0)
1754 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n"); 1754 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
1755 while(--len > 0) 1755 while(--len > 0)
1756 skip_bits(&s->gb, 8); 1756 skip_bits(&s->gb, 8);
1757 1757
1758 return 0; 1758 return 0;
1759 } 1759 }
1760 1760
1761 static int mjpeg_decode_com(MJpegDecodeContext *s) 1761 static int mjpeg_decode_com(MJpegDecodeContext *s)
1762 { 1762 {
1763 int len = get_bits(&s->gb, 16); 1763 int len = get_bits(&s->gb, 16);
1764 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) { 1764 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
1765 uint8_t *cbuf = av_malloc(len - 1); 1765 uint8_t *cbuf = av_malloc(len - 1);
1766 if (cbuf) { 1766 if (cbuf) {
1767 int i; 1767 int i;
1768 for (i = 0; i < len - 2; i++) 1768 for (i = 0; i < len - 2; i++)
1769 cbuf[i] = get_bits(&s->gb, 8); 1769 cbuf[i] = get_bits(&s->gb, 8);
1770 if (i > 0 && cbuf[i-1] == '\n') 1770 if (i > 0 && cbuf[i-1] == '\n')
1771 cbuf[i-1] = 0; 1771 cbuf[i-1] = 0;
1772 else 1772 else
1773 cbuf[i] = 0; 1773 cbuf[i] = 0;
1774 1774
1775 if(s->avctx->debug & FF_DEBUG_PICT_INFO) 1775 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1776 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf); 1776 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
1777 1777
1778 /* buggy avid, it puts EOI only at every 10th frame */ 1778 /* buggy avid, it puts EOI only at every 10th frame */
1779 if (!strcmp(cbuf, "AVID")) 1779 if (!strcmp(cbuf, "AVID"))
1780 { 1780 {
1781 s->buggy_avid = 1; 1781 s->buggy_avid = 1;
1782 // if (s->first_picture) 1782 // if (s->first_picture)
1783 // printf("mjpeg: workarounding buggy AVID\n"); 1783 // printf("mjpeg: workarounding buggy AVID\n");
1784 } 1784 }
1785 else if(!strcmp(cbuf, "CS=ITU601")){ 1785 else if(!strcmp(cbuf, "CS=ITU601")){
1786 s->cs_itu601= 1; 1786 s->cs_itu601= 1;
1787 } 1787 }
1788 1788
1789 av_free(cbuf); 1789 av_free(cbuf);
1790 } 1790 }
1791 } 1791 }
1792 1792
1793 return 0; 1793 return 0;
1794 } 1794 }
1795 1795
1828 #endif 1828 #endif
1829 1829
1830 buf_ptr = *pbuf_ptr; 1830 buf_ptr = *pbuf_ptr;
1831 while (buf_ptr < buf_end) { 1831 while (buf_ptr < buf_end) {
1832 v = *buf_ptr++; 1832 v = *buf_ptr++;
1833 v2 = *buf_ptr; 1833 v2 = *buf_ptr;
1834 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) { 1834 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1835 val = *buf_ptr++; 1835 val = *buf_ptr++;
1836 goto found; 1836 goto found;
1837 } 1837 }
1838 #ifdef DEBUG 1838 #ifdef DEBUG
1839 skipped++; 1839 skipped++;
1840 #endif 1840 #endif
1841 } 1841 }
1842 val = -1; 1842 val = -1;
1843 found: 1843 found:
1844 #ifdef DEBUG 1844 #ifdef DEBUG
1860 buf_ptr = buf; 1860 buf_ptr = buf;
1861 buf_end = buf + buf_size; 1861 buf_end = buf + buf_size;
1862 while (buf_ptr < buf_end) { 1862 while (buf_ptr < buf_end) {
1863 /* find start next marker */ 1863 /* find start next marker */
1864 start_code = find_marker(&buf_ptr, buf_end); 1864 start_code = find_marker(&buf_ptr, buf_end);
1865 { 1865 {
1866 /* EOF */ 1866 /* EOF */
1867 if (start_code < 0) { 1867 if (start_code < 0) {
1868 goto the_end; 1868 goto the_end;
1869 } else { 1869 } else {
1870 dprintf("marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr); 1870 dprintf("marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr);
1871 1871
1872 if ((buf_end - buf_ptr) > s->buffer_size) 1872 if ((buf_end - buf_ptr) > s->buffer_size)
1873 { 1873 {
1874 av_free(s->buffer); 1874 av_free(s->buffer);
1875 s->buffer_size = buf_end-buf_ptr; 1875 s->buffer_size = buf_end-buf_ptr;
1876 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE); 1876 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
1877 dprintf("buffer too small, expanding to %d bytes\n", 1877 dprintf("buffer too small, expanding to %d bytes\n",
1878 s->buffer_size); 1878 s->buffer_size);
1879 } 1879 }
1880 1880
1881 /* unescape buffer of SOS */ 1881 /* unescape buffer of SOS */
1882 if (start_code == SOS) 1882 if (start_code == SOS)
1883 { 1883 {
1884 uint8_t *src = buf_ptr; 1884 uint8_t *src = buf_ptr;
1885 uint8_t *dst = s->buffer; 1885 uint8_t *dst = s->buffer;
1886 1886
1887 while (src<buf_end) 1887 while (src<buf_end)
1888 { 1888 {
1889 uint8_t x = *(src++); 1889 uint8_t x = *(src++);
1890 1890
1891 *(dst++) = x; 1891 *(dst++) = x;
1892 if (x == 0xff) 1892 if (x == 0xff)
1893 { 1893 {
1894 while(src<buf_end && x == 0xff) 1894 while(src<buf_end && x == 0xff)
1895 x = *(src++); 1895 x = *(src++);
1896 1896
1897 if (x >= 0xd0 && x <= 0xd7) 1897 if (x >= 0xd0 && x <= 0xd7)
1898 *(dst++) = x; 1898 *(dst++) = x;
1899 else if (x) 1899 else if (x)
1900 break; 1900 break;
1901 } 1901 }
1902 } 1902 }
1903 init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8); 1903 init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
1904 1904
1905 dprintf("escaping removed %d bytes\n", 1905 dprintf("escaping removed %d bytes\n",
1906 (buf_end - buf_ptr) - (dst - s->buffer)); 1906 (buf_end - buf_ptr) - (dst - s->buffer));
1907 } 1907 }
1908 else 1908 else
1909 init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8); 1909 init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
1910 1910
1911 s->start_code = start_code; 1911 s->start_code = start_code;
1912 if(s->avctx->debug & FF_DEBUG_STARTCODE){ 1912 if(s->avctx->debug & FF_DEBUG_STARTCODE){
1913 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code); 1913 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1914 } 1914 }
1915 1915
1916 /* process markers */ 1916 /* process markers */
1917 if (start_code >= 0xd0 && start_code <= 0xd7) { 1917 if (start_code >= 0xd0 && start_code <= 0xd7) {
1918 dprintf("restart marker: %d\n", start_code&0x0f); 1918 dprintf("restart marker: %d\n", start_code&0x0f);
1919 /* APP fields */ 1919 /* APP fields */
1920 } else if (start_code >= APP0 && start_code <= APP15) { 1920 } else if (start_code >= APP0 && start_code <= APP15) {
1921 mjpeg_decode_app(s); 1921 mjpeg_decode_app(s);
1922 /* Comment */ 1922 /* Comment */
1923 } else if (start_code == COM){ 1923 } else if (start_code == COM){
1924 mjpeg_decode_com(s); 1924 mjpeg_decode_com(s);
1925 } 1925 }
1926 1926
1927 switch(start_code) { 1927 switch(start_code) {
1928 case SOI: 1928 case SOI:
1929 s->restart_interval = 0; 1929 s->restart_interval = 0;
1930 reset_ls_coding_parameters(s, 1); 1930 reset_ls_coding_parameters(s, 1);
1931 1931
1932 s->restart_count = 0; 1932 s->restart_count = 0;
1933 /* nothing to do on SOI */ 1933 /* nothing to do on SOI */
1934 break; 1934 break;
1935 case DQT: 1935 case DQT:
1936 mjpeg_decode_dqt(s); 1936 mjpeg_decode_dqt(s);
1937 break; 1937 break;
1942 } 1942 }
1943 break; 1943 break;
1944 case SOF0: 1944 case SOF0:
1945 s->lossless=0; 1945 s->lossless=0;
1946 if (mjpeg_decode_sof(s) < 0) 1946 if (mjpeg_decode_sof(s) < 0)
1947 return -1; 1947 return -1;
1948 break; 1948 break;
1949 case SOF3: 1949 case SOF3:
1950 s->lossless=1; 1950 s->lossless=1;
1951 if (mjpeg_decode_sof(s) < 0) 1951 if (mjpeg_decode_sof(s) < 0)
1952 return -1; 1952 return -1;
1953 break; 1953 break;
1954 case SOF48: 1954 case SOF48:
1955 s->lossless=1; 1955 s->lossless=1;
1956 s->ls=1; 1956 s->ls=1;
1957 if (mjpeg_decode_sof(s) < 0) 1957 if (mjpeg_decode_sof(s) < 0)
1959 break; 1959 break;
1960 case LSE: 1960 case LSE:
1961 if (decode_lse(s) < 0) 1961 if (decode_lse(s) < 0)
1962 return -1; 1962 return -1;
1963 break; 1963 break;
1964 case EOI: 1964 case EOI:
1965 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) 1965 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1966 break; 1966 break;
1967 eoi_parser: 1967 eoi_parser:
1968 { 1968 {
1969 if (s->interlaced) { 1969 if (s->interlaced) {
1970 s->bottom_field ^= 1; 1970 s->bottom_field ^= 1;
1971 /* if not bottom field, do not output image yet */ 1971 /* if not bottom field, do not output image yet */
1972 if (s->bottom_field) 1972 if (s->bottom_field)
1973 goto not_the_end; 1973 goto not_the_end;
1985 picture->quality*= FF_QP2LAMBDA; 1985 picture->quality*= FF_QP2LAMBDA;
1986 } 1986 }
1987 1987
1988 goto the_end; 1988 goto the_end;
1989 } 1989 }
1990 break; 1990 break;
1991 case SOS: 1991 case SOS:
1992 mjpeg_decode_sos(s); 1992 mjpeg_decode_sos(s);
1993 /* buggy avid puts EOI every 10-20th frame */ 1993 /* buggy avid puts EOI every 10-20th frame */
1994 /* if restart period is over process EOI */ 1994 /* if restart period is over process EOI */
1995 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) 1995 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1996 goto eoi_parser; 1996 goto eoi_parser;
1997 break; 1997 break;
1998 case DRI: 1998 case DRI:
1999 mjpeg_decode_dri(s); 1999 mjpeg_decode_dri(s);
2000 break; 2000 break;
2001 case SOF1: 2001 case SOF1:
2002 case SOF2: 2002 case SOF2:
2003 case SOF5: 2003 case SOF5:
2004 case SOF6: 2004 case SOF6:
2005 case SOF7: 2005 case SOF7:
2006 case SOF9: 2006 case SOF9:
2007 case SOF10: 2007 case SOF10:
2008 case SOF11: 2008 case SOF11:
2009 case SOF13: 2009 case SOF13:
2010 case SOF14: 2010 case SOF14:
2011 case SOF15: 2011 case SOF15:
2012 case JPG: 2012 case JPG:
2013 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code); 2013 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
2014 break; 2014 break;
2015 // default: 2015 // default:
2016 // printf("mjpeg: unsupported marker (%x)\n", start_code); 2016 // printf("mjpeg: unsupported marker (%x)\n", start_code);
2017 // break; 2017 // break;
2018 } 2018 }
2019 2019
2020 not_the_end: 2020 not_the_end:
2021 /* eof process start code */ 2021 /* eof process start code */
2022 buf_ptr += (get_bits_count(&s->gb)+7)/8; 2022 buf_ptr += (get_bits_count(&s->gb)+7)/8;
2023 dprintf("marker parser used %d bytes (%d bits)\n", 2023 dprintf("marker parser used %d bytes (%d bits)\n",
2024 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb)); 2024 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
2025 } 2025 }
2026 } 2026 }
2027 } 2027 }
2028 the_end: 2028 the_end:
2029 dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr); 2029 dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
2055 2055
2056 skip_bits(&hgb, 32); /* reserved zeros */ 2056 skip_bits(&hgb, 32); /* reserved zeros */
2057 2057
2058 if (get_bits_long(&hgb, 32) != be2me_32(ff_get_fourcc("mjpg"))) 2058 if (get_bits_long(&hgb, 32) != be2me_32(ff_get_fourcc("mjpg")))
2059 { 2059 {
2060 dprintf("not mjpeg-b (bad fourcc)\n"); 2060 dprintf("not mjpeg-b (bad fourcc)\n");
2061 return 0; 2061 return 0;
2062 } 2062 }
2063 2063
2064 field_size = get_bits_long(&hgb, 32); /* field size */ 2064 field_size = get_bits_long(&hgb, 32); /* field size */
2065 dprintf("field size: 0x%x\n", field_size); 2065 dprintf("field size: 0x%x\n", field_size);
2066 skip_bits(&hgb, 32); /* padded field size */ 2066 skip_bits(&hgb, 32); /* padded field size */
2067 second_field_offs = get_bits_long(&hgb, 32); 2067 second_field_offs = get_bits_long(&hgb, 32);
2068 dprintf("second field offs: 0x%x\n", second_field_offs); 2068 dprintf("second field offs: 0x%x\n", second_field_offs);
2069 if (second_field_offs) 2069 if (second_field_offs)
2070 s->interlaced = 1; 2070 s->interlaced = 1;
2071 2071
2072 dqt_offs = get_bits_long(&hgb, 32); 2072 dqt_offs = get_bits_long(&hgb, 32);
2073 dprintf("dqt offs: 0x%x\n", dqt_offs); 2073 dprintf("dqt offs: 0x%x\n", dqt_offs);
2074 if (dqt_offs) 2074 if (dqt_offs)
2075 { 2075 {
2076 init_get_bits(&s->gb, buf+dqt_offs, (buf_end - (buf+dqt_offs))*8); 2076 init_get_bits(&s->gb, buf+dqt_offs, (buf_end - (buf+dqt_offs))*8);
2077 s->start_code = DQT; 2077 s->start_code = DQT;
2078 mjpeg_decode_dqt(s); 2078 mjpeg_decode_dqt(s);
2079 } 2079 }
2080 2080
2081 dht_offs = get_bits_long(&hgb, 32); 2081 dht_offs = get_bits_long(&hgb, 32);
2082 dprintf("dht offs: 0x%x\n", dht_offs); 2082 dprintf("dht offs: 0x%x\n", dht_offs);
2083 if (dht_offs) 2083 if (dht_offs)
2084 { 2084 {
2085 init_get_bits(&s->gb, buf+dht_offs, (buf_end - (buf+dht_offs))*8); 2085 init_get_bits(&s->gb, buf+dht_offs, (buf_end - (buf+dht_offs))*8);
2086 s->start_code = DHT; 2086 s->start_code = DHT;
2087 mjpeg_decode_dht(s); 2087 mjpeg_decode_dht(s);
2088 } 2088 }
2089 2089
2090 sof_offs = get_bits_long(&hgb, 32); 2090 sof_offs = get_bits_long(&hgb, 32);
2091 dprintf("sof offs: 0x%x\n", sof_offs); 2091 dprintf("sof offs: 0x%x\n", sof_offs);
2092 if (sof_offs) 2092 if (sof_offs)
2093 { 2093 {
2094 init_get_bits(&s->gb, buf+sof_offs, (buf_end - (buf+sof_offs))*8); 2094 init_get_bits(&s->gb, buf+sof_offs, (buf_end - (buf+sof_offs))*8);
2095 s->start_code = SOF0; 2095 s->start_code = SOF0;
2096 if (mjpeg_decode_sof(s) < 0) 2096 if (mjpeg_decode_sof(s) < 0)
2097 return -1; 2097 return -1;
2098 } 2098 }
2099 2099
2100 sos_offs = get_bits_long(&hgb, 32); 2100 sos_offs = get_bits_long(&hgb, 32);
2101 dprintf("sos offs: 0x%x\n", sos_offs); 2101 dprintf("sos offs: 0x%x\n", sos_offs);
2102 sod_offs = get_bits_long(&hgb, 32); 2102 sod_offs = get_bits_long(&hgb, 32);
2103 dprintf("sod offs: 0x%x\n", sod_offs); 2103 dprintf("sod offs: 0x%x\n", sod_offs);
2104 if (sos_offs) 2104 if (sos_offs)
2105 { 2105 {
2106 // init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8); 2106 // init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8);
2107 init_get_bits(&s->gb, buf+sos_offs, field_size*8); 2107 init_get_bits(&s->gb, buf+sos_offs, field_size*8);
2108 s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16)); 2108 s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
2109 s->start_code = SOS; 2109 s->start_code = SOS;
2110 mjpeg_decode_sos(s); 2110 mjpeg_decode_sos(s);
2111 } 2111 }
2112 2112
2113 if (s->interlaced) { 2113 if (s->interlaced) {
2114 s->bottom_field ^= 1; 2114 s->bottom_field ^= 1;
2115 /* if not bottom field, do not output image yet */ 2115 /* if not bottom field, do not output image yet */
2116 if (s->bottom_field && second_field_offs) 2116 if (s->bottom_field && second_field_offs)
2117 { 2117 {
2118 buf_ptr = buf + second_field_offs; 2118 buf_ptr = buf + second_field_offs;
2119 second_field_offs = 0; 2119 second_field_offs = 0;
2120 goto read_header; 2120 goto read_header;
2121 } 2121 }
2122 } 2122 }
2123 2123
2124 //XXX FIXME factorize, this looks very similar to the EOI code 2124 //XXX FIXME factorize, this looks very similar to the EOI code
2125 2125
2126 *picture= s->picture; 2126 *picture= s->picture;
2151 const int qscale = 5; 2151 const int qscale = 5;
2152 uint8_t *buf_ptr, *buf_end, *recoded; 2152 uint8_t *buf_ptr, *buf_end, *recoded;
2153 int i = 0, j = 0; 2153 int i = 0, j = 0;
2154 2154
2155 if (!avctx->width || !avctx->height) 2155 if (!avctx->width || !avctx->height)
2156 return -1; 2156 return -1;
2157 2157
2158 buf_ptr = buf; 2158 buf_ptr = buf;
2159 buf_end = buf + buf_size; 2159 buf_end = buf + buf_size;
2160 2160
2161 #if 1 2161 #if 1
2162 recoded = av_mallocz(buf_size + 1024); 2162 recoded = av_mallocz(buf_size + 1024);
2163 if (!recoded) 2163 if (!recoded)
2164 return -1; 2164 return -1;
2165 2165
2166 /* SOI */ 2166 /* SOI */
2167 recoded[j++] = 0xFF; 2167 recoded[j++] = 0xFF;
2168 recoded[j++] = 0xD8; 2168 recoded[j++] = 0xD8;
2169 2169
2185 memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos)); 2185 memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
2186 j += sizeof(sp5x_data_sos); 2186 j += sizeof(sp5x_data_sos);
2187 2187
2188 for (i = 14; i < buf_size && j < buf_size+1024-2; i++) 2188 for (i = 14; i < buf_size && j < buf_size+1024-2; i++)
2189 { 2189 {
2190 recoded[j++] = buf[i]; 2190 recoded[j++] = buf[i];
2191 if (buf[i] == 0xff) 2191 if (buf[i] == 0xff)
2192 recoded[j++] = 0; 2192 recoded[j++] = 0;
2193 } 2193 }
2194 2194
2195 /* EOI */ 2195 /* EOI */
2196 recoded[j++] = 0xFF; 2196 recoded[j++] = 0xFF;
2197 recoded[j++] = 0xD9; 2197 recoded[j++] = 0xD9;
2227 2227
2228 s->picture.reference = 0; 2228 s->picture.reference = 0;
2229 if (avctx->get_buffer(avctx, &s->picture) < 0) 2229 if (avctx->get_buffer(avctx, &s->picture) < 0)
2230 { 2230 {
2231 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 2231 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2232 return -1; 2232 return -1;
2233 } 2233 }
2234 2234
2235 s->picture.pict_type = I_TYPE; 2235 s->picture.pict_type = I_TYPE;
2236 s->picture.key_frame = 1; 2236 s->picture.key_frame = 1;
2237 2237
2238 for (i = 0; i < 3; i++) 2238 for (i = 0; i < 3; i++)
2239 s->linesize[i] = s->picture.linesize[i] << s->interlaced; 2239 s->linesize[i] = s->picture.linesize[i] << s->interlaced;
2240 2240
2241 /* DQT */ 2241 /* DQT */
2242 for (i = 0; i < 64; i++) 2242 for (i = 0; i < 64; i++)
2243 { 2243 {
2244 j = s->scantable.permutated[i]; 2244 j = s->scantable.permutated[i];
2245 s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i]; 2245 s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i];
2246 } 2246 }
2247 s->qscale[0] = FFMAX( 2247 s->qscale[0] = FFMAX(
2248 s->quant_matrixes[0][s->scantable.permutated[1]], 2248 s->quant_matrixes[0][s->scantable.permutated[1]],
2249 s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1; 2249 s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1;
2250 2250
2251 for (i = 0; i < 64; i++) 2251 for (i = 0; i < 64; i++)
2252 { 2252 {
2253 j = s->scantable.permutated[i]; 2253 j = s->scantable.permutated[i];
2254 s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i]; 2254 s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i];
2255 } 2255 }
2256 s->qscale[1] = FFMAX( 2256 s->qscale[1] = FFMAX(
2257 s->quant_matrixes[1][s->scantable.permutated[1]], 2257 s->quant_matrixes[1][s->scantable.permutated[1]],
2258 s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1; 2258 s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1;
2259 2259
2260 /* DHT */ 2260 /* DHT */
2261 2261
2262 /* SOS */ 2262 /* SOS */
2263 s->comp_index[0] = 0; 2263 s->comp_index[0] = 0;
2280 s->v_scount[2] = s->v_count[2]; 2280 s->v_scount[2] = s->v_count[2];
2281 s->dc_index[2] = 1; 2281 s->dc_index[2] = 1;
2282 s->ac_index[2] = 1; 2282 s->ac_index[2] = 1;
2283 2283
2284 for (i = 0; i < 3; i++) 2284 for (i = 0; i < 3; i++)
2285 s->last_dc[i] = 1024; 2285 s->last_dc[i] = 1024;
2286 2286
2287 s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8); 2287 s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8);
2288 s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8); 2288 s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8);
2289 2289
2290 init_get_bits(&s->gb, buf+14, (buf_size-14)*8); 2290 init_get_bits(&s->gb, buf+14, (buf_size-14)*8);