00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/intreadwrite.h"
00028 #include "libavcodec/bytestream.h"
00029 #include "libavcodec/bmp.h"
00030 #include "avformat.h"
00031 #include "internal.h"
00032
00033 typedef struct {
00034 int offset;
00035 int size;
00036 int nb_pal;
00037 } IcoImage;
00038
00039 typedef struct {
00040 int current_image;
00041 int nb_images;
00042 IcoImage * images;
00043 } IcoDemuxContext;
00044
00045 static int probe(AVProbeData *p)
00046 {
00047 if (AV_RL16(p->buf) == 0 && AV_RL16(p->buf + 2) == 1 && AV_RL16(p->buf + 4))
00048 return AVPROBE_SCORE_MAX / 3;
00049 return 0;
00050 }
00051
00052 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
00053 {
00054 IcoDemuxContext *ico = s->priv_data;
00055 AVIOContext *pb = s->pb;
00056 int i;
00057
00058 avio_skip(pb, 4);
00059 ico->nb_images = avio_rl16(pb);
00060
00061 ico->images = av_malloc(ico->nb_images * sizeof(IcoImage));
00062 if (!ico->images)
00063 return AVERROR(ENOMEM);
00064
00065 for (i = 0; i < ico->nb_images; i++) {
00066 AVStream *st;
00067
00068 if (avio_seek(pb, 6 + i * 16, SEEK_SET) < 0)
00069 break;
00070
00071 st = avformat_new_stream(s, NULL);
00072 if (!st)
00073 return AVERROR(ENOMEM);
00074
00075 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00076 st->codec->width = avio_r8(pb);
00077 st->codec->height = avio_r8(pb);
00078 ico->images[i].nb_pal = avio_r8(pb);
00079
00080 avio_skip(pb, 3);
00081 st->codec->bits_per_coded_sample = avio_rl16(pb);
00082 if (st->codec->bits_per_coded_sample <= 8 && !ico->images[i].nb_pal)
00083 ico->images[i].nb_pal = 1 << st->codec->bits_per_coded_sample;
00084
00085 ico->images[i].size = avio_rl32(pb);
00086 ico->images[i].offset = avio_rl32(pb);
00087
00088 if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
00089 break;
00090
00091 switch(avio_rl32(pb)) {
00092 case MKTAG(0x89, 'P', 'N', 'G'):
00093 st->codec->codec_id = CODEC_ID_PNG;
00094 st->codec->width = 0;
00095 st->codec->height = 0;
00096 break;
00097 case 40:
00098 st->codec->codec_id = CODEC_ID_BMP;
00099 if (!st->codec->width || !st->codec->height) {
00100 st->codec->width = avio_rl32(pb);
00101 st->codec->height = avio_rl32(pb) / 2;
00102 }
00103 break;
00104 default:
00105 av_log_ask_for_sample(s, "unsupported codec\n");
00106 return AVERROR_INVALIDDATA;
00107 }
00108 }
00109
00110 return 0;
00111 }
00112
00113 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00114 {
00115 IcoDemuxContext *ico = s->priv_data;
00116 IcoImage *image;
00117 AVIOContext *pb = s->pb;
00118 int ret;
00119
00120 if (ico->current_image >= ico->nb_images)
00121 return AVERROR(EIO);
00122
00123 image = &ico->images[ico->current_image];
00124
00125 if ((ret = avio_seek(pb, image->offset, SEEK_SET)) < 0)
00126 return ret;
00127
00128 if (s->streams[ico->current_image]->codec->codec_id == CODEC_ID_PNG) {
00129 if ((ret = av_get_packet(pb, pkt, image->size)) < 0)
00130 return ret;
00131 } else {
00132 uint8_t *buf;
00133 if ((ret = av_new_packet(pkt, 14 + image->size)) < 0)
00134 return ret;
00135 buf = pkt->data;
00136
00137
00138 bytestream_put_byte(&buf, 'B');
00139 bytestream_put_byte(&buf, 'M');
00140 bytestream_put_le32(&buf, pkt->size);
00141 bytestream_put_le16(&buf, 0);
00142 bytestream_put_le16(&buf, 0);
00143 bytestream_put_le32(&buf, 14 + 40 + image->nb_pal * 4);
00144
00145 if ((ret = avio_read(pb, buf, image->size)) < 0)
00146 return ret;
00147
00148 AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
00149 AV_WL32(buf + 32, image->nb_pal);
00150 }
00151
00152 pkt->stream_index = ico->current_image++;
00153 pkt->flags |= AV_PKT_FLAG_KEY;
00154
00155 return 0;
00156 }
00157
00158 AVInputFormat ff_ico_demuxer = {
00159 .name = "ico",
00160 .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
00161 .priv_data_size = sizeof(IcoDemuxContext),
00162 .read_probe = probe,
00163 .read_header = read_header,
00164 .read_packet = read_packet,
00165 .flags = AVFMT_NOTIMESTAMPS,
00166 };