]> git.sesse.net Git - ffmpeg/blob - libavformat/iff.c
compute_pkt_fields: fix harmless integer overflow
[ffmpeg] / libavformat / iff.c
1 /*
2  * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
3  * Copyright (c) 2010 Peter Ross <pross@xvid.org>
4  * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * IFF file demuxer
26  * by Jaikrishnan Menon
27  * for more information on the .iff file format, visit:
28  * http://wiki.multimedia.cx/index.php?title=IFF
29  */
30
31 #include "libavcodec/bytestream.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/dict.h"
35 #include "avformat.h"
36 #include "internal.h"
37
38 #define ID_8SVX       MKTAG('8','S','V','X')
39 #define ID_VHDR       MKTAG('V','H','D','R')
40 #define ID_ATAK       MKTAG('A','T','A','K')
41 #define ID_RLSE       MKTAG('R','L','S','E')
42 #define ID_CHAN       MKTAG('C','H','A','N')
43 #define ID_PBM        MKTAG('P','B','M',' ')
44 #define ID_ILBM       MKTAG('I','L','B','M')
45 #define ID_BMHD       MKTAG('B','M','H','D')
46 #define ID_DGBL       MKTAG('D','G','B','L')
47 #define ID_CAMG       MKTAG('C','A','M','G')
48 #define ID_CMAP       MKTAG('C','M','A','P')
49 #define ID_ACBM       MKTAG('A','C','B','M')
50 #define ID_DEEP       MKTAG('D','E','E','P')
51
52 #define ID_FORM       MKTAG('F','O','R','M')
53 #define ID_ANNO       MKTAG('A','N','N','O')
54 #define ID_AUTH       MKTAG('A','U','T','H')
55 #define ID_CHRS       MKTAG('C','H','R','S')
56 #define ID_COPYRIGHT  MKTAG('(','c',')',' ')
57 #define ID_CSET       MKTAG('C','S','E','T')
58 #define ID_FVER       MKTAG('F','V','E','R')
59 #define ID_NAME       MKTAG('N','A','M','E')
60 #define ID_TEXT       MKTAG('T','E','X','T')
61 #define ID_ABIT       MKTAG('A','B','I','T')
62 #define ID_BODY       MKTAG('B','O','D','Y')
63 #define ID_DBOD       MKTAG('D','B','O','D')
64 #define ID_DPEL       MKTAG('D','P','E','L')
65
66 #define LEFT    2
67 #define RIGHT   4
68 #define STEREO  6
69
70 /**
71  * This number of bytes if added at the beginning of each AVPacket
72  * which contain additional information about video properties
73  * which has to be shared between demuxer and decoder.
74  * This number may change between frames, e.g. the demuxer might
75  * set it to smallest possible size of 2 to indicate that there's
76  * no extradata changing in this frame.
77  */
78 #define IFF_EXTRA_VIDEO_SIZE 9
79
80 typedef enum {
81     COMP_NONE,
82     COMP_FIB,
83     COMP_EXP
84 } svx8_compression_type;
85
86 typedef enum {
87     BITMAP_RAW,
88     BITMAP_BYTERUN1
89 } bitmap_compression_type;
90
91 typedef struct {
92     uint64_t  body_pos;
93     uint32_t  body_size;
94     uint32_t  sent_bytes;
95     svx8_compression_type   svx8_compression;
96     bitmap_compression_type bitmap_compression;  ///< delta compression method used
97     unsigned  bpp;          ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
98     unsigned  ham;          ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
99     unsigned  flags;        ///< 1 for EHB, 0 is no extra half darkening
100     unsigned  transparency; ///< transparency color index in palette
101     unsigned  masking;      ///< masking method used
102 } IffDemuxContext;
103
104 /* Metadata string read */
105 static int get_metadata(AVFormatContext *s,
106                         const char *const tag,
107                         const unsigned data_size)
108 {
109     uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
110
111     if (!buf)
112         return AVERROR(ENOMEM);
113
114     if (avio_read(s->pb, buf, data_size) < 0) {
115         av_free(buf);
116         return AVERROR(EIO);
117     }
118     buf[data_size] = 0;
119     av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
120     return 0;
121 }
122
123 static int iff_probe(AVProbeData *p)
124 {
125     const uint8_t *d = p->buf;
126
127     if (  AV_RL32(d)   == ID_FORM &&
128          (AV_RL32(d+8) == ID_8SVX ||
129           AV_RL32(d+8) == ID_PBM  ||
130           AV_RL32(d+8) == ID_ACBM ||
131           AV_RL32(d+8) == ID_DEEP ||
132           AV_RL32(d+8) == ID_ILBM) )
133         return AVPROBE_SCORE_MAX;
134     return 0;
135 }
136
137 static const uint8_t deep_rgb24[] = {0, 0, 0, 3, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
138 static const uint8_t deep_rgba[]  = {0, 0, 0, 4, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
139
140 static int iff_read_header(AVFormatContext *s)
141 {
142     IffDemuxContext *iff = s->priv_data;
143     AVIOContext *pb = s->pb;
144     AVStream *st;
145     uint8_t *buf;
146     uint32_t chunk_id, data_size;
147     uint32_t screenmode = 0;
148     unsigned transparency = 0;
149     unsigned masking = 0; // no mask
150     uint8_t fmt[16];
151     int fmt_size;
152
153     st = avformat_new_stream(s, NULL);
154     if (!st)
155         return AVERROR(ENOMEM);
156
157     st->codec->channels = 1;
158     avio_skip(pb, 8);
159     // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
160     st->codec->codec_tag = avio_rl32(pb);
161
162     while(!url_feof(pb)) {
163         uint64_t orig_pos;
164         int res;
165         const char *metadata_tag = NULL;
166         chunk_id = avio_rl32(pb);
167         data_size = avio_rb32(pb);
168         orig_pos = avio_tell(pb);
169
170         switch(chunk_id) {
171         case ID_VHDR:
172             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
173
174             if (data_size < 14)
175                 return AVERROR_INVALIDDATA;
176             avio_skip(pb, 12);
177             st->codec->sample_rate = avio_rb16(pb);
178             if (data_size >= 16) {
179                 avio_skip(pb, 1);
180                 iff->svx8_compression = avio_r8(pb);
181             }
182             break;
183
184         case ID_ABIT:
185         case ID_BODY:
186         case ID_DBOD:
187             iff->body_pos = avio_tell(pb);
188             iff->body_size = data_size;
189             break;
190
191         case ID_CHAN:
192             if (data_size < 4)
193                 return AVERROR_INVALIDDATA;
194             st->codec->channels = (avio_rb32(pb) < 6) ? 1 : 2;
195             break;
196
197         case ID_CAMG:
198             if (data_size < 4)
199                 return AVERROR_INVALIDDATA;
200             screenmode                = avio_rb32(pb);
201             break;
202
203         case ID_CMAP:
204             st->codec->extradata_size = data_size + IFF_EXTRA_VIDEO_SIZE;
205             st->codec->extradata      = av_malloc(data_size + IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
206             if (!st->codec->extradata)
207                 return AVERROR(ENOMEM);
208             if (avio_read(pb, st->codec->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0)
209                 return AVERROR(EIO);
210             break;
211
212         case ID_BMHD:
213             iff->bitmap_compression = -1;
214             st->codec->codec_type            = AVMEDIA_TYPE_VIDEO;
215             if (data_size <= 8)
216                 return AVERROR_INVALIDDATA;
217             st->codec->width                 = avio_rb16(pb);
218             st->codec->height                = avio_rb16(pb);
219             avio_skip(pb, 4); // x, y offset
220             st->codec->bits_per_coded_sample = avio_r8(pb);
221             if (data_size >= 10)
222                 masking                      = avio_r8(pb);
223             if (data_size >= 11)
224                 iff->bitmap_compression      = avio_r8(pb);
225             if (data_size >= 14) {
226                 avio_skip(pb, 1); // padding
227                 transparency                 = avio_rb16(pb);
228             }
229             if (data_size >= 16) {
230                 st->sample_aspect_ratio.num  = avio_r8(pb);
231                 st->sample_aspect_ratio.den  = avio_r8(pb);
232             }
233             break;
234
235         case ID_DPEL:
236             if (data_size < 4 || (data_size & 3))
237                 return AVERROR_INVALIDDATA;
238             if ((fmt_size = avio_read(pb, fmt, sizeof(fmt))) < 0)
239                 return fmt_size;
240             if (fmt_size == sizeof(deep_rgb24) && !memcmp(fmt, deep_rgb24, sizeof(deep_rgb24)))
241                 st->codec->pix_fmt = PIX_FMT_RGB24;
242             else if (fmt_size == sizeof(deep_rgba) && !memcmp(fmt, deep_rgba, sizeof(deep_rgba)))
243                 st->codec->pix_fmt = PIX_FMT_RGBA;
244             else {
245                 av_log_ask_for_sample(s, "unsupported color format\n");
246                 return AVERROR_PATCHWELCOME;
247             }
248             break;
249
250         case ID_DGBL:
251             st->codec->codec_type            = AVMEDIA_TYPE_VIDEO;
252             if (data_size < 8)
253                 return AVERROR_INVALIDDATA;
254             st->codec->width                 = avio_rb16(pb);
255             st->codec->height                = avio_rb16(pb);
256             iff->bitmap_compression          = avio_rb16(pb);
257             if (iff->bitmap_compression != 0) {
258                 av_log(s, AV_LOG_ERROR,
259                        "compression %i not supported\n", iff->bitmap_compression);
260                 return AVERROR_PATCHWELCOME;
261             }
262             st->sample_aspect_ratio.num      = avio_r8(pb);
263             st->sample_aspect_ratio.den      = avio_r8(pb);
264             st->codec->bits_per_coded_sample = 24;
265             break;
266
267         case ID_ANNO:
268         case ID_TEXT:      metadata_tag = "comment";   break;
269         case ID_AUTH:      metadata_tag = "artist";    break;
270         case ID_COPYRIGHT: metadata_tag = "copyright"; break;
271         case ID_NAME:      metadata_tag = "title";     break;
272         }
273
274         if (metadata_tag) {
275             if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
276                 av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", metadata_tag);
277                 return res;
278             }
279         }
280         avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
281     }
282
283     avio_seek(pb, iff->body_pos, SEEK_SET);
284
285     switch(st->codec->codec_type) {
286     case AVMEDIA_TYPE_AUDIO:
287         avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
288
289         switch (iff->svx8_compression) {
290         case COMP_NONE:
291             st->codec->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
292             break;
293         case COMP_FIB:
294             st->codec->codec_id = AV_CODEC_ID_8SVX_FIB;
295             break;
296         case COMP_EXP:
297             st->codec->codec_id = AV_CODEC_ID_8SVX_EXP;
298             break;
299         default:
300             av_log(s, AV_LOG_ERROR,
301                    "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
302             return -1;
303         }
304
305         st->codec->bits_per_coded_sample = iff->svx8_compression == COMP_NONE ? 8 : 4;
306         st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
307         st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
308         break;
309
310     case AVMEDIA_TYPE_VIDEO:
311         iff->bpp          = st->codec->bits_per_coded_sample;
312         if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
313             iff->ham      = iff->bpp > 6 ? 6 : 4;
314             st->codec->bits_per_coded_sample = 24;
315         }
316         iff->flags        = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
317         iff->masking      = masking;
318         iff->transparency = transparency;
319
320         if (!st->codec->extradata) {
321             st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
322             st->codec->extradata      = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
323             if (!st->codec->extradata)
324                 return AVERROR(ENOMEM);
325         }
326         buf = st->codec->extradata;
327         bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
328         bytestream_put_byte(&buf, iff->bitmap_compression);
329         bytestream_put_byte(&buf, iff->bpp);
330         bytestream_put_byte(&buf, iff->ham);
331         bytestream_put_byte(&buf, iff->flags);
332         bytestream_put_be16(&buf, iff->transparency);
333         bytestream_put_byte(&buf, iff->masking);
334
335         switch (iff->bitmap_compression) {
336         case BITMAP_RAW:
337             st->codec->codec_id = AV_CODEC_ID_IFF_ILBM;
338             break;
339         case BITMAP_BYTERUN1:
340             st->codec->codec_id = AV_CODEC_ID_IFF_BYTERUN1;
341             break;
342         default:
343             av_log(s, AV_LOG_ERROR,
344                    "Unknown bitmap compression method '%d'\n", iff->bitmap_compression);
345             return AVERROR_INVALIDDATA;
346         }
347         break;
348     default:
349         return -1;
350     }
351
352     return 0;
353 }
354
355 static int iff_read_packet(AVFormatContext *s,
356                            AVPacket *pkt)
357 {
358     IffDemuxContext *iff = s->priv_data;
359     AVIOContext *pb = s->pb;
360     AVStream *st = s->streams[0];
361     int ret;
362
363     if(iff->sent_bytes >= iff->body_size)
364         return AVERROR_EOF;
365
366     if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
367         ret = av_get_packet(pb, pkt, iff->body_size);
368     } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
369         uint8_t *buf;
370
371         if (av_new_packet(pkt, iff->body_size + 2) < 0) {
372             return AVERROR(ENOMEM);
373         }
374
375         buf = pkt->data;
376         bytestream_put_be16(&buf, 2);
377         ret = avio_read(pb, buf, iff->body_size);
378     } else {
379         av_assert0(0);
380     }
381
382     if(iff->sent_bytes == 0)
383         pkt->flags |= AV_PKT_FLAG_KEY;
384     iff->sent_bytes = iff->body_size;
385
386     pkt->stream_index = 0;
387     return ret;
388 }
389
390 AVInputFormat ff_iff_demuxer = {
391     .name           = "iff",
392     .long_name      = NULL_IF_CONFIG_SMALL("IFF (Interchange File Format)"),
393     .priv_data_size = sizeof(IffDemuxContext),
394     .read_probe     = iff_probe,
395     .read_header    = iff_read_header,
396     .read_packet    = iff_read_packet,
397 };