FFmpeg  2.8.17
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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 "avformat.h"
31 #include "internal.h"
32 
33 typedef struct {
34  int offset;
35  int size;
36  int nb_pal;
37 } IcoImage;
38 
39 typedef struct {
41  int nb_images;
44 
45 static int probe(AVProbeData *p)
46 {
47  if (AV_RL16(p->buf) == 0 && AV_RL16(p->buf + 2) == 1 && AV_RL16(p->buf + 4))
48  return AVPROBE_SCORE_MAX / 4;
49  return 0;
50 }
51 
53 {
54  IcoDemuxContext *ico = s->priv_data;
55  AVIOContext *pb = s->pb;
56  int i, codec;
57 
58  avio_skip(pb, 4);
59  ico->nb_images = avio_rl16(pb);
60 
61  ico->images = av_malloc_array(ico->nb_images, sizeof(IcoImage));
62  if (!ico->images)
63  return AVERROR(ENOMEM);
64 
65  for (i = 0; i < ico->nb_images; i++) {
66  AVStream *st;
67  int tmp;
68 
69  if (avio_seek(pb, 6 + i * 16, SEEK_SET) < 0)
70  break;
71 
72  st = avformat_new_stream(s, NULL);
73  if (!st)
74  return AVERROR(ENOMEM);
75 
77  st->codec->width = avio_r8(pb);
78  st->codec->height = avio_r8(pb);
79  ico->images[i].nb_pal = avio_r8(pb);
80  if (ico->images[i].nb_pal == 255)
81  ico->images[i].nb_pal = 0;
82 
83  avio_skip(pb, 5);
84 
85  ico->images[i].size = avio_rl32(pb);
86  if (ico->images[i].size <= 0) {
87  av_log(s, AV_LOG_ERROR, "Invalid image size %d\n", ico->images[i].size);
88  return AVERROR_INVALIDDATA;
89  }
90  ico->images[i].offset = avio_rl32(pb);
91 
92  if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
93  break;
94 
95  codec = avio_rl32(pb);
96  switch (codec) {
97  case MKTAG(0x89, 'P', 'N', 'G'):
99  st->codec->width = 0;
100  st->codec->height = 0;
101  break;
102  case 40:
103  if (ico->images[i].size < 40)
104  return AVERROR_INVALIDDATA;
106  tmp = avio_rl32(pb);
107  if (tmp)
108  st->codec->width = tmp;
109  tmp = avio_rl32(pb);
110  if (tmp)
111  st->codec->height = tmp / 2;
112  break;
113  default:
114  avpriv_request_sample(s, "codec %d", codec);
115  return AVERROR_INVALIDDATA;
116  }
117  }
118 
119  return 0;
120 }
121 
123 {
124  IcoDemuxContext *ico = s->priv_data;
125  IcoImage *image;
126  AVIOContext *pb = s->pb;
127  AVStream *st = s->streams[0];
128  int ret;
129 
130  if (ico->current_image >= ico->nb_images)
131  return AVERROR(EIO);
132 
133  image = &ico->images[ico->current_image];
134 
135  if ((ret = avio_seek(pb, image->offset, SEEK_SET)) < 0)
136  return ret;
137 
138  if (s->streams[ico->current_image]->codec->codec_id == AV_CODEC_ID_PNG) {
139  if ((ret = av_get_packet(pb, pkt, image->size)) < 0)
140  return ret;
141  } else {
142  uint8_t *buf;
143  if ((ret = av_new_packet(pkt, 14 + image->size)) < 0)
144  return ret;
145  buf = pkt->data;
146 
147  /* add BMP header */
148  bytestream_put_byte(&buf, 'B');
149  bytestream_put_byte(&buf, 'M');
150  bytestream_put_le32(&buf, pkt->size);
151  bytestream_put_le16(&buf, 0);
152  bytestream_put_le16(&buf, 0);
153  bytestream_put_le32(&buf, 0);
154 
155  if ((ret = avio_read(pb, buf, image->size)) != image->size) {
156  av_packet_unref(pkt);
157  return ret < 0 ? ret : AVERROR_INVALIDDATA;
158  }
159 
160  st->codec->bits_per_coded_sample = AV_RL16(buf + 14);
161 
162  if (AV_RL32(buf + 32))
163  image->nb_pal = AV_RL32(buf + 32);
164 
165  if (st->codec->bits_per_coded_sample <= 8 && !image->nb_pal) {
166  image->nb_pal = 1 << st->codec->bits_per_coded_sample;
167  AV_WL32(buf + 32, image->nb_pal);
168  }
169 
170  AV_WL32(buf - 4, 14 + 40 + image->nb_pal * 4);
171  AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
172  }
173 
174  pkt->stream_index = ico->current_image++;
175  pkt->flags |= AV_PKT_FLAG_KEY;
176 
177  return 0;
178 }
179 
181  .name = "ico",
182  .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
183  .priv_data_size = sizeof(IcoDemuxContext),
184  .read_probe = probe,
188 };
#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
#define avpriv_request_sample(...)
int size
Definition: avcodec.h:1434
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:204
#define AV_RL16
Definition: intreadwrite.h:42
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:279
static AVPacket pkt
int offset
Definition: icodec.c:34
IcoImage * images
Definition: icodec.c:42
Format I/O context.
Definition: avformat.h:1285
uint8_t
static int probe(AVProbeData *p)
Definition: icodec.c:45
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3761
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1353
uint8_t * data
Definition: avcodec.h:1433
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:244
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:3006
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:542
AVInputFormat ff_ico_demuxer
Definition: icodec.c:180
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1479
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int nb_pal
Definition: icodec.c:36
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:663
#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:178
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1439
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:533
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:873
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1691
#define AV_RL32
Definition: intreadwrite.h:146
Stream structure.
Definition: avformat.h:854
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:484
enum AVMediaType codec_type
Definition: avcodec.h:1520
enum AVCodecID codec_id
Definition: avcodec.h:1529
AVIOContext * pb
I/O context.
Definition: avformat.h:1327
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:562
int current_image
Definition: icodec.c:40
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:122
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:647
Main libavformat public API header.
int size
Definition: icodec.c:35
int nb_images
Definition: icodec.c:41
void * priv_data
Format private data.
Definition: avformat.h:1313
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:640
#define av_malloc_array(a, b)
static int read_header(AVFormatContext *s)
Definition: icodec.c:52
int stream_index
Definition: avcodec.h:1435
#define MKTAG(a, b, c, d)
Definition: common.h:341
This structure stores compressed data.
Definition: avcodec.h:1410
#define AV_WL32(p, v)
Definition: intreadwrite.h:426