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 FFmpeg.
9 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
35 #define ID_8SVX MKTAG('8','S','V','X')
36 #define ID_VHDR MKTAG('V','H','D','R')
37 #define ID_ATAK MKTAG('A','T','A','K')
38 #define ID_RLSE MKTAG('R','L','S','E')
39 #define ID_CHAN MKTAG('C','H','A','N')
40 #define ID_PBM MKTAG('P','B','M',' ')
41 #define ID_ILBM MKTAG('I','L','B','M')
42 #define ID_BMHD MKTAG('B','M','H','D')
43 #define ID_CMAP MKTAG('C','M','A','P')
45 #define ID_FORM MKTAG('F','O','R','M')
46 #define ID_ANNO MKTAG('A','N','N','O')
47 #define ID_AUTH MKTAG('A','U','T','H')
48 #define ID_CHRS MKTAG('C','H','R','S')
49 #define ID_COPYRIGHT MKTAG('(','c',')',' ')
50 #define ID_CSET MKTAG('C','S','E','T')
51 #define ID_FVER MKTAG('F','V','E','R')
52 #define ID_NAME MKTAG('N','A','M','E')
53 #define ID_TEXT MKTAG('T','E','X','T')
54 #define ID_BODY MKTAG('B','O','D','Y')
55 #define ID_ANNO MKTAG('A','N','N','O')
61 #define PACKET_SIZE 1024
67 } svx8_compression_type;
72 } bitmap_compression_type;
78 uint32_t audio_frame_count;
82 static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
84 uint8_t *end = dest + size;
89 *dest++ = *(src+size);
94 /* Metadata string read */
95 static int get_metadata(AVFormatContext *s,
96 const char *const tag,
97 const unsigned data_size)
99 uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
102 return AVERROR(ENOMEM);
104 if (get_buffer(s->pb, buf, data_size) < 0) {
109 av_metadata_set2(&s->metadata, tag, buf, AV_METADATA_DONT_STRDUP_VAL);
113 static int iff_probe(AVProbeData *p)
115 const uint8_t *d = p->buf;
117 if ( AV_RL32(d) == ID_FORM &&
118 (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
119 return AVPROBE_SCORE_MAX;
123 static int iff_read_header(AVFormatContext *s,
124 AVFormatParameters *ap)
126 IffDemuxContext *iff = s->priv_data;
127 ByteIOContext *pb = s->pb;
129 uint32_t chunk_id, data_size;
130 int compression = -1;
132 st = av_new_stream(s, 0);
134 return AVERROR(ENOMEM);
136 st->codec->channels = 1;
138 // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
139 st->codec->codec_tag = get_le32(pb);
141 while(!url_feof(pb)) {
144 const char *metadata_tag = NULL;
145 chunk_id = get_le32(pb);
146 data_size = get_be32(pb);
147 orig_pos = url_ftell(pb);
151 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
154 return AVERROR_INVALIDDATA;
156 st->codec->sample_rate = get_be16(pb);
157 if (data_size >= 16) {
159 compression = get_byte(pb);
164 iff->body_pos = url_ftell(pb);
165 iff->body_size = data_size;
170 return AVERROR_INVALIDDATA;
171 st->codec->channels = (get_be32(pb) < 6) ? 1 : 2;
175 st->codec->extradata_size = data_size;
176 st->codec->extradata = av_malloc(data_size);
177 if (!st->codec->extradata)
178 return AVERROR(ENOMEM);
179 if (get_buffer(pb, st->codec->extradata, data_size) < 0)
184 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
186 return AVERROR_INVALIDDATA;
187 st->codec->width = get_be16(pb);
188 st->codec->height = get_be16(pb);
189 url_fskip(pb, 4); // x, y offset
190 st->codec->bits_per_coded_sample = get_byte(pb);
191 if (data_size >= 11) {
192 url_fskip(pb, 1); // masking
193 compression = get_byte(pb);
195 if (data_size >= 16) {
196 url_fskip(pb, 3); // paddding, transparent
197 st->sample_aspect_ratio.num = get_byte(pb);
198 st->sample_aspect_ratio.den = get_byte(pb);
204 metadata_tag = "comment";
208 metadata_tag = "artist";
212 metadata_tag = "copyright";
216 metadata_tag = "title";
221 if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
222 av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
226 url_fskip(pb, data_size - (url_ftell(pb) - orig_pos) + (data_size & 1));
229 url_fseek(pb, iff->body_pos, SEEK_SET);
231 switch(st->codec->codec_type) {
232 case AVMEDIA_TYPE_AUDIO:
233 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
235 switch(compression) {
237 st->codec->codec_id = CODEC_ID_PCM_S8;
240 st->codec->codec_id = CODEC_ID_8SVX_FIB;
243 st->codec->codec_id = CODEC_ID_8SVX_EXP;
246 av_log(s, AV_LOG_ERROR, "unknown compression method\n");
250 st->codec->bits_per_coded_sample = 8;
251 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
252 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
255 case AVMEDIA_TYPE_VIDEO:
256 switch (compression) {
258 st->codec->codec_id = CODEC_ID_IFF_ILBM;
260 case BITMAP_BYTERUN1:
261 st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
264 av_log(s, AV_LOG_ERROR, "unknown compression method\n");
265 return AVERROR_INVALIDDATA;
275 static int iff_read_packet(AVFormatContext *s,
278 IffDemuxContext *iff = s->priv_data;
279 ByteIOContext *pb = s->pb;
280 AVStream *st = s->streams[0];
283 if(iff->sent_bytes >= iff->body_size)
286 if(st->codec->channels == 2) {
287 uint8_t sample_buffer[PACKET_SIZE];
289 ret = get_buffer(pb, sample_buffer, PACKET_SIZE);
290 if(av_new_packet(pkt, PACKET_SIZE) < 0) {
291 av_log(s, AV_LOG_ERROR, "cannot allocate packet\n");
292 return AVERROR(ENOMEM);
294 interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
295 } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
296 ret = av_get_packet(pb, pkt, iff->body_size);
298 ret = av_get_packet(pb, pkt, PACKET_SIZE);
301 if(iff->sent_bytes == 0)
302 pkt->flags |= AV_PKT_FLAG_KEY;
304 if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
305 iff->sent_bytes += PACKET_SIZE;
307 iff->sent_bytes = iff->body_size;
309 pkt->stream_index = 0;
310 if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
311 pkt->pts = iff->audio_frame_count;
312 iff->audio_frame_count += ret / st->codec->channels;
317 AVInputFormat ff_iff_demuxer = {
319 NULL_IF_CONFIG_SMALL("IFF format"),
320 sizeof(IffDemuxContext),