2 * Microsoft Windows ICO demuxer
3 * Copyright (c) 2011 Peter Ross (pross@xvid.org)
5 * This file is part of FFmpeg.
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.
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.
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
24 * Microsoft Windows ICO demuxer
27 #include "libavutil/intreadwrite.h"
28 #include "libavcodec/bytestream.h"
29 #include "libavcodec/bmp.h"
30 #include "libavcodec/png.h"
46 static int probe(AVProbeData *p)
48 unsigned i, frames = AV_RL16(p->buf + 4);
50 if (AV_RL16(p->buf) || AV_RL16(p->buf + 2) != 1 || !frames)
52 for (i = 0; i < frames; i++) {
54 if (AV_RL16(p->buf + 10 + i * 16) & ~1)
55 return FFMIN(i, AVPROBE_SCORE_MAX / 4);
56 if (p->buf[13 + i * 16])
57 return FFMIN(i, AVPROBE_SCORE_MAX / 4);
58 if (AV_RL32(p->buf + 14 + i * 16) < 40)
59 return FFMIN(i, AVPROBE_SCORE_MAX / 4);
60 offset = AV_RL32(p->buf + 18 + i * 16);
62 return FFMIN(i, AVPROBE_SCORE_MAX / 4);
63 if (offset + 8 > p->buf_size)
64 return AVPROBE_SCORE_MAX / 4 + FFMIN(i, 1);
65 if (p->buf[offset] != 40 && AV_RB64(p->buf + offset) != PNGSIG)
66 return FFMIN(i, AVPROBE_SCORE_MAX / 4);
67 if (i * 16 + 6 > p->buf_size)
68 return AVPROBE_SCORE_MAX / 4 + FFMIN(i, 1);
71 return AVPROBE_SCORE_MAX / 2 + 1;
74 static int read_header(AVFormatContext *s)
76 IcoDemuxContext *ico = s->priv_data;
77 AVIOContext *pb = s->pb;
81 ico->nb_images = avio_rl16(pb);
83 ico->images = av_malloc_array(ico->nb_images, sizeof(IcoImage));
85 return AVERROR(ENOMEM);
87 for (i = 0; i < ico->nb_images; i++) {
91 if (avio_seek(pb, 6 + i * 16, SEEK_SET) < 0)
94 st = avformat_new_stream(s, NULL);
96 return AVERROR(ENOMEM);
98 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
99 st->codec->width = avio_r8(pb);
100 st->codec->height = avio_r8(pb);
101 ico->images[i].nb_pal = avio_r8(pb);
102 if (ico->images[i].nb_pal == 255)
103 ico->images[i].nb_pal = 0;
107 ico->images[i].size = avio_rl32(pb);
108 ico->images[i].offset = avio_rl32(pb);
110 if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
113 codec = avio_rl32(pb);
115 case MKTAG(0x89, 'P', 'N', 'G'):
116 st->codec->codec_id = AV_CODEC_ID_PNG;
117 st->codec->width = 0;
118 st->codec->height = 0;
121 if (ico->images[i].size < 40)
122 return AVERROR_INVALIDDATA;
123 st->codec->codec_id = AV_CODEC_ID_BMP;
126 st->codec->width = tmp;
129 st->codec->height = tmp / 2;
132 avpriv_request_sample(s, "codec %d", codec);
133 return AVERROR_INVALIDDATA;
140 static int read_packet(AVFormatContext *s, AVPacket *pkt)
142 IcoDemuxContext *ico = s->priv_data;
144 AVIOContext *pb = s->pb;
145 AVStream *st = s->streams[0];
148 if (ico->current_image >= ico->nb_images)
151 image = &ico->images[ico->current_image];
153 if ((ret = avio_seek(pb, image->offset, SEEK_SET)) < 0)
156 if (s->streams[ico->current_image]->codec->codec_id == AV_CODEC_ID_PNG) {
157 if ((ret = av_get_packet(pb, pkt, image->size)) < 0)
161 if ((ret = av_new_packet(pkt, 14 + image->size)) < 0)
166 bytestream_put_byte(&buf, 'B');
167 bytestream_put_byte(&buf, 'M');
168 bytestream_put_le32(&buf, pkt->size);
169 bytestream_put_le16(&buf, 0);
170 bytestream_put_le16(&buf, 0);
171 bytestream_put_le32(&buf, 0);
173 if ((ret = avio_read(pb, buf, image->size)) < 0)
176 st->codec->bits_per_coded_sample = AV_RL16(buf + 14);
178 if (AV_RL32(buf + 32))
179 image->nb_pal = AV_RL32(buf + 32);
181 if (st->codec->bits_per_coded_sample <= 8 && !image->nb_pal) {
182 image->nb_pal = 1 << st->codec->bits_per_coded_sample;
183 AV_WL32(buf + 32, image->nb_pal);
186 AV_WL32(buf - 4, 14 + 40 + image->nb_pal * 4);
187 AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
190 pkt->stream_index = ico->current_image++;
191 pkt->flags |= AV_PKT_FLAG_KEY;
196 AVInputFormat ff_ico_demuxer = {
198 .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
199 .priv_data_size = sizeof(IcoDemuxContext),
201 .read_header = read_header,
202 .read_packet = read_packet,
203 .flags = AVFMT_NOTIMESTAMPS,