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
, checked
= 0;
50 if (p
->buf_size
< 22 || AV_RL16(p
->buf
) || AV_RL16(p
->buf
+ 2) != 1)
52 frames
= AV_RL16(p
->buf
+ 4);
55 for (i
= 0; i
< frames
&& i
* 16 + 22 <= p
->buf_size
; i
++) {
57 if (AV_RL16(p
->buf
+ 10 + i
* 16) & ~1)
58 return FFMIN(i
, AVPROBE_SCORE_MAX
/ 4);
59 if (p
->buf
[13 + i
* 16])
60 return FFMIN(i
, AVPROBE_SCORE_MAX
/ 4);
61 if (AV_RL32(p
->buf
+ 14 + i
* 16) < 40)
62 return FFMIN(i
, AVPROBE_SCORE_MAX
/ 4);
63 offset
= AV_RL32(p
->buf
+ 18 + i
* 16);
65 return FFMIN(i
, AVPROBE_SCORE_MAX
/ 4);
66 if (offset
> p
->buf_size
- 8)
68 if (p
->buf
[offset
] != 40 && AV_RB64(p
->buf
+ offset
) != PNGSIG
)
69 return FFMIN(i
, AVPROBE_SCORE_MAX
/ 4);
74 return AVPROBE_SCORE_MAX
/ 4 + FFMIN(checked
, 1);
75 return AVPROBE_SCORE_MAX
/ 2 + 1;
78 static int read_header(AVFormatContext
*s
)
80 IcoDemuxContext
*ico
= s
->priv_data
;
81 AVIOContext
*pb
= s
->pb
;
85 ico
->nb_images
= avio_rl16(pb
);
88 return AVERROR_INVALIDDATA
;
90 ico
->images
= av_malloc_array(ico
->nb_images
, sizeof(IcoImage
));
92 return AVERROR(ENOMEM
);
94 for (i
= 0; i
< ico
->nb_images
; i
++) {
98 if (avio_seek(pb
, 6 + i
* 16, SEEK_SET
) < 0)
101 st
= avformat_new_stream(s
, NULL
);
103 av_freep(&ico
->images
);
104 return AVERROR(ENOMEM
);
107 st
->codecpar
->codec_type
= AVMEDIA_TYPE_VIDEO
;
108 st
->codecpar
->width
= avio_r8(pb
);
109 st
->codecpar
->height
= avio_r8(pb
);
110 ico
->images
[i
].nb_pal
= avio_r8(pb
);
111 if (ico
->images
[i
].nb_pal
== 255)
112 ico
->images
[i
].nb_pal
= 0;
116 ico
->images
[i
].size
= avio_rl32(pb
);
117 if (ico
->images
[i
].size
<= 0) {
118 av_log(s
, AV_LOG_ERROR
, "Invalid image size %d\n", ico
->images
[i
].size
);
121 ico
->images
[i
].offset
= avio_rl32(pb
);
123 if (avio_seek(pb
, ico
->images
[i
].offset
, SEEK_SET
) < 0)
126 codec
= avio_rl32(pb
);
128 case MKTAG(0x89, 'P', 'N', 'G'):
129 st
->codecpar
->codec_id
= AV_CODEC_ID_PNG
;
130 st
->codecpar
->width
= 0;
131 st
->codecpar
->height
= 0;
134 if (ico
->images
[i
].size
< 40) {
137 st
->codecpar
->codec_id
= AV_CODEC_ID_BMP
;
140 st
->codecpar
->width
= tmp
;
143 st
->codecpar
->height
= tmp
/ 2;
146 avpriv_request_sample(s
, "codec %d", codec
);
153 av_freep(&ico
->images
);
154 return AVERROR_INVALIDDATA
;
157 static int read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
159 IcoDemuxContext
*ico
= s
->priv_data
;
161 AVIOContext
*pb
= s
->pb
;
165 if (ico
->current_image
>= ico
->nb_images
)
170 image
= &ico
->images
[ico
->current_image
];
172 if ((ret
= avio_seek(pb
, image
->offset
, SEEK_SET
)) < 0)
175 if (s
->streams
[ico
->current_image
]->codecpar
->codec_id
== AV_CODEC_ID_PNG
) {
176 if ((ret
= av_get_packet(pb
, pkt
, image
->size
)) < 0)
180 if ((ret
= av_new_packet(pkt
, 14 + image
->size
)) < 0)
185 bytestream_put_byte(&buf
, 'B');
186 bytestream_put_byte(&buf
, 'M');
187 bytestream_put_le32(&buf
, pkt
->size
);
188 bytestream_put_le16(&buf
, 0);
189 bytestream_put_le16(&buf
, 0);
190 bytestream_put_le32(&buf
, 0);
192 if ((ret
= avio_read(pb
, buf
, image
->size
)) != image
->size
) {
193 av_packet_unref(pkt
);
194 return ret
< 0 ? ret
: AVERROR_INVALIDDATA
;
197 st
->codecpar
->bits_per_coded_sample
= AV_RL16(buf
+ 14);
199 if (AV_RL32(buf
+ 32))
200 image
->nb_pal
= AV_RL32(buf
+ 32);
202 if (st
->codecpar
->bits_per_coded_sample
<= 8 && !image
->nb_pal
) {
203 image
->nb_pal
= 1 << st
->codecpar
->bits_per_coded_sample
;
204 AV_WL32(buf
+ 32, image
->nb_pal
);
207 if (image
->nb_pal
> INT_MAX
/ 4 - 14 - 40)
208 return AVERROR_INVALIDDATA
;
210 AV_WL32(buf
- 4, 14 + 40 + image
->nb_pal
* 4);
211 AV_WL32(buf
+ 8, AV_RL32(buf
+ 8) / 2);
214 pkt
->stream_index
= ico
->current_image
++;
215 pkt
->flags
|= AV_PKT_FLAG_KEY
;
220 AVInputFormat ff_ico_demuxer
= {
222 .long_name
= NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
223 .priv_data_size
= sizeof(IcoDemuxContext
),
225 .read_header
= read_header
,
226 .read_packet
= read_packet
,
227 .flags
= AVFMT_NOTIMESTAMPS
,