icodec.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Microsoft Windows ICO demuxer
  3. * Copyright (c) 2011 Peter Ross (pross@xvid.org)
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Microsoft Windows ICO demuxer
  24. */
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavcodec/bytestream.h"
  27. #include "libavcodec/bmp.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. typedef struct {
  31. int offset;
  32. int size;
  33. int nb_pal;
  34. } IcoImage;
  35. typedef struct {
  36. int current_image;
  37. int nb_images;
  38. IcoImage * images;
  39. } IcoDemuxContext;
  40. static int probe(AVProbeData *p)
  41. {
  42. if (AV_RL16(p->buf) == 0 && AV_RL16(p->buf + 2) == 1 && AV_RL16(p->buf + 4))
  43. return AVPROBE_SCORE_MAX / 3;
  44. return 0;
  45. }
  46. static int read_header(AVFormatContext *s)
  47. {
  48. IcoDemuxContext *ico = s->priv_data;
  49. AVIOContext *pb = s->pb;
  50. int i;
  51. avio_skip(pb, 4);
  52. ico->nb_images = avio_rl16(pb);
  53. ico->images = av_malloc(ico->nb_images * sizeof(IcoImage));
  54. if (!ico->images)
  55. return AVERROR(ENOMEM);
  56. for (i = 0; i < ico->nb_images; i++) {
  57. AVStream *st;
  58. int tmp;
  59. if (avio_seek(pb, 6 + i * 16, SEEK_SET) < 0)
  60. break;
  61. st = avformat_new_stream(s, NULL);
  62. if (!st)
  63. return AVERROR(ENOMEM);
  64. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  65. st->codec->width = avio_r8(pb);
  66. st->codec->height = avio_r8(pb);
  67. ico->images[i].nb_pal = avio_r8(pb);
  68. if (ico->images[i].nb_pal == 255)
  69. ico->images[i].nb_pal = 0;
  70. avio_skip(pb, 5);
  71. ico->images[i].size = avio_rl32(pb);
  72. ico->images[i].offset = avio_rl32(pb);
  73. if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
  74. break;
  75. switch(avio_rl32(pb)) {
  76. case MKTAG(0x89, 'P', 'N', 'G'):
  77. st->codec->codec_id = AV_CODEC_ID_PNG;
  78. st->codec->width = 0;
  79. st->codec->height = 0;
  80. break;
  81. case 40:
  82. if (ico->images[i].size < 40)
  83. return AVERROR_INVALIDDATA;
  84. st->codec->codec_id = AV_CODEC_ID_BMP;
  85. tmp = avio_rl32(pb);
  86. if (tmp)
  87. st->codec->width = tmp;
  88. tmp = avio_rl32(pb);
  89. if (tmp)
  90. st->codec->height = tmp / 2;
  91. break;
  92. default:
  93. av_log_ask_for_sample(s, "unsupported codec\n");
  94. return AVERROR_INVALIDDATA;
  95. }
  96. }
  97. return 0;
  98. }
  99. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  100. {
  101. IcoDemuxContext *ico = s->priv_data;
  102. IcoImage *image;
  103. AVIOContext *pb = s->pb;
  104. AVStream *st = s->streams[0];
  105. int ret;
  106. if (ico->current_image >= ico->nb_images)
  107. return AVERROR(EIO);
  108. image = &ico->images[ico->current_image];
  109. if ((ret = avio_seek(pb, image->offset, SEEK_SET)) < 0)
  110. return ret;
  111. if (s->streams[ico->current_image]->codec->codec_id == AV_CODEC_ID_PNG) {
  112. if ((ret = av_get_packet(pb, pkt, image->size)) < 0)
  113. return ret;
  114. } else {
  115. uint8_t *buf;
  116. if ((ret = av_new_packet(pkt, 14 + image->size)) < 0)
  117. return ret;
  118. buf = pkt->data;
  119. /* add BMP header */
  120. bytestream_put_byte(&buf, 'B');
  121. bytestream_put_byte(&buf, 'M');
  122. bytestream_put_le32(&buf, pkt->size);
  123. bytestream_put_le16(&buf, 0);
  124. bytestream_put_le16(&buf, 0);
  125. bytestream_put_le32(&buf, 0);
  126. if ((ret = avio_read(pb, buf, image->size)) < 0)
  127. return ret;
  128. st->codec->bits_per_coded_sample = AV_RL16(buf + 14);
  129. if (AV_RL32(buf + 32))
  130. image->nb_pal = AV_RL32(buf + 32);
  131. if (st->codec->bits_per_coded_sample <= 8 && !image->nb_pal) {
  132. image->nb_pal = 1 << st->codec->bits_per_coded_sample;
  133. AV_WL32(buf + 32, image->nb_pal);
  134. }
  135. AV_WL32(buf - 4, 14 + 40 + image->nb_pal * 4);
  136. AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
  137. }
  138. pkt->stream_index = ico->current_image++;
  139. pkt->flags |= AV_PKT_FLAG_KEY;
  140. return 0;
  141. }
  142. AVInputFormat ff_ico_demuxer = {
  143. .name = "ico",
  144. .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
  145. .priv_data_size = sizeof(IcoDemuxContext),
  146. .read_probe = probe,
  147. .read_header = read_header,
  148. .read_packet = read_packet,
  149. .flags = AVFMT_NOTIMESTAMPS,
  150. };