FFmpeg
 All Data Structures 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 "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 / 3;
49  return 0;
50 }
51 
53 {
54  IcoDemuxContext *ico = s->priv_data;
55  AVIOContext *pb = s->pb;
56  int i;
57 
58  avio_skip(pb, 4);
59  ico->nb_images = avio_rl16(pb);
60 
61  ico->images = av_malloc(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  ico->images[i].offset = avio_rl32(pb);
87 
88  if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
89  break;
90 
91  switch(avio_rl32(pb)) {
92  case MKTAG(0x89, 'P', 'N', 'G'):
94  st->codec->width = 0;
95  st->codec->height = 0;
96  break;
97  case 40:
98  if (ico->images[i].size < 40)
99  return AVERROR_INVALIDDATA;
101  tmp = avio_rl32(pb);
102  if (tmp)
103  st->codec->width = tmp;
104  tmp = avio_rl32(pb);
105  if (tmp)
106  st->codec->height = tmp / 2;
107  break;
108  default:
109  av_log_ask_for_sample(s, "unsupported codec\n");
110  return AVERROR_INVALIDDATA;
111  }
112  }
113 
114  return 0;
115 }
116 
118 {
119  IcoDemuxContext *ico = s->priv_data;
120  IcoImage *image;
121  AVIOContext *pb = s->pb;
122  AVStream *st = s->streams[0];
123  int ret;
124 
125  if (ico->current_image >= ico->nb_images)
126  return AVERROR(EIO);
127 
128  image = &ico->images[ico->current_image];
129 
130  if ((ret = avio_seek(pb, image->offset, SEEK_SET)) < 0)
131  return ret;
132 
133  if (s->streams[ico->current_image]->codec->codec_id == AV_CODEC_ID_PNG) {
134  if ((ret = av_get_packet(pb, pkt, image->size)) < 0)
135  return ret;
136  } else {
137  uint8_t *buf;
138  if ((ret = av_new_packet(pkt, 14 + image->size)) < 0)
139  return ret;
140  buf = pkt->data;
141 
142  /* add BMP header */
143  bytestream_put_byte(&buf, 'B');
144  bytestream_put_byte(&buf, 'M');
145  bytestream_put_le32(&buf, pkt->size);
146  bytestream_put_le16(&buf, 0);
147  bytestream_put_le16(&buf, 0);
148  bytestream_put_le32(&buf, 0);
149 
150  if ((ret = avio_read(pb, buf, image->size)) < 0)
151  return ret;
152 
153  st->codec->bits_per_coded_sample = AV_RL16(buf + 14);
154 
155  if (AV_RL32(buf + 32))
156  image->nb_pal = AV_RL32(buf + 32);
157 
158  if (st->codec->bits_per_coded_sample <= 8 && !image->nb_pal) {
159  image->nb_pal = 1 << st->codec->bits_per_coded_sample;
160  AV_WL32(buf + 32, image->nb_pal);
161  }
162 
163  AV_WL32(buf - 4, 14 + 40 + image->nb_pal * 4);
164  AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
165  }
166 
167  pkt->stream_index = ico->current_image++;
168  pkt->flags |= AV_PKT_FLAG_KEY;
169 
170  return 0;
171 }
172 
174  .name = "ico",
175  .long_name = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
176  .priv_data_size = sizeof(IcoDemuxContext),
177  .read_probe = probe,
181 };