2011-12-04 06:45:24 +00:00
|
|
|
/*
|
|
|
|
* Microsoft Windows ICO demuxer
|
|
|
|
* Copyright (c) 2011 Peter Ross (pross@xvid.org)
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Microsoft Windows ICO demuxer
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavcodec/bytestream.h"
|
2016-01-17 23:36:44 +00:00
|
|
|
#include "libavcodec/png.h"
|
2011-12-04 06:45:24 +00:00
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int offset;
|
|
|
|
int size;
|
|
|
|
int nb_pal;
|
|
|
|
} IcoImage;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int current_image;
|
|
|
|
int nb_images;
|
|
|
|
IcoImage * images;
|
|
|
|
} IcoDemuxContext;
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int probe(const AVProbeData *p)
|
2011-12-04 06:45:24 +00:00
|
|
|
{
|
2016-02-16 07:52:12 +00:00
|
|
|
unsigned i, frames, checked = 0;
|
2016-01-17 23:36:44 +00:00
|
|
|
|
2016-02-16 07:52:12 +00:00
|
|
|
if (p->buf_size < 22 || AV_RL16(p->buf) || AV_RL16(p->buf + 2) != 1)
|
2016-01-17 23:36:44 +00:00
|
|
|
return 0;
|
2016-02-16 07:52:12 +00:00
|
|
|
frames = AV_RL16(p->buf + 4);
|
|
|
|
if (!frames)
|
|
|
|
return 0;
|
|
|
|
for (i = 0; i < frames && i * 16 + 22 <= p->buf_size; i++) {
|
2016-01-17 23:36:44 +00:00
|
|
|
unsigned offset;
|
|
|
|
if (AV_RL16(p->buf + 10 + i * 16) & ~1)
|
|
|
|
return FFMIN(i, AVPROBE_SCORE_MAX / 4);
|
|
|
|
if (p->buf[13 + i * 16])
|
|
|
|
return FFMIN(i, AVPROBE_SCORE_MAX / 4);
|
|
|
|
if (AV_RL32(p->buf + 14 + i * 16) < 40)
|
|
|
|
return FFMIN(i, AVPROBE_SCORE_MAX / 4);
|
|
|
|
offset = AV_RL32(p->buf + 18 + i * 16);
|
|
|
|
if (offset < 22)
|
|
|
|
return FFMIN(i, AVPROBE_SCORE_MAX / 4);
|
2016-02-16 07:52:13 +00:00
|
|
|
if (offset > p->buf_size - 8)
|
2016-02-16 07:52:12 +00:00
|
|
|
continue;
|
2016-01-17 23:36:44 +00:00
|
|
|
if (p->buf[offset] != 40 && AV_RB64(p->buf + offset) != PNGSIG)
|
|
|
|
return FFMIN(i, AVPROBE_SCORE_MAX / 4);
|
2016-02-16 07:52:12 +00:00
|
|
|
checked++;
|
2016-01-17 23:36:44 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 07:52:12 +00:00
|
|
|
if (checked < frames)
|
|
|
|
return AVPROBE_SCORE_MAX / 4 + FFMIN(checked, 1);
|
2016-01-17 23:36:44 +00:00
|
|
|
return AVPROBE_SCORE_MAX / 2 + 1;
|
2011-12-04 06:45:24 +00:00
|
|
|
}
|
|
|
|
|
2012-01-28 03:23:26 +00:00
|
|
|
static int read_header(AVFormatContext *s)
|
2011-12-04 06:45:24 +00:00
|
|
|
{
|
|
|
|
IcoDemuxContext *ico = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
2013-03-15 17:13:52 +00:00
|
|
|
int i, codec;
|
2011-12-04 06:45:24 +00:00
|
|
|
|
|
|
|
avio_skip(pb, 4);
|
|
|
|
ico->nb_images = avio_rl16(pb);
|
|
|
|
|
2020-11-04 00:06:46 +00:00
|
|
|
if (!ico->nb_images)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2014-04-16 17:41:41 +00:00
|
|
|
ico->images = av_malloc_array(ico->nb_images, sizeof(IcoImage));
|
2011-12-04 06:45:24 +00:00
|
|
|
if (!ico->images)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (i = 0; i < ico->nb_images; i++) {
|
|
|
|
AVStream *st;
|
2012-01-13 08:59:29 +00:00
|
|
|
int tmp;
|
2011-12-04 06:45:24 +00:00
|
|
|
|
|
|
|
if (avio_seek(pb, 6 + i * 16, SEEK_SET) < 0)
|
2020-03-21 17:31:06 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-12-04 06:45:24 +00:00
|
|
|
|
|
|
|
st = avformat_new_stream(s, NULL);
|
2020-03-21 17:31:06 +00:00
|
|
|
if (!st)
|
2011-12-04 06:45:24 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
st->codecpar->width = avio_r8(pb);
|
|
|
|
st->codecpar->height = avio_r8(pb);
|
2011-12-04 06:45:24 +00:00
|
|
|
ico->images[i].nb_pal = avio_r8(pb);
|
2012-01-12 12:41:38 +00:00
|
|
|
if (ico->images[i].nb_pal == 255)
|
|
|
|
ico->images[i].nb_pal = 0;
|
2011-12-04 06:45:24 +00:00
|
|
|
|
2012-01-12 12:41:50 +00:00
|
|
|
avio_skip(pb, 5);
|
2011-12-04 06:45:24 +00:00
|
|
|
|
|
|
|
ico->images[i].size = avio_rl32(pb);
|
2016-11-08 22:29:28 +00:00
|
|
|
if (ico->images[i].size <= 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid image size %d\n", ico->images[i].size);
|
2020-03-21 17:31:06 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2016-11-08 22:29:28 +00:00
|
|
|
}
|
2011-12-04 06:45:24 +00:00
|
|
|
ico->images[i].offset = avio_rl32(pb);
|
|
|
|
|
|
|
|
if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
|
2020-03-21 17:31:06 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-12-04 06:45:24 +00:00
|
|
|
|
2013-03-15 17:13:52 +00:00
|
|
|
codec = avio_rl32(pb);
|
|
|
|
switch (codec) {
|
2011-12-04 06:45:24 +00:00
|
|
|
case MKTAG(0x89, 'P', 'N', 'G'):
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_PNG;
|
|
|
|
st->codecpar->width = 0;
|
|
|
|
st->codecpar->height = 0;
|
2011-12-04 06:45:24 +00:00
|
|
|
break;
|
|
|
|
case 40:
|
2020-03-21 17:31:06 +00:00
|
|
|
if (ico->images[i].size < 40)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_BMP;
|
2012-01-13 08:59:29 +00:00
|
|
|
tmp = avio_rl32(pb);
|
|
|
|
if (tmp)
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->width = tmp;
|
2012-01-13 08:59:29 +00:00
|
|
|
tmp = avio_rl32(pb);
|
|
|
|
if (tmp)
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->height = tmp / 2;
|
2011-12-04 06:45:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-03-15 17:13:52 +00:00
|
|
|
avpriv_request_sample(s, "codec %d", codec);
|
2020-03-21 17:31:06 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-12-04 06:45:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
IcoDemuxContext *ico = s->priv_data;
|
|
|
|
IcoImage *image;
|
|
|
|
AVIOContext *pb = s->pb;
|
2020-10-16 17:05:23 +00:00
|
|
|
AVStream *st;
|
2011-12-04 06:45:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (ico->current_image >= ico->nb_images)
|
2015-08-21 14:46:16 +00:00
|
|
|
return AVERROR_EOF;
|
2011-12-04 06:45:24 +00:00
|
|
|
|
2020-10-16 17:05:23 +00:00
|
|
|
st = s->streams[0];
|
|
|
|
|
2011-12-04 06:45:24 +00:00
|
|
|
image = &ico->images[ico->current_image];
|
|
|
|
|
|
|
|
if ((ret = avio_seek(pb, image->offset, SEEK_SET)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2016-04-10 19:58:15 +00:00
|
|
|
if (s->streams[ico->current_image]->codecpar->codec_id == AV_CODEC_ID_PNG) {
|
2011-12-04 06:45:24 +00:00
|
|
|
if ((ret = av_get_packet(pb, pkt, image->size)) < 0)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
uint8_t *buf;
|
|
|
|
if ((ret = av_new_packet(pkt, 14 + image->size)) < 0)
|
|
|
|
return ret;
|
|
|
|
buf = pkt->data;
|
|
|
|
|
|
|
|
/* add BMP header */
|
|
|
|
bytestream_put_byte(&buf, 'B');
|
|
|
|
bytestream_put_byte(&buf, 'M');
|
|
|
|
bytestream_put_le32(&buf, pkt->size);
|
|
|
|
bytestream_put_le16(&buf, 0);
|
|
|
|
bytestream_put_le16(&buf, 0);
|
2012-01-12 12:41:50 +00:00
|
|
|
bytestream_put_le32(&buf, 0);
|
2011-12-04 06:45:24 +00:00
|
|
|
|
2016-11-08 22:29:28 +00:00
|
|
|
if ((ret = avio_read(pb, buf, image->size)) != image->size) {
|
|
|
|
return ret < 0 ? ret : AVERROR_INVALIDDATA;
|
2016-11-08 22:53:52 +00:00
|
|
|
}
|
2011-12-04 06:45:24 +00:00
|
|
|
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->bits_per_coded_sample = AV_RL16(buf + 14);
|
2012-01-12 12:41:50 +00:00
|
|
|
|
|
|
|
if (AV_RL32(buf + 32))
|
|
|
|
image->nb_pal = AV_RL32(buf + 32);
|
|
|
|
|
2016-04-10 19:58:15 +00:00
|
|
|
if (st->codecpar->bits_per_coded_sample <= 8 && !image->nb_pal) {
|
|
|
|
image->nb_pal = 1 << st->codecpar->bits_per_coded_sample;
|
2012-01-12 12:41:50 +00:00
|
|
|
AV_WL32(buf + 32, image->nb_pal);
|
|
|
|
}
|
|
|
|
|
2022-09-17 21:15:56 +00:00
|
|
|
if (image->nb_pal > INT_MAX / 4 - 14 - 40)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-01-12 12:41:50 +00:00
|
|
|
AV_WL32(buf - 4, 14 + 40 + image->nb_pal * 4);
|
2011-12-04 06:45:24 +00:00
|
|
|
AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt->stream_index = ico->current_image++;
|
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-08 22:54:41 +00:00
|
|
|
static int ico_read_close(AVFormatContext * s)
|
|
|
|
{
|
|
|
|
IcoDemuxContext *ico = s->priv_data;
|
|
|
|
av_freep(&ico->images);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:45:24 +00:00
|
|
|
const AVInputFormat ff_ico_demuxer = {
|
2011-12-04 06:45:24 +00:00
|
|
|
.name = "ico",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
|
|
|
|
.priv_data_size = sizeof(IcoDemuxContext),
|
2020-03-21 17:31:06 +00:00
|
|
|
.flags_internal = FF_FMT_INIT_CLEANUP,
|
2011-12-04 06:45:24 +00:00
|
|
|
.read_probe = probe,
|
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
2016-11-08 22:54:41 +00:00
|
|
|
.read_close = ico_read_close,
|
2011-12-04 06:45:24 +00:00
|
|
|
.flags = AVFMT_NOTIMESTAMPS,
|
|
|
|
};
|