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 "libavutil/mem.h"
29 #include "libavcodec/bytestream.h"
30 #include "libavcodec/png.h"
47 static int probe(const AVProbeData
*p
)
49 unsigned i
, frames
, checked
= 0;
51 if (p
->buf_size
< 22 || AV_RL16(p
->buf
) || AV_RL16(p
->buf
+ 2) != 1)
53 frames
= AV_RL16(p
->buf
+ 4);
56 for (i
= 0; i
< frames
&& i
* 16 + 22 <= p
->buf_size
; i
++) {
58 if (AV_RL16(p
->buf
+ 10 + i
* 16) & ~1)
59 return FFMIN(i
, AVPROBE_SCORE_MAX
/ 4);
60 if (p
->buf
[13 + i
* 16])
61 return FFMIN(i
, AVPROBE_SCORE_MAX
/ 4);
62 if (AV_RL32(p
->buf
+ 14 + i
* 16) < 40)
63 return FFMIN(i
, AVPROBE_SCORE_MAX
/ 4);
64 offset
= AV_RL32(p
->buf
+ 18 + i
* 16);
66 return FFMIN(i
, AVPROBE_SCORE_MAX
/ 4);
67 if (offset
> p
->buf_size
- 8)
69 if (p
->buf
[offset
] != 40 && AV_RB64(p
->buf
+ offset
) != PNGSIG
)
70 return FFMIN(i
, AVPROBE_SCORE_MAX
/ 4);
75 return AVPROBE_SCORE_MAX
/ 4 + FFMIN(checked
, 1);
76 return AVPROBE_SCORE_MAX
/ 2 + 1;
79 static int read_header(AVFormatContext
*s
)
81 IcoDemuxContext
*ico
= s
->priv_data
;
82 AVIOContext
*pb
= s
->pb
;
86 ico
->nb_images
= avio_rl16(pb
);
89 return AVERROR_INVALIDDATA
;
91 ico
->images
= av_malloc_array(ico
->nb_images
, sizeof(IcoImage
));
93 return AVERROR(ENOMEM
);
95 for (i
= 0; i
< ico
->nb_images
; i
++) {
99 if (avio_seek(pb
, 6 + i
* 16, SEEK_SET
) < 0)
100 return AVERROR_INVALIDDATA
;
102 st
= avformat_new_stream(s
, NULL
);
104 return AVERROR(ENOMEM
);
106 st
->codecpar
->codec_type
= AVMEDIA_TYPE_VIDEO
;
107 st
->codecpar
->width
= avio_r8(pb
);
108 st
->codecpar
->height
= avio_r8(pb
);
109 ico
->images
[i
].nb_pal
= avio_r8(pb
);
110 if (ico
->images
[i
].nb_pal
== 255)
111 ico
->images
[i
].nb_pal
= 0;
115 ico
->images
[i
].size
= avio_rl32(pb
);
116 if (ico
->images
[i
].size
<= 0) {
117 av_log(s
, AV_LOG_ERROR
, "Invalid image size %d\n", ico
->images
[i
].size
);
118 return AVERROR_INVALIDDATA
;
120 ico
->images
[i
].offset
= avio_rl32(pb
);
122 if (avio_seek(pb
, ico
->images
[i
].offset
, SEEK_SET
) < 0)
123 return AVERROR_INVALIDDATA
;
125 codec
= avio_rl32(pb
);
127 case MKTAG(0x89, 'P', 'N', 'G'):
128 st
->codecpar
->codec_id
= AV_CODEC_ID_PNG
;
129 st
->codecpar
->width
= 0;
130 st
->codecpar
->height
= 0;
133 if (ico
->images
[i
].size
< 40)
134 return AVERROR_INVALIDDATA
;
135 st
->codecpar
->codec_id
= AV_CODEC_ID_BMP
;
138 st
->codecpar
->width
= tmp
;
141 st
->codecpar
->height
= tmp
/ 2;
144 avpriv_request_sample(s
, "codec %d", codec
);
145 return AVERROR_INVALIDDATA
;
152 static int read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
154 IcoDemuxContext
*ico
= s
->priv_data
;
156 AVIOContext
*pb
= s
->pb
;
160 if (ico
->current_image
>= ico
->nb_images
)
165 image
= &ico
->images
[ico
->current_image
];
167 if ((ret
= avio_seek(pb
, image
->offset
, SEEK_SET
)) < 0)
170 if (s
->streams
[ico
->current_image
]->codecpar
->codec_id
== AV_CODEC_ID_PNG
) {
171 if ((ret
= av_get_packet(pb
, pkt
, image
->size
)) < 0)
175 if ((ret
= av_new_packet(pkt
, 14 + image
->size
)) < 0)
180 bytestream_put_byte(&buf
, 'B');
181 bytestream_put_byte(&buf
, 'M');
182 bytestream_put_le32(&buf
, pkt
->size
);
183 bytestream_put_le16(&buf
, 0);
184 bytestream_put_le16(&buf
, 0);
185 bytestream_put_le32(&buf
, 0);
187 if ((ret
= avio_read(pb
, buf
, image
->size
)) != image
->size
) {
188 return ret
< 0 ? ret
: AVERROR_INVALIDDATA
;
191 st
->codecpar
->bits_per_coded_sample
= AV_RL16(buf
+ 14);
193 if (AV_RL32(buf
+ 32))
194 image
->nb_pal
= AV_RL32(buf
+ 32);
196 if (st
->codecpar
->bits_per_coded_sample
<= 8 && !image
->nb_pal
) {
197 image
->nb_pal
= 1 << st
->codecpar
->bits_per_coded_sample
;
198 AV_WL32(buf
+ 32, image
->nb_pal
);
201 if (image
->nb_pal
> INT_MAX
/ 4 - 14 - 40U)
202 return AVERROR_INVALIDDATA
;
204 AV_WL32(buf
- 4, 14 + 40 + image
->nb_pal
* 4);
205 AV_WL32(buf
+ 8, AV_RL32(buf
+ 8) / 2);
208 pkt
->stream_index
= ico
->current_image
++;
209 pkt
->flags
|= AV_PKT_FLAG_KEY
;
214 static int ico_read_close(AVFormatContext
* s
)
216 IcoDemuxContext
*ico
= s
->priv_data
;
217 av_freep(&ico
->images
);
221 const FFInputFormat ff_ico_demuxer
= {
223 .p
.long_name
= NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
224 .p
.flags
= AVFMT_NOTIMESTAMPS
,
225 .priv_data_size
= sizeof(IcoDemuxContext
),
226 .flags_internal
= FF_INFMT_FLAG_INIT_CLEANUP
,
228 .read_header
= read_header
,
229 .read_packet
= read_packet
,
230 .read_close
= ico_read_close
,