FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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(AVProbeData *p)
47 {
48  unsigned i, frames = AV_RL16(p->buf + 4);
49 
50  if (AV_RL16(p->buf) || AV_RL16(p->buf + 2) != 1 || !frames)
51  return 0;
52  for (i = 0; i < frames; i++) {
53  unsigned offset;
54  if (AV_RL16(p->buf + 10 + i * 16) & ~1)
55  return FFMIN(i, AVPROBE_SCORE_MAX / 4);
56  if (p->buf[13 + i * 16])
57  return FFMIN(i, AVPROBE_SCORE_MAX / 4);
58  if (AV_RL32(p->buf + 14 + i * 16) < 40)
59  return FFMIN(i, AVPROBE_SCORE_MAX / 4);
60  offset = AV_RL32(p->buf + 18 + i * 16);
61  if (offset < 22)
62  return FFMIN(i, AVPROBE_SCORE_MAX / 4);
63  if (offset + 8 > p->buf_size)
64  return AVPROBE_SCORE_MAX / 4 + FFMIN(i, 1);
65  if (p->buf[offset] != 40 && AV_RB64(p->buf + offset) != PNGSIG)
66  return FFMIN(i, AVPROBE_SCORE_MAX / 4);
67  if (i * 16 + 6 > p->buf_size)
68  return AVPROBE_SCORE_MAX / 4 + FFMIN(i, 1);
69  }
70 
71  return AVPROBE_SCORE_MAX / 2 + 1;
72 }
73 
75 {
76  IcoDemuxContext *ico = s->priv_data;
77  AVIOContext *pb = s->pb;
78  int i, codec;
79 
80  avio_skip(pb, 4);
81  ico->nb_images = avio_rl16(pb);
82 
83  ico->images = av_malloc_array(ico->nb_images, sizeof(IcoImage));
84  if (!ico->images)
85  return AVERROR(ENOMEM);
86 
87  for (i = 0; i < ico->nb_images; i++) {
88  AVStream *st;
89  int tmp;
90 
91  if (avio_seek(pb, 6 + i * 16, SEEK_SET) < 0)
92  break;
93 
94  st = avformat_new_stream(s, NULL);
95  if (!st)
96  return AVERROR(ENOMEM);
97 
99  st->codec->width = avio_r8(pb);
100  st->codec->height = avio_r8(pb);
101  ico->images[i].nb_pal = avio_r8(pb);
102  if (ico->images[i].nb_pal == 255)
103  ico->images[i].nb_pal = 0;
104 
105  avio_skip(pb, 5);
106 
107  ico->images[i].size = avio_rl32(pb);
108  ico->images[i].offset = avio_rl32(pb);
109 
110  if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
111  break;
112 
113  codec = avio_rl32(pb);
114  switch (codec) {
115  case MKTAG(0x89, 'P', 'N', 'G'):
117  st->codec->width = 0;
118  st->codec->height = 0;
119  break;
120  case 40:
121  if (ico->images[i].size < 40)
122  return AVERROR_INVALIDDATA;
124  tmp = avio_rl32(pb);
125  if (tmp)
126  st->codec->width = tmp;
127  tmp = avio_rl32(pb);
128  if (tmp)
129  st->codec->height = tmp / 2;
130  break;
131  default:
132  avpriv_request_sample(s, "codec %d", codec);
133  return AVERROR_INVALIDDATA;
134  }
135  }
136 
137  return 0;
138 }
139 
141 {
142  IcoDemuxContext *ico = s->priv_data;
143  IcoImage *image;
144  AVIOContext *pb = s->pb;
145  AVStream *st = s->streams[0];
146  int ret;
147 
148  if (ico->current_image >= ico->nb_images)
149  return AVERROR_EOF;
150 
151  image = &ico->images[ico->current_image];
152 
153  if ((ret = avio_seek(pb, image->offset, SEEK_SET)) < 0)
154  return ret;
155 
156  if (s->streams[ico->current_image]->codec->codec_id == AV_CODEC_ID_PNG) {
157  if ((ret = av_get_packet(pb, pkt, image->size)) < 0)
158  return ret;
159  } else {
160  uint8_t *buf;
161  if ((ret = av_new_packet(pkt, 14 + image->size)) < 0)
162  return ret;
163  buf = pkt->data;
164 
165  /* add BMP header */
166  bytestream_put_byte(&buf, 'B');
167  bytestream_put_byte(&buf, 'M');
168  bytestream_put_le32(&buf, pkt->size);
169  bytestream_put_le16(&buf, 0);
170  bytestream_put_le16(&buf, 0);
171  bytestream_put_le32(&buf, 0);
172 
173  if ((ret = avio_read(pb, buf, image->size)) < 0)
174  return ret;
175 
176  st->codec->bits_per_coded_sample = AV_RL16(buf + 14);
177 
178  if (AV_RL32(buf + 32))
179  image->nb_pal = AV_RL32(buf + 32);
180 
181  if (st->codec->bits_per_coded_sample <= 8 && !image->nb_pal) {
182  image->nb_pal = 1 << st->codec->bits_per_coded_sample;
183  AV_WL32(buf + 32, image->nb_pal);
184  }
185 
186  AV_WL32(buf - 4, 14 + 40 + image->nb_pal * 4);
187  AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
188  }
189 
190  pkt->stream_index = ico->current_image++;
191  pkt->flags |= AV_PKT_FLAG_KEY;
192 
193  return 0;
194 }
195 
197  .name = "ico",
198  .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
199  .priv_data_size = sizeof(IcoDemuxContext),
200  .read_probe = probe,
204 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int size
Definition: avcodec.h:1468
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:208
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:282
int frames
Definition: movenc-test.c:65
static AVPacket pkt
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
int offset
Definition: icodec.c:35
IcoImage * images
Definition: icodec.c:43
Format I/O context.
Definition: avformat.h:1314
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
static int probe(AVProbeData *p)
Definition: icodec.c:46
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3805
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
uint8_t * data
Definition: avcodec.h:1467
#define AVERROR_EOF
End of file.
Definition: error.h:55
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:252
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2917
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
AVInputFormat ff_ico_demuxer
Definition: icodec.c:196
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
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
int nb_pal
Definition: icodec.c:37
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:667
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
#define PNGSIG
Definition: png.h:52
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1473
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:463
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
#define FFMIN(a, b)
Definition: common.h:96
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1711
Stream structure.
Definition: avformat.h:877
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:485
enum AVMediaType codec_type
Definition: avcodec.h:1540
enum AVCodecID codec_id
Definition: avcodec.h:1549
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
int current_image
Definition: icodec.c:41
void * buf
Definition: avisynth_c.h:553
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: icodec.c:140
static int flags
Definition: cpu.c:47
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:651
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:87
Main libavformat public API header.
int size
Definition: icodec.c:36
int nb_images
Definition: icodec.c:42
void * priv_data
Format private data.
Definition: avformat.h:1342
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
#define av_malloc_array(a, b)
static int read_header(AVFormatContext *s)
Definition: icodec.c:74
int stream_index
Definition: avcodec.h:1469
#define MKTAG(a, b, c, d)
Definition: common.h:342
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1444
#define AV_WL32(p, v)
Definition: intreadwrite.h:426