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 "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 {
43  int nb_images;
46 
47 static 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)
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 
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'):
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++;
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 };
IcoDemuxContext::nb_images
int nb_images
Definition: icodec.c:43
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:422
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
read_header
static int read_header(AVFormatContext *s)
Definition: icodec.c:79
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
IcoDemuxContext::images
IcoImage * images
Definition: icodec.c:44
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:143
ff_ico_demuxer
const FFInputFormat ff_ico_demuxer
Definition: icodec.c:221
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
ico_read_close
static int ico_read_close(AVFormatContext *s)
Definition: icodec.c:214
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: packet.c:98
AV_CODEC_ID_BMP
@ AV_CODEC_ID_BMP
Definition: codec_id.h:130
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
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:94
AV_CODEC_ID_PNG
@ AV_CODEC_ID_PNG
Definition: codec_id.h:113
FF_INFMT_FLAG_INIT_CLEANUP
#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
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
NULL
#define NULL
Definition: coverity.c:32
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:540
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:94
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: icodec.c:152
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: packet.h:545
PNGSIG
#define PNGSIG
Definition: png.h:49
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecParameters::height
int height
Definition: codec_par.h:135
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
IcoImage
Definition: icodec.c:35
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
demux.h
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:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
avformat.h
IcoImage::offset
int offset
Definition: icodec.c:36
IcoImage::size
int size
Definition: icodec.c:37
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
IcoDemuxContext::current_image
int current_image
Definition: icodec.c:42
probe
static int probe(const AVProbeData *p)
Definition: icodec.c:47
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
AVPacket::stream_index
int stream_index
Definition: packet.h:541
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
mem.h
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
png.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FFInputFormat
Definition: demux.h:42
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
IcoDemuxContext
Definition: icodec.c:41
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:95
IcoImage::nb_pal
int nb_pal
Definition: icodec.c:38