FFmpeg
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 
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 {
42  int nb_images;
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 
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  if (!ico->nb_images)
88  return AVERROR_INVALIDDATA;
89 
90  ico->images = av_malloc_array(ico->nb_images, sizeof(IcoImage));
91  if (!ico->images)
92  return AVERROR(ENOMEM);
93 
94  for (i = 0; i < ico->nb_images; i++) {
95  AVStream *st;
96  int tmp;
97 
98  if (avio_seek(pb, 6 + i * 16, SEEK_SET) < 0)
99  goto fail;
100 
101  st = avformat_new_stream(s, NULL);
102  if (!st) {
103  av_freep(&ico->images);
104  return AVERROR(ENOMEM);
105  }
106 
108  st->codecpar->width = avio_r8(pb);
109  st->codecpar->height = avio_r8(pb);
110  ico->images[i].nb_pal = avio_r8(pb);
111  if (ico->images[i].nb_pal == 255)
112  ico->images[i].nb_pal = 0;
113 
114  avio_skip(pb, 5);
115 
116  ico->images[i].size = avio_rl32(pb);
117  if (ico->images[i].size <= 0) {
118  av_log(s, AV_LOG_ERROR, "Invalid image size %d\n", ico->images[i].size);
119  goto fail;
120  }
121  ico->images[i].offset = avio_rl32(pb);
122 
123  if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
124  goto fail;
125 
126  codec = avio_rl32(pb);
127  switch (codec) {
128  case MKTAG(0x89, 'P', 'N', 'G'):
130  st->codecpar->width = 0;
131  st->codecpar->height = 0;
132  break;
133  case 40:
134  if (ico->images[i].size < 40) {
135  goto fail;
136  }
138  tmp = avio_rl32(pb);
139  if (tmp)
140  st->codecpar->width = tmp;
141  tmp = avio_rl32(pb);
142  if (tmp)
143  st->codecpar->height = tmp / 2;
144  break;
145  default:
146  avpriv_request_sample(s, "codec %d", codec);
147  goto fail;
148  }
149  }
150 
151  return 0;
152 fail:
153  av_freep(&ico->images);
154  return AVERROR_INVALIDDATA;
155 }
156 
158 {
159  IcoDemuxContext *ico = s->priv_data;
160  IcoImage *image;
161  AVIOContext *pb = s->pb;
162  AVStream *st;
163  int ret;
164 
165  if (ico->current_image >= ico->nb_images)
166  return AVERROR_EOF;
167 
168  st = s->streams[0];
169 
170  image = &ico->images[ico->current_image];
171 
172  if ((ret = avio_seek(pb, image->offset, SEEK_SET)) < 0)
173  return ret;
174 
175  if (s->streams[ico->current_image]->codecpar->codec_id == AV_CODEC_ID_PNG) {
176  if ((ret = av_get_packet(pb, pkt, image->size)) < 0)
177  return ret;
178  } else {
179  uint8_t *buf;
180  if ((ret = av_new_packet(pkt, 14 + image->size)) < 0)
181  return ret;
182  buf = pkt->data;
183 
184  /* add BMP header */
185  bytestream_put_byte(&buf, 'B');
186  bytestream_put_byte(&buf, 'M');
187  bytestream_put_le32(&buf, pkt->size);
188  bytestream_put_le16(&buf, 0);
189  bytestream_put_le16(&buf, 0);
190  bytestream_put_le32(&buf, 0);
191 
192  if ((ret = avio_read(pb, buf, image->size)) != image->size) {
194  return ret < 0 ? ret : AVERROR_INVALIDDATA;
195  }
196 
198 
199  if (AV_RL32(buf + 32))
200  image->nb_pal = AV_RL32(buf + 32);
201 
202  if (st->codecpar->bits_per_coded_sample <= 8 && !image->nb_pal) {
203  image->nb_pal = 1 << st->codecpar->bits_per_coded_sample;
204  AV_WL32(buf + 32, image->nb_pal);
205  }
206 
207  AV_WL32(buf - 4, 14 + 40 + image->nb_pal * 4);
208  AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
209  }
210 
211  pkt->stream_index = ico->current_image++;
213 
214  return 0;
215 }
216 
218 {
219  IcoDemuxContext *ico = s->priv_data;
220  av_freep(&ico->images);
221  return 0;
222 }
223 
225  .name = "ico",
226  .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
227  .priv_data_size = sizeof(IcoDemuxContext),
228  .read_probe = probe,
233 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
IcoDemuxContext::nb_images
int nb_images
Definition: icodec.c:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4480
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3953
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:366
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:467
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:449
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1509
read_header
static int read_header(AVFormatContext *s)
Definition: icodec.c:78
ff_ico_demuxer
AVInputFormat ff_ico_demuxer
Definition: icodec.c:224
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
fail
#define fail()
Definition: checkasm.h:120
IcoDemuxContext::images
IcoImage * images
Definition: icodec.c:43
frames
if it could not because there are no more frames
Definition: filter_design.txt:266
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:753
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
AVInputFormat
Definition: avformat.h:640
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
ico_read_close
static int ico_read_close(AVFormatContext *s)
Definition: icodec.c:217
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
AV_CODEC_ID_BMP
@ AV_CODEC_ID_BMP
Definition: avcodec.h:296
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
AVCodecParameters::width
int width
Video only.
Definition: avcodec.h:4023
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:90
AV_CODEC_ID_PNG
@ AV_CODEC_ID_PNG
Definition: avcodec.h:279
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
internal.h
bmp.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:769
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: avcodec.h:1478
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:638
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: icodec.c:157
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1483
PNGSIG
#define PNGSIG
Definition: png.h:47
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AVCodecParameters::height
int height
Definition: avcodec.h:4024
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
IcoImage
Definition: icodec.c:34
uint8_t
uint8_t
Definition: audio_convert.c:194
av_get_packet
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:313
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:870
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
avformat.h
IcoImage::offset
int offset
Definition: icodec.c:35
IcoImage::size
int size
Definition: icodec.c:36
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:88
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
IcoDemuxContext::current_image
int current_image
Definition: icodec.c:41
probe
static int probe(const AVProbeData *p)
Definition: icodec.c:46
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:647
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3999
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:39
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
png.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
bytestream.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
IcoDemuxContext
Definition: icodec.c:40
AV_RB64
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:91
IcoImage::nb_pal
int nb_pal
Definition: icodec.c:37