2 * IFF (.iff) file demuxer
3 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
4 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
5 * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
7 * This file is part of Libav.
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * by Jaikrishnan Menon
28 * for more information on the .iff file format, visit:
29 * http://wiki.multimedia.cx/index.php?title=IFF
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/dict.h"
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_CMAP MKTAG('C','M','A','P')
47 #define ID_FORM MKTAG('F','O','R','M')
48 #define ID_ANNO MKTAG('A','N','N','O')
49 #define ID_AUTH MKTAG('A','U','T','H')
50 #define ID_CHRS MKTAG('C','H','R','S')
51 #define ID_COPYRIGHT MKTAG('(','c',')',' ')
52 #define ID_CSET MKTAG('C','S','E','T')
53 #define ID_FVER MKTAG('F','V','E','R')
54 #define ID_NAME MKTAG('N','A','M','E')
55 #define ID_TEXT MKTAG('T','E','X','T')
56 #define ID_BODY MKTAG('B','O','D','Y')
57 #define ID_ANNO MKTAG('A','N','N','O')
67 } svx8_compression_type;
72 } bitmap_compression_type;
81 /* Metadata string read */
82 static int get_metadata(AVFormatContext *s,
83 const char *const tag,
84 const unsigned data_size)
86 uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
89 return AVERROR(ENOMEM);
91 if (avio_read(s->pb, buf, data_size) < 0) {
96 av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
100 static int iff_probe(AVProbeData *p)
102 const uint8_t *d = p->buf;
104 if ( AV_RL32(d) == ID_FORM &&
105 (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
106 return AVPROBE_SCORE_MAX;
110 static int iff_read_header(AVFormatContext *s)
112 IffDemuxContext *iff = s->priv_data;
113 AVIOContext *pb = s->pb;
115 uint32_t chunk_id, data_size;
116 int compression = -1;
118 st = avformat_new_stream(s, NULL);
120 return AVERROR(ENOMEM);
122 st->codec->channels = 1;
124 // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
125 st->codec->codec_tag = avio_rl32(pb);
127 while(!pb->eof_reached) {
130 const char *metadata_tag = NULL;
131 chunk_id = avio_rl32(pb);
132 data_size = avio_rb32(pb);
133 orig_pos = avio_tell(pb);
137 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
140 return AVERROR_INVALIDDATA;
142 st->codec->sample_rate = avio_rb16(pb);
143 if (data_size >= 16) {
145 compression = avio_r8(pb);
150 iff->body_pos = avio_tell(pb);
151 iff->body_size = data_size;
156 return AVERROR_INVALIDDATA;
157 st->codec->channels = (avio_rb32(pb) < 6) ? 1 : 2;
161 st->codec->extradata_size = data_size;
162 st->codec->extradata = av_malloc(data_size);
163 if (!st->codec->extradata)
164 return AVERROR(ENOMEM);
165 if (avio_read(pb, st->codec->extradata, data_size) < 0)
170 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
172 return AVERROR_INVALIDDATA;
173 st->codec->width = avio_rb16(pb);
174 st->codec->height = avio_rb16(pb);
175 avio_skip(pb, 4); // x, y offset
176 st->codec->bits_per_coded_sample = avio_r8(pb);
177 if (data_size >= 11) {
178 avio_skip(pb, 1); // masking
179 compression = avio_r8(pb);
181 if (data_size >= 16) {
182 avio_skip(pb, 3); // paddding, transparent
183 st->sample_aspect_ratio.num = avio_r8(pb);
184 st->sample_aspect_ratio.den = avio_r8(pb);
190 metadata_tag = "comment";
194 metadata_tag = "artist";
198 metadata_tag = "copyright";
202 metadata_tag = "title";
207 if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
208 av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
212 avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
215 avio_seek(pb, iff->body_pos, SEEK_SET);
217 switch(st->codec->codec_type) {
218 case AVMEDIA_TYPE_AUDIO:
219 avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
221 switch(compression) {
223 st->codec->codec_id = CODEC_ID_PCM_S8_PLANAR;
226 st->codec->codec_id = CODEC_ID_8SVX_FIB;
229 st->codec->codec_id = CODEC_ID_8SVX_EXP;
232 av_log(s, AV_LOG_ERROR, "unknown compression method\n");
236 st->codec->bits_per_coded_sample = 8;
237 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
238 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
241 case AVMEDIA_TYPE_VIDEO:
242 switch (compression) {
244 st->codec->codec_id = CODEC_ID_IFF_ILBM;
246 case BITMAP_BYTERUN1:
247 st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
250 av_log(s, AV_LOG_ERROR, "unknown compression method\n");
251 return AVERROR_INVALIDDATA;
261 static int iff_read_packet(AVFormatContext *s,
264 IffDemuxContext *iff = s->priv_data;
265 AVIOContext *pb = s->pb;
268 if(iff->sent_bytes >= iff->body_size)
271 ret = av_get_packet(pb, pkt, iff->body_size);
275 if(iff->sent_bytes == 0)
276 pkt->flags |= AV_PKT_FLAG_KEY;
277 iff->sent_bytes = iff->body_size;
279 pkt->stream_index = 0;
283 AVInputFormat ff_iff_demuxer = {
285 .long_name = NULL_IF_CONFIG_SMALL("Interchange File Format"),
286 .priv_data_size = sizeof(IffDemuxContext),
287 .read_probe = iff_probe,
288 .read_header = iff_read_header,
289 .read_packet = iff_read_packet,