FFmpeg
Loading...
Searching...
No Matches
icodec.c
Go to the documentation of this file.
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
28#include "libavutil/mem.h"
30#include "libavcodec/png.h"
31#include "avformat.h"
32#include "demux.h"
33#include "internal.h"
34
35typedef struct {
36 int offset;
37 int size;
38 int nb_pal;
39} IcoImage;
40
46
47static int probe(const AVProbeData *p)
48{
49 unsigned i, frames, checked = 0;
50
51 if (p->buf_size < 22 || AV_RL16(p->buf) || AV_RL16(p->buf + 2) != 1)
52 return 0;
53 frames = AV_RL16(p->buf + 4);
54 if (!frames)
55 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
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)
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
103 if (!st)
104 return AVERROR(ENOMEM);
105
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 || ico->images[i].size > INT_MAX - 14) {
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'):
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;
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
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 - 40U)
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
215{
216 IcoDemuxContext *ico = s->priv_data;
217 av_freep(&ico->images);
218 return 0;
219}
220
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,
231};
static int probe(const AVProbeData *p)
Definition act.c:39
const FFInputFormat ff_ico_demuxer
Definition icodec.c:221
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:498
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_PNG
Definition codec_id.h:111
@ AV_CODEC_ID_BMP
Definition codec_id.h:128
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static int ico_read_close(AVFormatContext *s)
Definition icodec.c:214
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition icodec.c:152
static int read_header(AVFormatContext *s)
Definition icodec.c:79
static int probe(const AVProbeData *p)
Definition icodec.c:47
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_RB64(p)
#define AV_RL16(p)
unsigned offset
Definition libaomenc.c:763
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
Memory handling functions.
#define PNGSIG
Definition png.h:49
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
IcoImage * images
Definition icodec.c:44
int current_image
Definition icodec.c:42
int nb_pal
Definition icodec.c:38
int offset
Definition icodec.c:36
int size
Definition icodec.c:37
#define av_malloc_array(a, b)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
static int frames
Definition movenc.c:67