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