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