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