Mercurial > libavcodec.hg
comparison mjpegdec.c @ 5044:924fdd6175b1 libavcodec
move mjpeg-b decoder in its own file
author | aurel |
---|---|
date | Sat, 19 May 2007 14:58:31 +0000 |
parents | 259b58518ecc |
children | 93e369c55c31 |
comparison
equal
deleted
inserted
replaced
5043:41d1e47e30e0 | 5044:924fdd6175b1 |
---|---|
38 #include "mjpeg.h" | 38 #include "mjpeg.h" |
39 #include "mjpegdec.h" | 39 #include "mjpegdec.h" |
40 #include "jpeglsdec.h" | 40 #include "jpeglsdec.h" |
41 | 41 |
42 | 42 |
43 static int mjpeg_decode_dht(MJpegDecodeContext *s); | |
44 | |
45 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, | 43 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, |
46 int nb_codes, int use_static, int is_ac) | 44 int nb_codes, int use_static, int is_ac) |
47 { | 45 { |
48 uint8_t huff_size[256+16]; | 46 uint8_t huff_size[256+16]; |
49 uint16_t huff_code[256+16]; | 47 uint16_t huff_code[256+16]; |
88 | 86 |
89 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF) | 87 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF) |
90 { | 88 { |
91 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n"); | 89 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n"); |
92 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8); | 90 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8); |
93 mjpeg_decode_dht(s); | 91 ff_mjpeg_decode_dht(s); |
94 /* should check for error - but dunno */ | 92 /* should check for error - but dunno */ |
95 } | 93 } |
96 if (avctx->extradata_size > 9 && | 94 if (avctx->extradata_size > 9 && |
97 AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) { | 95 AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) { |
98 if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */ | 96 if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */ |
104 return 0; | 102 return 0; |
105 } | 103 } |
106 | 104 |
107 | 105 |
108 /* quantize tables */ | 106 /* quantize tables */ |
109 static int mjpeg_decode_dqt(MJpegDecodeContext *s) | 107 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s) |
110 { | 108 { |
111 int len, index, i, j; | 109 int len, index, i, j; |
112 | 110 |
113 len = get_bits(&s->gb, 16) - 2; | 111 len = get_bits(&s->gb, 16) - 2; |
114 | 112 |
139 | 137 |
140 return 0; | 138 return 0; |
141 } | 139 } |
142 | 140 |
143 /* decode huffman tables and build VLC decoders */ | 141 /* decode huffman tables and build VLC decoders */ |
144 static int mjpeg_decode_dht(MJpegDecodeContext *s) | 142 int ff_mjpeg_decode_dht(MJpegDecodeContext *s) |
145 { | 143 { |
146 int len, index, i, class, n, v, code_max; | 144 int len, index, i, class, n, v, code_max; |
147 uint8_t bits_table[17]; | 145 uint8_t bits_table[17]; |
148 uint8_t val_table[256]; | 146 uint8_t val_table[256]; |
149 | 147 |
185 } | 183 } |
186 } | 184 } |
187 return 0; | 185 return 0; |
188 } | 186 } |
189 | 187 |
190 static int mjpeg_decode_sof(MJpegDecodeContext *s) | 188 int ff_mjpeg_decode_sof(MJpegDecodeContext *s) |
191 { | 189 { |
192 int len, nb_components, i, width, height, pix_fmt_id; | 190 int len, nb_components, i, width, height, pix_fmt_id; |
193 | 191 |
194 /* XXX: verify len field validity */ | 192 /* XXX: verify len field validity */ |
195 len = get_bits(&s->gb, 16); | 193 len = get_bits(&s->gb, 16); |
706 } | 704 } |
707 } | 705 } |
708 return 0; | 706 return 0; |
709 } | 707 } |
710 | 708 |
711 static int mjpeg_decode_sos(MJpegDecodeContext *s) | 709 int ff_mjpeg_decode_sos(MJpegDecodeContext *s) |
712 { | 710 { |
713 int len, nb_components, i, h, v, predictor, point_transform; | 711 int len, nb_components, i, h, v, predictor, point_transform; |
714 int vmax, hmax, index, id; | 712 int vmax, hmax, index, id; |
715 const int block_size= s->lossless ? 1 : 8; | 713 const int block_size= s->lossless ? 1 : 8; |
716 int ilv, prev_shift; | 714 int ilv, prev_shift; |
1199 | 1197 |
1200 s->restart_count = 0; | 1198 s->restart_count = 0; |
1201 /* nothing to do on SOI */ | 1199 /* nothing to do on SOI */ |
1202 break; | 1200 break; |
1203 case DQT: | 1201 case DQT: |
1204 mjpeg_decode_dqt(s); | 1202 ff_mjpeg_decode_dqt(s); |
1205 break; | 1203 break; |
1206 case DHT: | 1204 case DHT: |
1207 if(mjpeg_decode_dht(s) < 0){ | 1205 if(ff_mjpeg_decode_dht(s) < 0){ |
1208 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n"); | 1206 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n"); |
1209 return -1; | 1207 return -1; |
1210 } | 1208 } |
1211 break; | 1209 break; |
1212 case SOF0: | 1210 case SOF0: |
1213 s->lossless=0; | 1211 s->lossless=0; |
1214 s->ls=0; | 1212 s->ls=0; |
1215 s->progressive=0; | 1213 s->progressive=0; |
1216 if (mjpeg_decode_sof(s) < 0) | 1214 if (ff_mjpeg_decode_sof(s) < 0) |
1217 return -1; | 1215 return -1; |
1218 break; | 1216 break; |
1219 case SOF2: | 1217 case SOF2: |
1220 s->lossless=0; | 1218 s->lossless=0; |
1221 s->ls=0; | 1219 s->ls=0; |
1222 s->progressive=1; | 1220 s->progressive=1; |
1223 if (mjpeg_decode_sof(s) < 0) | 1221 if (ff_mjpeg_decode_sof(s) < 0) |
1224 return -1; | 1222 return -1; |
1225 break; | 1223 break; |
1226 case SOF3: | 1224 case SOF3: |
1227 s->lossless=1; | 1225 s->lossless=1; |
1228 s->ls=0; | 1226 s->ls=0; |
1229 s->progressive=0; | 1227 s->progressive=0; |
1230 if (mjpeg_decode_sof(s) < 0) | 1228 if (ff_mjpeg_decode_sof(s) < 0) |
1231 return -1; | 1229 return -1; |
1232 break; | 1230 break; |
1233 case SOF48: | 1231 case SOF48: |
1234 s->lossless=1; | 1232 s->lossless=1; |
1235 s->ls=1; | 1233 s->ls=1; |
1236 s->progressive=0; | 1234 s->progressive=0; |
1237 if (mjpeg_decode_sof(s) < 0) | 1235 if (ff_mjpeg_decode_sof(s) < 0) |
1238 return -1; | 1236 return -1; |
1239 break; | 1237 break; |
1240 case LSE: | 1238 case LSE: |
1241 if (!ENABLE_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0) | 1239 if (!ENABLE_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0) |
1242 return -1; | 1240 return -1; |
1268 | 1266 |
1269 goto the_end; | 1267 goto the_end; |
1270 } | 1268 } |
1271 break; | 1269 break; |
1272 case SOS: | 1270 case SOS: |
1273 mjpeg_decode_sos(s); | 1271 ff_mjpeg_decode_sos(s); |
1274 /* buggy avid puts EOI every 10-20th frame */ | 1272 /* buggy avid puts EOI every 10-20th frame */ |
1275 /* if restart period is over process EOI */ | 1273 /* if restart period is over process EOI */ |
1276 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) | 1274 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) |
1277 goto eoi_parser; | 1275 goto eoi_parser; |
1278 break; | 1276 break; |
1309 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr); | 1307 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr); |
1310 // return buf_end - buf_ptr; | 1308 // return buf_end - buf_ptr; |
1311 return buf_ptr - buf; | 1309 return buf_ptr - buf; |
1312 } | 1310 } |
1313 | 1311 |
1314 static int mjpegb_decode_frame(AVCodecContext *avctx, | |
1315 void *data, int *data_size, | |
1316 uint8_t *buf, int buf_size) | |
1317 { | |
1318 MJpegDecodeContext *s = avctx->priv_data; | |
1319 uint8_t *buf_end, *buf_ptr; | |
1320 AVFrame *picture = data; | |
1321 GetBitContext hgb; /* for the header */ | |
1322 uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs; | |
1323 uint32_t field_size, sod_offs; | |
1324 | |
1325 buf_ptr = buf; | |
1326 buf_end = buf + buf_size; | |
1327 | |
1328 read_header: | |
1329 /* reset on every SOI */ | |
1330 s->restart_interval = 0; | |
1331 s->restart_count = 0; | |
1332 s->mjpb_skiptosod = 0; | |
1333 | |
1334 init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8); | |
1335 | |
1336 skip_bits(&hgb, 32); /* reserved zeros */ | |
1337 | |
1338 if (get_bits_long(&hgb, 32) != MKBETAG('m','j','p','g')) | |
1339 { | |
1340 av_log(avctx, AV_LOG_WARNING, "not mjpeg-b (bad fourcc)\n"); | |
1341 return 0; | |
1342 } | |
1343 | |
1344 field_size = get_bits_long(&hgb, 32); /* field size */ | |
1345 av_log(avctx, AV_LOG_DEBUG, "field size: 0x%x\n", field_size); | |
1346 skip_bits(&hgb, 32); /* padded field size */ | |
1347 second_field_offs = get_bits_long(&hgb, 32); | |
1348 av_log(avctx, AV_LOG_DEBUG, "second field offs: 0x%x\n", second_field_offs); | |
1349 | |
1350 dqt_offs = get_bits_long(&hgb, 32); | |
1351 av_log(avctx, AV_LOG_DEBUG, "dqt offs: 0x%x\n", dqt_offs); | |
1352 if (dqt_offs) | |
1353 { | |
1354 init_get_bits(&s->gb, buf+dqt_offs, (buf_end - (buf+dqt_offs))*8); | |
1355 s->start_code = DQT; | |
1356 mjpeg_decode_dqt(s); | |
1357 } | |
1358 | |
1359 dht_offs = get_bits_long(&hgb, 32); | |
1360 av_log(avctx, AV_LOG_DEBUG, "dht offs: 0x%x\n", dht_offs); | |
1361 if (dht_offs) | |
1362 { | |
1363 init_get_bits(&s->gb, buf+dht_offs, (buf_end - (buf+dht_offs))*8); | |
1364 s->start_code = DHT; | |
1365 mjpeg_decode_dht(s); | |
1366 } | |
1367 | |
1368 sof_offs = get_bits_long(&hgb, 32); | |
1369 av_log(avctx, AV_LOG_DEBUG, "sof offs: 0x%x\n", sof_offs); | |
1370 if (sof_offs) | |
1371 { | |
1372 init_get_bits(&s->gb, buf+sof_offs, (buf_end - (buf+sof_offs))*8); | |
1373 s->start_code = SOF0; | |
1374 if (mjpeg_decode_sof(s) < 0) | |
1375 return -1; | |
1376 } | |
1377 | |
1378 sos_offs = get_bits_long(&hgb, 32); | |
1379 av_log(avctx, AV_LOG_DEBUG, "sos offs: 0x%x\n", sos_offs); | |
1380 sod_offs = get_bits_long(&hgb, 32); | |
1381 av_log(avctx, AV_LOG_DEBUG, "sod offs: 0x%x\n", sod_offs); | |
1382 if (sos_offs) | |
1383 { | |
1384 // init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8); | |
1385 init_get_bits(&s->gb, buf+sos_offs, field_size*8); | |
1386 s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16)); | |
1387 s->start_code = SOS; | |
1388 mjpeg_decode_sos(s); | |
1389 } | |
1390 | |
1391 if (s->interlaced) { | |
1392 s->bottom_field ^= 1; | |
1393 /* if not bottom field, do not output image yet */ | |
1394 if (s->bottom_field && second_field_offs) | |
1395 { | |
1396 buf_ptr = buf + second_field_offs; | |
1397 second_field_offs = 0; | |
1398 goto read_header; | |
1399 } | |
1400 } | |
1401 | |
1402 //XXX FIXME factorize, this looks very similar to the EOI code | |
1403 | |
1404 *picture= s->picture; | |
1405 *data_size = sizeof(AVFrame); | |
1406 | |
1407 if(!s->lossless){ | |
1408 picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]); | |
1409 picture->qstride= 0; | |
1410 picture->qscale_table= s->qscale_table; | |
1411 memset(picture->qscale_table, picture->quality, (s->width+15)/16); | |
1412 if(avctx->debug & FF_DEBUG_QP) | |
1413 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality); | |
1414 picture->quality*= FF_QP2LAMBDA; | |
1415 } | |
1416 | |
1417 return buf_ptr - buf; | |
1418 } | |
1419 | |
1420 int ff_mjpeg_decode_end(AVCodecContext *avctx) | 1312 int ff_mjpeg_decode_end(AVCodecContext *avctx) |
1421 { | 1313 { |
1422 MJpegDecodeContext *s = avctx->priv_data; | 1314 MJpegDecodeContext *s = avctx->priv_data; |
1423 int i, j; | 1315 int i, j; |
1424 | 1316 |
1455 ff_mjpeg_decode_end, | 1347 ff_mjpeg_decode_end, |
1456 ff_mjpeg_decode_frame, | 1348 ff_mjpeg_decode_frame, |
1457 CODEC_CAP_DR1, | 1349 CODEC_CAP_DR1, |
1458 NULL | 1350 NULL |
1459 }; | 1351 }; |
1460 | |
1461 AVCodec mjpegb_decoder = { | |
1462 "mjpegb", | |
1463 CODEC_TYPE_VIDEO, | |
1464 CODEC_ID_MJPEGB, | |
1465 sizeof(MJpegDecodeContext), | |
1466 ff_mjpeg_decode_init, | |
1467 NULL, | |
1468 ff_mjpeg_decode_end, | |
1469 mjpegb_decode_frame, | |
1470 CODEC_CAP_DR1, | |
1471 NULL | |
1472 }; |