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