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