00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/intreadwrite.h"
00028 #include "libavcodec/bytestream.h"
00029 #include "libavcodec/bmp.h"
00030 #include "avformat.h"
00031 #include "internal.h"
00032
00033 typedef struct {
00034 int offset;
00035 int size;
00036 int nb_pal;
00037 } IcoImage;
00038
00039 typedef struct {
00040 int current_image;
00041 int nb_images;
00042 IcoImage * images;
00043 } IcoDemuxContext;
00044
00045 static int probe(AVProbeData *p)
00046 {
00047 if (AV_RL16(p->buf) == 0 && AV_RL16(p->buf + 2) == 1 && AV_RL16(p->buf + 4))
00048 return AVPROBE_SCORE_MAX / 3;
00049 return 0;
00050 }
00051
00052 static int read_header(AVFormatContext *s)
00053 {
00054 IcoDemuxContext *ico = s->priv_data;
00055 AVIOContext *pb = s->pb;
00056 int i;
00057
00058 avio_skip(pb, 4);
00059 ico->nb_images = avio_rl16(pb);
00060
00061 ico->images = av_malloc(ico->nb_images * sizeof(IcoImage));
00062 if (!ico->images)
00063 return AVERROR(ENOMEM);
00064
00065 for (i = 0; i < ico->nb_images; i++) {
00066 AVStream *st;
00067 int tmp;
00068
00069 if (avio_seek(pb, 6 + i * 16, SEEK_SET) < 0)
00070 break;
00071
00072 st = avformat_new_stream(s, NULL);
00073 if (!st)
00074 return AVERROR(ENOMEM);
00075
00076 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00077 st->codec->width = avio_r8(pb);
00078 st->codec->height = avio_r8(pb);
00079 ico->images[i].nb_pal = avio_r8(pb);
00080 if (ico->images[i].nb_pal == 255)
00081 ico->images[i].nb_pal = 0;
00082
00083 avio_skip(pb, 5);
00084
00085 ico->images[i].size = avio_rl32(pb);
00086 ico->images[i].offset = avio_rl32(pb);
00087
00088 if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
00089 break;
00090
00091 switch(avio_rl32(pb)) {
00092 case MKTAG(0x89, 'P', 'N', 'G'):
00093 st->codec->codec_id = CODEC_ID_PNG;
00094 st->codec->width = 0;
00095 st->codec->height = 0;
00096 break;
00097 case 40:
00098 if (ico->images[i].size < 40)
00099 return AVERROR_INVALIDDATA;
00100 st->codec->codec_id = CODEC_ID_BMP;
00101 tmp = avio_rl32(pb);
00102 if (tmp)
00103 st->codec->width = tmp;
00104 tmp = avio_rl32(pb);
00105 if (tmp)
00106 st->codec->height = tmp / 2;
00107 break;
00108 default:
00109 av_log_ask_for_sample(s, "unsupported codec\n");
00110 return AVERROR_INVALIDDATA;
00111 }
00112 }
00113
00114 return 0;
00115 }
00116
00117 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00118 {
00119 IcoDemuxContext *ico = s->priv_data;
00120 IcoImage *image;
00121 AVIOContext *pb = s->pb;
00122 AVStream *st = s->streams[0];
00123 int ret;
00124
00125 if (ico->current_image >= ico->nb_images)
00126 return AVERROR(EIO);
00127
00128 image = &ico->images[ico->current_image];
00129
00130 if ((ret = avio_seek(pb, image->offset, SEEK_SET)) < 0)
00131 return ret;
00132
00133 if (s->streams[ico->current_image]->codec->codec_id == CODEC_ID_PNG) {
00134 if ((ret = av_get_packet(pb, pkt, image->size)) < 0)
00135 return ret;
00136 } else {
00137 uint8_t *buf;
00138 if ((ret = av_new_packet(pkt, 14 + image->size)) < 0)
00139 return ret;
00140 buf = pkt->data;
00141
00142
00143 bytestream_put_byte(&buf, 'B');
00144 bytestream_put_byte(&buf, 'M');
00145 bytestream_put_le32(&buf, pkt->size);
00146 bytestream_put_le16(&buf, 0);
00147 bytestream_put_le16(&buf, 0);
00148 bytestream_put_le32(&buf, 0);
00149
00150 if ((ret = avio_read(pb, buf, image->size)) < 0)
00151 return ret;
00152
00153 st->codec->bits_per_coded_sample = AV_RL16(buf + 14);
00154
00155 if (AV_RL32(buf + 32))
00156 image->nb_pal = AV_RL32(buf + 32);
00157
00158 if (st->codec->bits_per_coded_sample <= 8 && !image->nb_pal) {
00159 image->nb_pal = 1 << st->codec->bits_per_coded_sample;
00160 AV_WL32(buf + 32, image->nb_pal);
00161 }
00162
00163 AV_WL32(buf - 4, 14 + 40 + image->nb_pal * 4);
00164 AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
00165 }
00166
00167 pkt->stream_index = ico->current_image++;
00168 pkt->flags |= AV_PKT_FLAG_KEY;
00169
00170 return 0;
00171 }
00172
00173 AVInputFormat ff_ico_demuxer = {
00174 .name = "ico",
00175 .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
00176 .priv_data_size = sizeof(IcoDemuxContext),
00177 .read_probe = probe,
00178 .read_header = read_header,
00179 .read_packet = read_packet,
00180 .flags = AVFMT_NOTIMESTAMPS,
00181 };