]> git.sesse.net Git - ffmpeg/blob - libavformat/iff.c
concatdec: change data type to suppress warning about limited range of data type...
[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 "libavutil/avassert.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/dict.h"
35 #include "libavcodec/bytestream.h"
36 #include "avformat.h"
37 #include "internal.h"
38
39 #define ID_8SVX       MKTAG('8','S','V','X')
40 #define ID_16SV       MKTAG('1','6','S','V')
41 #define ID_MAUD       MKTAG('M','A','U','D')
42 #define ID_MHDR       MKTAG('M','H','D','R')
43 #define ID_MDAT       MKTAG('M','D','A','T')
44 #define ID_VHDR       MKTAG('V','H','D','R')
45 #define ID_ATAK       MKTAG('A','T','A','K')
46 #define ID_RLSE       MKTAG('R','L','S','E')
47 #define ID_CHAN       MKTAG('C','H','A','N')
48 #define ID_PBM        MKTAG('P','B','M',' ')
49 #define ID_ILBM       MKTAG('I','L','B','M')
50 #define ID_BMHD       MKTAG('B','M','H','D')
51 #define ID_DGBL       MKTAG('D','G','B','L')
52 #define ID_CAMG       MKTAG('C','A','M','G')
53 #define ID_CMAP       MKTAG('C','M','A','P')
54 #define ID_ACBM       MKTAG('A','C','B','M')
55 #define ID_DEEP       MKTAG('D','E','E','P')
56 #define ID_RGB8       MKTAG('R','G','B','8')
57 #define ID_RGBN       MKTAG('R','G','B','N')
58
59 #define ID_FORM       MKTAG('F','O','R','M')
60 #define ID_ANNO       MKTAG('A','N','N','O')
61 #define ID_AUTH       MKTAG('A','U','T','H')
62 #define ID_CHRS       MKTAG('C','H','R','S')
63 #define ID_COPYRIGHT  MKTAG('(','c',')',' ')
64 #define ID_CSET       MKTAG('C','S','E','T')
65 #define ID_FVER       MKTAG('F','V','E','R')
66 #define ID_NAME       MKTAG('N','A','M','E')
67 #define ID_TEXT       MKTAG('T','E','X','T')
68 #define ID_ABIT       MKTAG('A','B','I','T')
69 #define ID_BODY       MKTAG('B','O','D','Y')
70 #define ID_DBOD       MKTAG('D','B','O','D')
71 #define ID_DPEL       MKTAG('D','P','E','L')
72 #define ID_DLOC       MKTAG('D','L','O','C')
73 #define ID_TVDC       MKTAG('T','V','D','C')
74
75 #define LEFT    2
76 #define RIGHT   4
77 #define STEREO  6
78
79 /**
80  * This number of bytes if added at the beginning of each AVPacket
81  * which contain additional information about video properties
82  * which has to be shared between demuxer and decoder.
83  * This number may change between frames, e.g. the demuxer might
84  * set it to smallest possible size of 2 to indicate that there's
85  * no extradata changing in this frame.
86  */
87 #define IFF_EXTRA_VIDEO_SIZE 41
88
89 typedef enum {
90     COMP_NONE,
91     COMP_FIB,
92     COMP_EXP
93 } svx8_compression_type;
94
95 typedef struct {
96     uint64_t  body_pos;
97     uint32_t  body_size;
98     uint32_t  sent_bytes;
99     svx8_compression_type   svx8_compression;
100     unsigned  maud_bits;
101     unsigned  maud_compression;
102     unsigned  bitmap_compression;  ///< delta compression method used
103     unsigned  bpp;          ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
104     unsigned  ham;          ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
105     unsigned  flags;        ///< 1 for EHB, 0 is no extra half darkening
106     unsigned  transparency; ///< transparency color index in palette
107     unsigned  masking;      ///< masking method used
108     uint8_t   tvdc[32];     ///< TVDC lookup table
109 } IffDemuxContext;
110
111 /* Metadata string read */
112 static int get_metadata(AVFormatContext *s,
113                         const char *const tag,
114                         const unsigned data_size)
115 {
116     uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
117
118     if (!buf)
119         return AVERROR(ENOMEM);
120
121     if (avio_read(s->pb, buf, data_size) < 0) {
122         av_free(buf);
123         return AVERROR(EIO);
124     }
125     buf[data_size] = 0;
126     av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
127     return 0;
128 }
129
130 static int iff_probe(AVProbeData *p)
131 {
132     const uint8_t *d = p->buf;
133
134     if (  AV_RL32(d)   == ID_FORM &&
135          (AV_RL32(d+8) == ID_8SVX ||
136           AV_RL32(d+8) == ID_16SV ||
137           AV_RL32(d+8) == ID_MAUD ||
138           AV_RL32(d+8) == ID_PBM  ||
139           AV_RL32(d+8) == ID_ACBM ||
140           AV_RL32(d+8) == ID_DEEP ||
141           AV_RL32(d+8) == ID_ILBM ||
142           AV_RL32(d+8) == ID_RGB8 ||
143           AV_RL32(d+8) == ID_RGBN) )
144         return AVPROBE_SCORE_MAX;
145     return 0;
146 }
147
148 static const uint8_t deep_rgb24[] = {0, 0, 0, 3, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
149 static const uint8_t deep_rgba[]  = {0, 0, 0, 4, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
150 static const uint8_t deep_bgra[]  = {0, 0, 0, 4, 0, 3, 0, 8, 0, 2, 0, 8, 0, 1, 0, 8};
151 static const uint8_t deep_argb[]  = {0, 0, 0, 4, 0,17, 0, 8, 0, 1, 0, 8, 0, 2, 0, 8};
152 static const uint8_t deep_abgr[]  = {0, 0, 0, 4, 0,17, 0, 8, 0, 3, 0, 8, 0, 2, 0, 8};
153
154 static int iff_read_header(AVFormatContext *s)
155 {
156     IffDemuxContext *iff = s->priv_data;
157     AVIOContext *pb = s->pb;
158     AVStream *st;
159     uint8_t *buf;
160     uint32_t chunk_id, data_size;
161     uint32_t screenmode = 0, num, den;
162     unsigned transparency = 0;
163     unsigned masking = 0; // no mask
164     uint8_t fmt[16];
165     int fmt_size;
166
167     st = avformat_new_stream(s, NULL);
168     if (!st)
169         return AVERROR(ENOMEM);
170
171     st->codec->channels = 1;
172     st->codec->channel_layout = AV_CH_LAYOUT_MONO;
173     avio_skip(pb, 8);
174     // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
175     st->codec->codec_tag = avio_rl32(pb);
176     iff->bitmap_compression = -1;
177     iff->svx8_compression = -1;
178     iff->maud_bits = -1;
179     iff->maud_compression = -1;
180
181     while(!url_feof(pb)) {
182         uint64_t orig_pos;
183         int res;
184         const char *metadata_tag = NULL;
185         chunk_id = avio_rl32(pb);
186         data_size = avio_rb32(pb);
187         orig_pos = avio_tell(pb);
188
189         switch(chunk_id) {
190         case ID_VHDR:
191             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
192
193             if (data_size < 14)
194                 return AVERROR_INVALIDDATA;
195             avio_skip(pb, 12);
196             st->codec->sample_rate = avio_rb16(pb);
197             if (data_size >= 16) {
198                 avio_skip(pb, 1);
199                 iff->svx8_compression = avio_r8(pb);
200             }
201             break;
202
203         case ID_MHDR:
204             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
205
206             if (data_size < 32)
207                 return AVERROR_INVALIDDATA;
208             avio_skip(pb, 4);
209             iff->maud_bits = avio_rb16(pb);
210             avio_skip(pb, 2);
211             num = avio_rb32(pb);
212             den = avio_rb16(pb);
213             if (!den)
214                 return AVERROR_INVALIDDATA;
215             avio_skip(pb, 2);
216             st->codec->sample_rate = num / den;
217             st->codec->channels = avio_rb16(pb);
218             iff->maud_compression = avio_rb16(pb);
219             if (st->codec->channels == 1)
220                 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
221             else if (st->codec->channels == 2)
222                 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
223             break;
224
225         case ID_ABIT:
226         case ID_BODY:
227         case ID_DBOD:
228         case ID_MDAT:
229             iff->body_pos = avio_tell(pb);
230             iff->body_size = data_size;
231             break;
232
233         case ID_CHAN:
234             if (data_size < 4)
235                 return AVERROR_INVALIDDATA;
236             if (avio_rb32(pb) < 6) {
237                 st->codec->channels       = 1;
238                 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
239             } else {
240                 st->codec->channels       = 2;
241                 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
242             }
243             break;
244
245         case ID_CAMG:
246             if (data_size < 4)
247                 return AVERROR_INVALIDDATA;
248             screenmode                = avio_rb32(pb);
249             break;
250
251         case ID_CMAP:
252             st->codec->extradata_size = data_size + IFF_EXTRA_VIDEO_SIZE;
253             st->codec->extradata      = av_malloc(data_size + IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
254             if (!st->codec->extradata)
255                 return AVERROR(ENOMEM);
256             if (avio_read(pb, st->codec->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0)
257                 return AVERROR(EIO);
258             break;
259
260         case ID_BMHD:
261             st->codec->codec_type            = AVMEDIA_TYPE_VIDEO;
262             if (data_size <= 8)
263                 return AVERROR_INVALIDDATA;
264             st->codec->width                 = avio_rb16(pb);
265             st->codec->height                = avio_rb16(pb);
266             avio_skip(pb, 4); // x, y offset
267             st->codec->bits_per_coded_sample = avio_r8(pb);
268             if (data_size >= 10)
269                 masking                      = avio_r8(pb);
270             if (data_size >= 11)
271                 iff->bitmap_compression      = avio_r8(pb);
272             if (data_size >= 14) {
273                 avio_skip(pb, 1); // padding
274                 transparency                 = avio_rb16(pb);
275             }
276             if (data_size >= 16) {
277                 st->sample_aspect_ratio.num  = avio_r8(pb);
278                 st->sample_aspect_ratio.den  = avio_r8(pb);
279             }
280             break;
281
282         case ID_DPEL:
283             if (data_size < 4 || (data_size & 3))
284                 return AVERROR_INVALIDDATA;
285             if ((fmt_size = avio_read(pb, fmt, sizeof(fmt))) < 0)
286                 return fmt_size;
287             if (fmt_size == sizeof(deep_rgb24) && !memcmp(fmt, deep_rgb24, sizeof(deep_rgb24)))
288                 st->codec->pix_fmt = AV_PIX_FMT_RGB24;
289             else if (fmt_size == sizeof(deep_rgba) && !memcmp(fmt, deep_rgba, sizeof(deep_rgba)))
290                 st->codec->pix_fmt = AV_PIX_FMT_RGBA;
291             else if (fmt_size == sizeof(deep_bgra) && !memcmp(fmt, deep_bgra, sizeof(deep_bgra)))
292                 st->codec->pix_fmt = AV_PIX_FMT_BGRA;
293             else if (fmt_size == sizeof(deep_argb) && !memcmp(fmt, deep_argb, sizeof(deep_argb)))
294                 st->codec->pix_fmt = AV_PIX_FMT_ARGB;
295             else if (fmt_size == sizeof(deep_abgr) && !memcmp(fmt, deep_abgr, sizeof(deep_abgr)))
296                 st->codec->pix_fmt = AV_PIX_FMT_ABGR;
297             else {
298                 av_log_ask_for_sample(s, "unsupported color format\n");
299                 return AVERROR_PATCHWELCOME;
300             }
301             break;
302
303         case ID_DGBL:
304             st->codec->codec_type            = AVMEDIA_TYPE_VIDEO;
305             if (data_size < 8)
306                 return AVERROR_INVALIDDATA;
307             st->codec->width                 = avio_rb16(pb);
308             st->codec->height                = avio_rb16(pb);
309             iff->bitmap_compression          = avio_rb16(pb);
310             st->sample_aspect_ratio.num      = avio_r8(pb);
311             st->sample_aspect_ratio.den      = avio_r8(pb);
312             st->codec->bits_per_coded_sample = 24;
313             break;
314
315         case ID_DLOC:
316             if (data_size < 4)
317                 return AVERROR_INVALIDDATA;
318             st->codec->width  = avio_rb16(pb);
319             st->codec->height = avio_rb16(pb);
320             break;
321
322         case ID_TVDC:
323             if (data_size < sizeof(iff->tvdc))
324                 return AVERROR_INVALIDDATA;
325             res = avio_read(pb, iff->tvdc, sizeof(iff->tvdc));
326             if (res < 0)
327                 return res;
328             break;
329
330         case ID_ANNO:
331         case ID_TEXT:      metadata_tag = "comment";   break;
332         case ID_AUTH:      metadata_tag = "artist";    break;
333         case ID_COPYRIGHT: metadata_tag = "copyright"; break;
334         case ID_NAME:      metadata_tag = "title";     break;
335         }
336
337         if (metadata_tag) {
338             if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
339                 av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", metadata_tag);
340                 return res;
341             }
342         }
343         avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
344     }
345
346     avio_seek(pb, iff->body_pos, SEEK_SET);
347
348     switch(st->codec->codec_type) {
349     case AVMEDIA_TYPE_AUDIO:
350         avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
351
352         if (st->codec->codec_tag == ID_16SV)
353             st->codec->codec_id = AV_CODEC_ID_PCM_S16BE_PLANAR;
354         else if (st->codec->codec_tag == ID_MAUD) {
355             if (iff->maud_bits == 8 && !iff->maud_compression) {
356                 st->codec->codec_id = AV_CODEC_ID_PCM_U8;
357             } else if (iff->maud_bits == 16 && !iff->maud_compression) {
358                 st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
359             } else if (iff->maud_bits ==  8 && iff->maud_compression == 2) {
360                 st->codec->codec_id = AV_CODEC_ID_PCM_ALAW;
361             } else if (iff->maud_bits ==  8 && iff->maud_compression == 3) {
362                 st->codec->codec_id = AV_CODEC_ID_PCM_MULAW;
363             } else {
364                 av_log_ask_for_sample(s, "unsupported compression %d and bit depth %d\n", iff->maud_compression, iff->maud_bits);
365                 return AVERROR_PATCHWELCOME;
366             }
367
368             st->codec->bits_per_coded_sample =
369                 av_get_bits_per_sample(st->codec->codec_id);
370
371             st->codec->block_align =
372                 st->codec->bits_per_coded_sample * st->codec->channels / 8;
373         } else {
374         switch (iff->svx8_compression) {
375         case COMP_NONE:
376             st->codec->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
377             break;
378         case COMP_FIB:
379             st->codec->codec_id = AV_CODEC_ID_8SVX_FIB;
380             break;
381         case COMP_EXP:
382             st->codec->codec_id = AV_CODEC_ID_8SVX_EXP;
383             break;
384         default:
385             av_log(s, AV_LOG_ERROR,
386                    "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
387             return -1;
388         }
389         }
390
391         st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
392         st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
393         st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
394         break;
395
396     case AVMEDIA_TYPE_VIDEO:
397         iff->bpp          = st->codec->bits_per_coded_sample;
398         if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
399             iff->ham      = iff->bpp > 6 ? 6 : 4;
400             st->codec->bits_per_coded_sample = 24;
401         }
402         iff->flags        = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
403         iff->masking      = masking;
404         iff->transparency = transparency;
405
406         if (!st->codec->extradata) {
407             st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
408             st->codec->extradata      = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
409             if (!st->codec->extradata)
410                 return AVERROR(ENOMEM);
411         }
412         buf = st->codec->extradata;
413         bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
414         bytestream_put_byte(&buf, iff->bitmap_compression);
415         bytestream_put_byte(&buf, iff->bpp);
416         bytestream_put_byte(&buf, iff->ham);
417         bytestream_put_byte(&buf, iff->flags);
418         bytestream_put_be16(&buf, iff->transparency);
419         bytestream_put_byte(&buf, iff->masking);
420         bytestream_put_buffer(&buf, iff->tvdc, sizeof(iff->tvdc));
421         st->codec->codec_id = AV_CODEC_ID_IFF_ILBM;
422         break;
423     default:
424         return -1;
425     }
426
427     return 0;
428 }
429
430 static int iff_read_packet(AVFormatContext *s,
431                            AVPacket *pkt)
432 {
433     IffDemuxContext *iff = s->priv_data;
434     AVIOContext *pb = s->pb;
435     AVStream *st = s->streams[0];
436     int ret;
437
438     if(iff->sent_bytes >= iff->body_size)
439         return AVERROR_EOF;
440
441     if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
442         if (st->codec->codec_tag == ID_MAUD) {
443             ret = av_get_packet(pb, pkt,
444                                 FFMIN(iff->body_size - iff->sent_bytes, 1024 * st->codec->block_align));
445         } else {
446             ret = av_get_packet(pb, pkt, iff->body_size);
447         }
448     } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
449         uint8_t *buf;
450
451         if (av_new_packet(pkt, iff->body_size + 2) < 0) {
452             return AVERROR(ENOMEM);
453         }
454
455         buf = pkt->data;
456         bytestream_put_be16(&buf, 2);
457         ret = avio_read(pb, buf, iff->body_size);
458     } else {
459         av_assert0(0);
460     }
461
462     if(iff->sent_bytes == 0)
463         pkt->flags |= AV_PKT_FLAG_KEY;
464     if (ret < 0)
465         return ret;
466     iff->sent_bytes += ret;
467     pkt->stream_index = 0;
468     return ret;
469 }
470
471 AVInputFormat ff_iff_demuxer = {
472     .name           = "iff",
473     .long_name      = NULL_IF_CONFIG_SMALL("IFF (Interchange File Format)"),
474     .priv_data_size = sizeof(IffDemuxContext),
475     .read_probe     = iff_probe,
476     .read_header    = iff_read_header,
477     .read_packet    = iff_read_packet,
478 };