]> git.sesse.net Git - ffmpeg/blob - libavformat/icodec.c
avcodec/dvbsubdec: prefer to use variable instead of type for sizeof
[ffmpeg] / libavformat / icodec.c
1 /*
2  * Microsoft Windows ICO demuxer
3  * Copyright (c) 2011 Peter Ross (pross@xvid.org)
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * Microsoft Windows ICO demuxer
25  */
26
27 #include "libavutil/intreadwrite.h"
28 #include "libavcodec/bytestream.h"
29 #include "libavcodec/bmp.h"
30 #include "libavcodec/png.h"
31 #include "avformat.h"
32 #include "internal.h"
33
34 typedef struct {
35     int offset;
36     int size;
37     int nb_pal;
38 } IcoImage;
39
40 typedef struct {
41     int current_image;
42     int nb_images;
43     IcoImage * images;
44 } IcoDemuxContext;
45
46 static int probe(const AVProbeData *p)
47 {
48     unsigned i, frames, checked = 0;
49
50     if (p->buf_size < 22 || AV_RL16(p->buf) || AV_RL16(p->buf + 2) != 1)
51         return 0;
52     frames = AV_RL16(p->buf + 4);
53     if (!frames)
54         return 0;
55     for (i = 0; i < frames && i * 16 + 22 <= p->buf_size; i++) {
56         unsigned offset;
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);
64         if (offset < 22)
65             return FFMIN(i, AVPROBE_SCORE_MAX / 4);
66         if (offset > p->buf_size - 8)
67             continue;
68         if (p->buf[offset] != 40 && AV_RB64(p->buf + offset) != PNGSIG)
69             return FFMIN(i, AVPROBE_SCORE_MAX / 4);
70         checked++;
71     }
72
73     if (checked < frames)
74         return AVPROBE_SCORE_MAX / 4 + FFMIN(checked, 1);
75     return AVPROBE_SCORE_MAX / 2 + 1;
76 }
77
78 static int read_header(AVFormatContext *s)
79 {
80     IcoDemuxContext *ico = s->priv_data;
81     AVIOContext *pb = s->pb;
82     int i, codec;
83
84     avio_skip(pb, 4);
85     ico->nb_images = avio_rl16(pb);
86
87     ico->images = av_malloc_array(ico->nb_images, sizeof(IcoImage));
88     if (!ico->images)
89         return AVERROR(ENOMEM);
90
91     for (i = 0; i < ico->nb_images; i++) {
92         AVStream *st;
93         int tmp;
94
95         if (avio_seek(pb, 6 + i * 16, SEEK_SET) < 0)
96             break;
97
98         st = avformat_new_stream(s, NULL);
99         if (!st) {
100             av_freep(&ico->images);
101             return AVERROR(ENOMEM);
102         }
103
104         st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
105         st->codecpar->width      = avio_r8(pb);
106         st->codecpar->height     = avio_r8(pb);
107         ico->images[i].nb_pal = avio_r8(pb);
108         if (ico->images[i].nb_pal == 255)
109             ico->images[i].nb_pal = 0;
110
111         avio_skip(pb, 5);
112
113         ico->images[i].size   = avio_rl32(pb);
114         if (ico->images[i].size <= 0) {
115             av_log(s, AV_LOG_ERROR, "Invalid image size %d\n", ico->images[i].size);
116             av_freep(&ico->images);
117             return AVERROR_INVALIDDATA;
118         }
119         ico->images[i].offset = avio_rl32(pb);
120
121         if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
122             break;
123
124         codec = avio_rl32(pb);
125         switch (codec) {
126         case MKTAG(0x89, 'P', 'N', 'G'):
127             st->codecpar->codec_id = AV_CODEC_ID_PNG;
128             st->codecpar->width    = 0;
129             st->codecpar->height   = 0;
130             break;
131         case 40:
132             if (ico->images[i].size < 40) {
133                 av_freep(&ico->images);
134                 return AVERROR_INVALIDDATA;
135             }
136             st->codecpar->codec_id = AV_CODEC_ID_BMP;
137             tmp = avio_rl32(pb);
138             if (tmp)
139                 st->codecpar->width = tmp;
140             tmp = avio_rl32(pb);
141             if (tmp)
142                 st->codecpar->height = tmp / 2;
143             break;
144         default:
145             avpriv_request_sample(s, "codec %d", codec);
146             av_freep(&ico->images);
147             return AVERROR_INVALIDDATA;
148         }
149     }
150
151     return 0;
152 }
153
154 static int read_packet(AVFormatContext *s, AVPacket *pkt)
155 {
156     IcoDemuxContext *ico = s->priv_data;
157     IcoImage *image;
158     AVIOContext *pb = s->pb;
159     AVStream *st = s->streams[0];
160     int ret;
161
162     if (ico->current_image >= ico->nb_images)
163         return AVERROR_EOF;
164
165     image = &ico->images[ico->current_image];
166
167     if ((ret = avio_seek(pb, image->offset, SEEK_SET)) < 0)
168         return ret;
169
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)
172             return ret;
173     } else {
174         uint8_t *buf;
175         if ((ret = av_new_packet(pkt, 14 + image->size)) < 0)
176             return ret;
177         buf = pkt->data;
178
179         /* add BMP header */
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);
186
187         if ((ret = avio_read(pb, buf, image->size)) != image->size) {
188             return ret < 0 ? ret : AVERROR_INVALIDDATA;
189         }
190
191         st->codecpar->bits_per_coded_sample = AV_RL16(buf + 14);
192
193         if (AV_RL32(buf + 32))
194             image->nb_pal = AV_RL32(buf + 32);
195
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);
199         }
200
201         AV_WL32(buf - 4, 14 + 40 + image->nb_pal * 4);
202         AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
203     }
204
205     pkt->stream_index = ico->current_image++;
206     pkt->flags |= AV_PKT_FLAG_KEY;
207
208     return 0;
209 }
210
211 static int ico_read_close(AVFormatContext * s)
212 {
213     IcoDemuxContext *ico = s->priv_data;
214     av_freep(&ico->images);
215     return 0;
216 }
217
218 AVInputFormat ff_ico_demuxer = {
219     .name           = "ico",
220     .long_name      = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
221     .priv_data_size = sizeof(IcoDemuxContext),
222     .read_probe     = probe,
223     .read_header    = read_header,
224     .read_packet    = read_packet,
225     .read_close     = ico_read_close,
226     .flags          = AVFMT_NOTIMESTAMPS,
227 };