FFmpeg  4.0
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, 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  ico->images = av_malloc_array(ico->nb_images, sizeof(IcoImage));
88  if (!ico->images)
89  return AVERROR(ENOMEM);
90 
91  for (i = 0; i < ico->nb_images; i++) {
92  AVStream *st;
93  int tmp;
94 
95  if (avio_seek(pb, 6 + i * 16, SEEK_SET) < 0)
96  break;
97 
98  st = avformat_new_stream(s, NULL);
99  if (!st)
100  return AVERROR(ENOMEM);
101 
103  st->codecpar->width = avio_r8(pb);
104  st->codecpar->height = avio_r8(pb);
105  ico->images[i].nb_pal = avio_r8(pb);
106  if (ico->images[i].nb_pal == 255)
107  ico->images[i].nb_pal = 0;
108 
109  avio_skip(pb, 5);
110 
111  ico->images[i].size = avio_rl32(pb);
112  if (ico->images[i].size <= 0) {
113  av_log(s, AV_LOG_ERROR, "Invalid image size %d\n", ico->images[i].size);
114  return AVERROR_INVALIDDATA;
115  }
116  ico->images[i].offset = avio_rl32(pb);
117 
118  if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
119  break;
120 
121  codec = avio_rl32(pb);
122  switch (codec) {
123  case MKTAG(0x89, 'P', 'N', 'G'):
125  st->codecpar->width = 0;
126  st->codecpar->height = 0;
127  break;
128  case 40:
129  if (ico->images[i].size < 40)
130  return AVERROR_INVALIDDATA;
132  tmp = avio_rl32(pb);
133  if (tmp)
134  st->codecpar->width = tmp;
135  tmp = avio_rl32(pb);
136  if (tmp)
137  st->codecpar->height = tmp / 2;
138  break;
139  default:
140  avpriv_request_sample(s, "codec %d", codec);
141  return AVERROR_INVALIDDATA;
142  }
143  }
144 
145  return 0;
146 }
147 
149 {
150  IcoDemuxContext *ico = s->priv_data;
151  IcoImage *image;
152  AVIOContext *pb = s->pb;
153  AVStream *st = s->streams[0];
154  int ret;
155 
156  if (ico->current_image >= ico->nb_images)
157  return AVERROR_EOF;
158 
159  image = &ico->images[ico->current_image];
160 
161  if ((ret = avio_seek(pb, image->offset, SEEK_SET)) < 0)
162  return ret;
163 
165  if ((ret = av_get_packet(pb, pkt, image->size)) < 0)
166  return ret;
167  } else {
168  uint8_t *buf;
169  if ((ret = av_new_packet(pkt, 14 + image->size)) < 0)
170  return ret;
171  buf = pkt->data;
172 
173  /* add BMP header */
174  bytestream_put_byte(&buf, 'B');
175  bytestream_put_byte(&buf, 'M');
176  bytestream_put_le32(&buf, pkt->size);
177  bytestream_put_le16(&buf, 0);
178  bytestream_put_le16(&buf, 0);
179  bytestream_put_le32(&buf, 0);
180 
181  if ((ret = avio_read(pb, buf, image->size)) != image->size) {
182  av_packet_unref(pkt);
183  return ret < 0 ? ret : AVERROR_INVALIDDATA;
184  }
185 
186  st->codecpar->bits_per_coded_sample = AV_RL16(buf + 14);
187 
188  if (AV_RL32(buf + 32))
189  image->nb_pal = AV_RL32(buf + 32);
190 
191  if (st->codecpar->bits_per_coded_sample <= 8 && !image->nb_pal) {
192  image->nb_pal = 1 << st->codecpar->bits_per_coded_sample;
193  AV_WL32(buf + 32, image->nb_pal);
194  }
195 
196  AV_WL32(buf - 4, 14 + 40 + image->nb_pal * 4);
197  AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
198  }
199 
200  pkt->stream_index = ico->current_image++;
201  pkt->flags |= AV_PKT_FLAG_KEY;
202 
203  return 0;
204 }
205 
207 {
208  IcoDemuxContext *ico = s->priv_data;
209  av_freep(&ico->images);
210  return 0;
211 }
212 
214  .name = "ico",
215  .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
216  .priv_data_size = sizeof(IcoDemuxContext),
217  .read_probe = probe,
222 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_RB64
Definition: intreadwrite.h:164
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3884
int size
Definition: avcodec.h:1431
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
#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:331
static AVPacket pkt
int offset
Definition: icodec.c:35
IcoImage * images
Definition: icodec.c:43
Format I/O context.
Definition: avformat.h:1342
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
int width
Video only.
Definition: avcodec.h:3950
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:4441
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1410
uint8_t * data
Definition: avcodec.h:1430
static int flags
Definition: log.c:55
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
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:310
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:648
AVInputFormat ff_ico_demuxer
Definition: icodec.c:213
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1462
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
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int nb_pal
Definition: icodec.c:37
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:770
#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:186
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3880
#define PNGSIG
Definition: png.h:47
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1436
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:639
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:451
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
#define FFMIN(a, b)
Definition: common.h:96
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
#define AV_RL32
Definition: intreadwrite.h:146
int frames
Definition: movenc.c:65
Stream structure.
Definition: avformat.h:873
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:469
AVIOContext * pb
I/O context.
Definition: avformat.h:1384
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:592
int current_image
Definition: icodec.c:41
void * buf
Definition: avisynth_c.h:690
static int ico_read_close(AVFormatContext *s)
Definition: icodec.c:206
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: icodec.c:148
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:754
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:1370
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3926
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1020
#define av_malloc_array(a, b)
static int read_header(AVFormatContext *s)
Definition: icodec.c:78
int stream_index
Definition: avcodec.h:1432
#define MKTAG(a, b, c, d)
Definition: common.h:366
This structure stores compressed data.
Definition: avcodec.h:1407
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
static uint8_t tmp[11]
Definition: aes_ctr.c:26