2 * IFF (.iff) file demuxer
3 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
4 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
6 * This file is part of FFmpeg.
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.
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.
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
26 * by Jaikrishnan Menon
27 * for more information on the .iff file format, visit:
28 * http://wiki.multimedia.cx/index.php?title=IFF
31 #include "libavutil/intreadwrite.h"
32 #include "libavcodec/iff.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;
77 uint32_t audio_frame_count;
81 static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
83 uint8_t *end = dest + size;
88 *dest++ = *(src+size);
93 static int iff_probe(AVProbeData *p)
95 const uint8_t *d = p->buf;
97 if ( AV_RL32(d) == ID_FORM &&
98 (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
99 return AVPROBE_SCORE_MAX;
103 static int iff_read_header(AVFormatContext *s,
104 AVFormatParameters *ap)
106 IffDemuxContext *iff = s->priv_data;
107 ByteIOContext *pb = s->pb;
109 uint32_t chunk_id, data_size;
110 int padding, done = 0;
111 int compression = -1;
114 st = av_new_stream(s, 0);
116 return AVERROR(ENOMEM);
118 st->codec->channels = 1;
120 // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
121 st->codec->codec_tag = get_le32(pb);
123 while(!done && !url_feof(pb)) {
124 chunk_id = get_le32(pb);
125 data_size = get_be32(pb);
126 padding = data_size & 1;
130 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
132 st->codec->sample_rate = get_be16(pb);
134 compression = get_byte(pb);
139 iff->body_size = data_size;
144 st->codec->channels = (get_be32(pb) < 6) ? 1 : 2;
148 st->codec->extradata_size = data_size;
149 st->codec->extradata = av_malloc(data_size);
150 if (!st->codec->extradata)
151 return AVERROR(ENOMEM);
152 if (get_buffer(pb, st->codec->extradata, data_size) < 0)
157 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
158 st->codec->width = get_be16(pb);
159 st->codec->height = get_be16(pb);
160 url_fskip(pb, 4); // x, y offset
161 st->codec->bits_per_coded_sample = get_byte(pb);
162 url_fskip(pb, 1); // masking
163 compression = get_byte(pb);
164 url_fskip(pb, 3); // paddding, transparent
165 st->sample_aspect_ratio.num = get_byte(pb);
166 st->sample_aspect_ratio.den = get_byte(pb);
167 url_fskip(pb, 4); // source page width, height
171 buf = av_malloc(data_size + 1);
174 get_buffer(pb, buf, data_size);
176 av_metadata_set2(&s->metadata, "comment", buf, AV_METADATA_DONT_STRDUP_VAL);
180 url_fseek(pb, data_size + padding, SEEK_CUR);
185 switch(st->codec->codec_type) {
186 case AVMEDIA_TYPE_AUDIO:
187 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
189 switch(compression) {
191 st->codec->codec_id = CODEC_ID_PCM_S8;
194 st->codec->codec_id = CODEC_ID_8SVX_FIB;
197 st->codec->codec_id = CODEC_ID_8SVX_EXP;
200 av_log(s, AV_LOG_ERROR, "iff: unknown compression method\n");
204 st->codec->bits_per_coded_sample = 8;
205 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
206 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
209 case AVMEDIA_TYPE_VIDEO:
210 switch (compression) {
212 if (st->codec->codec_tag == ID_ILBM) {
213 st->codec->codec_id = CODEC_ID_IFF_ILBM;
215 st->codec->codec_id = CODEC_ID_RAWVIDEO;
216 st->codec->pix_fmt = PIX_FMT_PAL8;
217 st->codec->codec_tag = 0;
220 case BITMAP_BYTERUN1:
221 st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
224 av_log(s, AV_LOG_ERROR, "unknown compression method\n");
225 return AVERROR_INVALIDDATA;
235 static int iff_read_packet(AVFormatContext *s,
238 IffDemuxContext *iff = s->priv_data;
239 ByteIOContext *pb = s->pb;
240 AVStream *st = s->streams[0];
243 if(iff->sent_bytes >= iff->body_size)
246 if(s->streams[0]->codec->channels == 2) {
247 uint8_t sample_buffer[PACKET_SIZE];
249 ret = get_buffer(pb, sample_buffer, PACKET_SIZE);
250 if(av_new_packet(pkt, PACKET_SIZE) < 0) {
251 av_log(s, AV_LOG_ERROR, "iff: cannot allocate packet \n");
252 return AVERROR(ENOMEM);
254 interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
255 } else if (s->streams[0]->codec->codec_id == CODEC_ID_RAWVIDEO) {
256 if(av_new_packet(pkt, iff->body_size + AVPALETTE_SIZE) < 0) {
257 return AVERROR(ENOMEM);
260 ret = ff_cmap_read_palette(st->codec, (uint32_t*)(pkt->data + iff->body_size));
263 av_freep(&st->codec->extradata);
264 st->codec->extradata_size = 0;
266 ret = get_buffer(pb, pkt->data, iff->body_size);
267 } else if (s->streams[0]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
268 ret = av_get_packet(pb, pkt, iff->body_size);
270 ret = av_get_packet(pb, pkt, PACKET_SIZE);
273 if(iff->sent_bytes == 0)
274 pkt->flags |= AV_PKT_FLAG_KEY;
276 if(s->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
277 iff->sent_bytes += PACKET_SIZE;
279 iff->sent_bytes = iff->body_size;
281 pkt->stream_index = 0;
282 if(s->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
283 pkt->pts = iff->audio_frame_count;
284 iff->audio_frame_count += ret / s->streams[0]->codec->channels;
289 AVInputFormat iff_demuxer = {
291 NULL_IF_CONFIG_SMALL("IFF format"),
292 sizeof(IffDemuxContext),