comparison mjpeg.c @ 881:d326091dae9f libavcodec

mjpegb support (need more samples)
author al3x
date Wed, 20 Nov 2002 15:18:11 +0000
parents 4033915880d9
children 07a677389920
comparison
equal deleted inserted replaced
880:1c32039e7215 881:d326091dae9f
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 new decode_frame mechanism 20 * aspecting, new decode_frame mechanism and apple mjpeg-b support
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"
1059 mb_height = (s->height + v * 8 - 1) / (v * 8); 1059 mb_height = (s->height + v * 8 - 1) / (v * 8);
1060 nb_blocks[0] = 1; 1060 nb_blocks[0] = 1;
1061 h_count[0] = 1; 1061 h_count[0] = 1;
1062 v_count[0] = 1; 1062 v_count[0] = 1;
1063 } 1063 }
1064 1064
1065 for(mb_y = 0; mb_y < mb_height; mb_y++) { 1065 for(mb_y = 0; mb_y < mb_height; mb_y++) {
1066 for(mb_x = 0; mb_x < mb_width; mb_x++) { 1066 for(mb_x = 0; mb_x < mb_width; mb_x++) {
1067 for(i=0;i<nb_components;i++) { 1067 for(i=0;i<nb_components;i++) {
1068 UINT8 *ptr; 1068 UINT8 *ptr;
1069 int x, y, c; 1069 int x, y, c;
1096 y++; 1096 y++;
1097 } 1097 }
1098 } 1098 }
1099 } 1099 }
1100 /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */ 1100 /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */
1101 1101 if (s->restart_interval && (s->restart_interval < 1350) &&
1102 if ((s->restart_interval < 1350) && !--s->restart_count) { 1102 !--s->restart_count) {
1103 align_get_bits(&s->gb); 1103 align_get_bits(&s->gb);
1104 skip_bits(&s->gb, 16); /* skip RSTn */ 1104 skip_bits(&s->gb, 16); /* skip RSTn */
1105 for (j=0; j<nb_components; j++) /* reset dc */ 1105 for (j=0; j<nb_components; j++) /* reset dc */
1106 s->last_dc[j] = 1024; 1106 s->last_dc[j] = 1024;
1107 } 1107 }
1371 1371
1372 while (src<buf_end) 1372 while (src<buf_end)
1373 { 1373 {
1374 UINT8 x = *(src++); 1374 UINT8 x = *(src++);
1375 1375
1376 #if 0
1377 if (x == 0xff && *src == 0xff)
1378 break;
1379 #endif
1380 *(dst++) = x; 1376 *(dst++) = x;
1381 if (x == 0xff) 1377 if (x == 0xff)
1382 { 1378 {
1383 while(*src == 0xff) src++; 1379 while(*src == 0xff) src++;
1384 1380
1508 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb)); 1504 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
1509 } 1505 }
1510 } 1506 }
1511 } 1507 }
1512 the_end: 1508 the_end:
1513
1514 dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr); 1509 dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
1515 // return buf_end - buf_ptr; 1510 // return buf_end - buf_ptr;
1516 return buf_ptr - buf; 1511 return buf_ptr - buf;
1517 } 1512 }
1513
1514 static int mjpegb_decode_frame(AVCodecContext *avctx,
1515 void *data, int *data_size,
1516 UINT8 *buf, int buf_size)
1517 {
1518 MJpegDecodeContext *s = avctx->priv_data;
1519 UINT8 *buf_end, *buf_ptr;
1520 int i;
1521 AVPicture *picture = data;
1522 GetBitContext hgb; /* for the header */
1523 uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
1524 uint32_t field_size;
1525
1526 *data_size = 0;
1527
1528 /* no supplementary picture */
1529 if (buf_size == 0)
1530 return 0;
1531
1532 buf_ptr = buf;
1533 buf_end = buf + buf_size;
1534
1535 read_header:
1536 /* reset on every SOI */
1537 s->restart_interval = 0;
1538
1539 init_get_bits(&hgb, buf_ptr, /*buf_size*/buf_end - buf_ptr);
1540
1541 skip_bits(&hgb, 32); /* reserved zeros */
1542
1543 if (get_bits(&hgb, 32) != be2me_32(ff_get_fourcc("mjpg")))
1544 {
1545 dprintf("not mjpeg-b (bad fourcc)\n");
1546 return 0;
1547 }
1548
1549 field_size = get_bits(&hgb, 32); /* field size */
1550 dprintf("field size: 0x%x\n", field_size);
1551 skip_bits(&hgb, 32); /* padded field size */
1552 second_field_offs = get_bits(&hgb, 32);
1553 dprintf("second field offs: 0x%x\n", second_field_offs);
1554 if (second_field_offs)
1555 s->interlaced = 1;
1556
1557 dqt_offs = get_bits(&hgb, 32);
1558 dprintf("dqt offs: 0x%x\n", dqt_offs);
1559 if (dqt_offs)
1560 {
1561 init_get_bits(&s->gb, buf+dqt_offs, buf_end - (buf+dqt_offs));
1562 s->start_code = DQT;
1563 mjpeg_decode_dqt(s);
1564 }
1565
1566 dht_offs = get_bits(&hgb, 32);
1567 dprintf("dht offs: 0x%x\n", dht_offs);
1568 if (dht_offs)
1569 {
1570 init_get_bits(&s->gb, buf+dht_offs, buf_end - (buf+dht_offs));
1571 s->start_code = DHT;
1572 mjpeg_decode_dht(s);
1573 }
1574
1575 sof_offs = get_bits(&hgb, 32);
1576 dprintf("sof offs: 0x%x\n", sof_offs);
1577 if (sof_offs)
1578 {
1579 init_get_bits(&s->gb, buf+sof_offs, buf_end - (buf+sof_offs));
1580 s->start_code = SOF0;
1581 mjpeg_decode_sof0(s);
1582 }
1583
1584 sos_offs = get_bits(&hgb, 32);
1585 dprintf("sos offs: 0x%x\n", sos_offs);
1586 if (sos_offs)
1587 {
1588 // init_get_bits(&s->gb, buf+sos_offs, buf_end - (buf+sos_offs));
1589 init_get_bits(&s->gb, buf+sos_offs, field_size);
1590 s->start_code = SOS;
1591 mjpeg_decode_sos(s);
1592 }
1593
1594 skip_bits(&hgb, 32); /* start of data offset */
1595
1596 if (s->interlaced) {
1597 s->bottom_field ^= 1;
1598 /* if not bottom field, do not output image yet */
1599 if (s->bottom_field && second_field_offs)
1600 {
1601 buf_ptr = buf + second_field_offs;
1602 second_field_offs = 0;
1603 goto read_header;
1604 }
1605 }
1606
1607 for(i=0;i<3;i++) {
1608 picture->data[i] = s->current_picture[i];
1609 picture->linesize[i] = (s->interlaced) ?
1610 s->linesize[i] >> 1 : s->linesize[i];
1611 }
1612 *data_size = sizeof(AVPicture);
1613 avctx->height = s->height;
1614 if (s->interlaced)
1615 avctx->height *= 2;
1616 avctx->width = s->width;
1617 /* XXX: not complete test ! */
1618 switch((s->h_count[0] << 4) | s->v_count[0]) {
1619 case 0x11:
1620 avctx->pix_fmt = PIX_FMT_YUV444P;
1621 break;
1622 case 0x21:
1623 avctx->pix_fmt = PIX_FMT_YUV422P;
1624 break;
1625 default:
1626 case 0x22:
1627 avctx->pix_fmt = PIX_FMT_YUV420P;
1628 break;
1629 }
1630 /* dummy quality */
1631 /* XXX: infer it with matrix */
1632 avctx->quality = 3;
1633
1634 return buf_ptr - buf;
1635 }
1636
1518 1637
1519 static int mjpeg_decode_end(AVCodecContext *avctx) 1638 static int mjpeg_decode_end(AVCodecContext *avctx)
1520 { 1639 {
1521 MJpegDecodeContext *s = avctx->priv_data; 1640 MJpegDecodeContext *s = avctx->priv_data;
1522 int i, j; 1641 int i, j;
1541 mjpeg_decode_end, 1660 mjpeg_decode_end,
1542 mjpeg_decode_frame, 1661 mjpeg_decode_frame,
1543 0, 1662 0,
1544 NULL 1663 NULL
1545 }; 1664 };
1665
1666 AVCodec mjpegb_decoder = {
1667 "mjpegb",
1668 CODEC_TYPE_VIDEO,
1669 CODEC_ID_MJPEGB,
1670 sizeof(MJpegDecodeContext),
1671 mjpeg_decode_init,
1672 NULL,
1673 mjpeg_decode_end,
1674 mjpegb_decode_frame,
1675 0,
1676 NULL
1677 };