FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/icodec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 7 117 6.0%
Functions: 1 4 25.0%
Branches: 6 75 8.0%

Line Branch Exec Source
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 "libavutil/mem.h"
29 #include "libavcodec/bytestream.h"
30 #include "libavcodec/png.h"
31 #include "avformat.h"
32 #include "demux.h"
33 #include "internal.h"
34
35 typedef struct {
36 int offset;
37 int size;
38 int nb_pal;
39 } IcoImage;
40
41 typedef struct {
42 int current_image;
43 int nb_images;
44 IcoImage * images;
45 } IcoDemuxContext;
46
47 7186 static int probe(const AVProbeData *p)
48 {
49 7186 unsigned i, frames, checked = 0;
50
51
5/6
✓ Branch 0 taken 7186 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1401 times.
✓ Branch 3 taken 5785 times.
✓ Branch 4 taken 1392 times.
✓ Branch 5 taken 9 times.
7186 if (p->buf_size < 22 || AV_RL16(p->buf) || AV_RL16(p->buf + 2) != 1)
52 7177 return 0;
53 9 frames = AV_RL16(p->buf + 4);
54
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!frames)
55 9 return 0;
56 for (i = 0; i < frames && i * 16 + 22 <= p->buf_size; i++) {
57 unsigned offset;
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);
65 if (offset < 22)
66 return FFMIN(i, AVPROBE_SCORE_MAX / 4);
67 if (offset > p->buf_size - 8)
68 continue;
69 if (p->buf[offset] != 40 && AV_RB64(p->buf + offset) != PNGSIG)
70 return FFMIN(i, AVPROBE_SCORE_MAX / 4);
71 checked++;
72 }
73
74 if (checked < frames)
75 return AVPROBE_SCORE_MAX / 4 + FFMIN(checked, 1);
76 return AVPROBE_SCORE_MAX / 2 + 1;
77 }
78
79 static int read_header(AVFormatContext *s)
80 {
81 IcoDemuxContext *ico = s->priv_data;
82 AVIOContext *pb = s->pb;
83 int i, codec;
84
85 avio_skip(pb, 4);
86 ico->nb_images = avio_rl16(pb);
87
88 if (!ico->nb_images)
89 return AVERROR_INVALIDDATA;
90
91 ico->images = av_malloc_array(ico->nb_images, sizeof(IcoImage));
92 if (!ico->images)
93 return AVERROR(ENOMEM);
94
95 for (i = 0; i < ico->nb_images; i++) {
96 AVStream *st;
97 int tmp;
98
99 if (avio_seek(pb, 6 + i * 16, SEEK_SET) < 0)
100 return AVERROR_INVALIDDATA;
101
102 st = avformat_new_stream(s, NULL);
103 if (!st)
104 return AVERROR(ENOMEM);
105
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;
112
113 avio_skip(pb, 5);
114
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;
119 }
120 ico->images[i].offset = avio_rl32(pb);
121
122 if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
123 return AVERROR_INVALIDDATA;
124
125 codec = avio_rl32(pb);
126 switch (codec) {
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;
131 break;
132 case 40:
133 if (ico->images[i].size < 40)
134 return AVERROR_INVALIDDATA;
135 st->codecpar->codec_id = AV_CODEC_ID_BMP;
136 tmp = avio_rl32(pb);
137 if (tmp)
138 st->codecpar->width = tmp;
139 tmp = avio_rl32(pb);
140 if (tmp)
141 st->codecpar->height = tmp / 2;
142 break;
143 default:
144 avpriv_request_sample(s, "codec %d", codec);
145 return AVERROR_INVALIDDATA;
146 }
147 }
148
149 return 0;
150 }
151
152 static int read_packet(AVFormatContext *s, AVPacket *pkt)
153 {
154 IcoDemuxContext *ico = s->priv_data;
155 IcoImage *image;
156 AVIOContext *pb = s->pb;
157 AVStream *st;
158 int ret;
159
160 if (ico->current_image >= ico->nb_images)
161 return AVERROR_EOF;
162
163 st = s->streams[0];
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 if (image->nb_pal > INT_MAX / 4 - 14 - 40)
202 return AVERROR_INVALIDDATA;
203
204 AV_WL32(buf - 4, 14 + 40 + image->nb_pal * 4);
205 AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
206 }
207
208 pkt->stream_index = ico->current_image++;
209 pkt->flags |= AV_PKT_FLAG_KEY;
210
211 return 0;
212 }
213
214 static int ico_read_close(AVFormatContext * s)
215 {
216 IcoDemuxContext *ico = s->priv_data;
217 av_freep(&ico->images);
218 return 0;
219 }
220
221 const FFInputFormat ff_ico_demuxer = {
222 .p.name = "ico",
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,
227 .read_probe = probe,
228 .read_header = read_header,
229 .read_packet = read_packet,
230 .read_close = ico_read_close,
231 };
232