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