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"
45 static int probe(AVProbeData
*p
)
47 if (AV_RL16(p
->buf
) == 0 && AV_RL16(p
->buf
+ 2) == 1 && AV_RL16(p
->buf
+ 4))
48 return AVPROBE_SCORE_MAX
/ 4;
52 static int read_header(AVFormatContext
*s
)
54 IcoDemuxContext
*ico
= s
->priv_data
;
55 AVIOContext
*pb
= s
->pb
;
59 ico
->nb_images
= avio_rl16(pb
);
61 ico
->images
= av_malloc_array(ico
->nb_images
, sizeof(IcoImage
));
63 return AVERROR(ENOMEM
);
65 for (i
= 0; i
< ico
->nb_images
; i
++) {
69 if (avio_seek(pb
, 6 + i
* 16, SEEK_SET
) < 0)
72 st
= avformat_new_stream(s
, NULL
);
74 return AVERROR(ENOMEM
);
76 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
77 st
->codec
->width
= avio_r8(pb
);
78 st
->codec
->height
= avio_r8(pb
);
79 ico
->images
[i
].nb_pal
= avio_r8(pb
);
80 if (ico
->images
[i
].nb_pal
== 255)
81 ico
->images
[i
].nb_pal
= 0;
85 ico
->images
[i
].size
= avio_rl32(pb
);
86 ico
->images
[i
].offset
= avio_rl32(pb
);
88 if (avio_seek(pb
, ico
->images
[i
].offset
, SEEK_SET
) < 0)
91 codec
= avio_rl32(pb
);
93 case MKTAG(0x89, 'P', 'N', 'G'):
94 st
->codec
->codec_id
= AV_CODEC_ID_PNG
;
96 st
->codec
->height
= 0;
99 if (ico
->images
[i
].size
< 40)
100 return AVERROR_INVALIDDATA
;
101 st
->codec
->codec_id
= AV_CODEC_ID_BMP
;
104 st
->codec
->width
= tmp
;
107 st
->codec
->height
= tmp
/ 2;
110 avpriv_request_sample(s
, "codec %d", codec
);
111 return AVERROR_INVALIDDATA
;
118 static int read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
120 IcoDemuxContext
*ico
= s
->priv_data
;
122 AVIOContext
*pb
= s
->pb
;
123 AVStream
*st
= s
->streams
[0];
126 if (ico
->current_image
>= ico
->nb_images
)
129 image
= &ico
->images
[ico
->current_image
];
131 if ((ret
= avio_seek(pb
, image
->offset
, SEEK_SET
)) < 0)
134 if (s
->streams
[ico
->current_image
]->codec
->codec_id
== AV_CODEC_ID_PNG
) {
135 if ((ret
= av_get_packet(pb
, pkt
, image
->size
)) < 0)
139 if ((ret
= av_new_packet(pkt
, 14 + image
->size
)) < 0)
144 bytestream_put_byte(&buf
, 'B');
145 bytestream_put_byte(&buf
, 'M');
146 bytestream_put_le32(&buf
, pkt
->size
);
147 bytestream_put_le16(&buf
, 0);
148 bytestream_put_le16(&buf
, 0);
149 bytestream_put_le32(&buf
, 0);
151 if ((ret
= avio_read(pb
, buf
, image
->size
)) < 0)
154 st
->codec
->bits_per_coded_sample
= AV_RL16(buf
+ 14);
156 if (AV_RL32(buf
+ 32))
157 image
->nb_pal
= AV_RL32(buf
+ 32);
159 if (st
->codec
->bits_per_coded_sample
<= 8 && !image
->nb_pal
) {
160 image
->nb_pal
= 1 << st
->codec
->bits_per_coded_sample
;
161 AV_WL32(buf
+ 32, image
->nb_pal
);
164 AV_WL32(buf
- 4, 14 + 40 + image
->nb_pal
* 4);
165 AV_WL32(buf
+ 8, AV_RL32(buf
+ 8) / 2);
168 pkt
->stream_index
= ico
->current_image
++;
169 pkt
->flags
|= AV_PKT_FLAG_KEY
;
174 AVInputFormat ff_ico_demuxer
= {
176 .long_name
= NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
177 .priv_data_size
= sizeof(IcoDemuxContext
),
179 .read_header
= read_header
,
180 .read_packet
= read_packet
,
181 .flags
= AVFMT_NOTIMESTAMPS
,