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 "libavcodec/bytestream.h"
33 #include "libavutil/intreadwrite.h"
36 #define ID_8SVX MKTAG('8','S','V','X')
37 #define ID_VHDR MKTAG('V','H','D','R')
38 #define ID_ATAK MKTAG('A','T','A','K')
39 #define ID_RLSE MKTAG('R','L','S','E')
40 #define ID_CHAN MKTAG('C','H','A','N')
41 #define ID_PBM MKTAG('P','B','M',' ')
42 #define ID_ILBM MKTAG('I','L','B','M')
43 #define ID_BMHD MKTAG('B','M','H','D')
44 #define ID_CAMG MKTAG('C','A','M','G')
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')
64 * This number of bytes if added at the beginning of each AVPacket
65 * which contain additional information about video properties
66 * which has to be shared between demuxer and decoder.
67 * This number may change between frames, e.g. the demuxer might
68 * set it to smallest possible size of 2 to indicate that there's
69 * no extradata changing in this frame.
71 #define IFF_EXTRA_VIDEO_SIZE 9
77 } svx8_compression_type;
82 } bitmap_compression_type;
88 uint32_t audio_frame_count;
89 svx8_compression_type svx8_compression;
90 bitmap_compression_type bitmap_compression; ///< delta compression method used
91 unsigned bpp; ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
92 unsigned ham; ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
93 unsigned flags; ///< 1 for EHB, 0 is no extra half darkening
94 unsigned transparency; ///< transparency color index in palette
95 unsigned masking; ///< masking method used
98 /* Metadata string read */
99 static int get_metadata(AVFormatContext *s,
100 const char *const tag,
101 const unsigned data_size)
103 uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
106 return AVERROR(ENOMEM);
108 if (avio_read(s->pb, buf, data_size) < 0) {
113 av_metadata_set2(&s->metadata, tag, buf, AV_METADATA_DONT_STRDUP_VAL);
117 static int iff_probe(AVProbeData *p)
119 const uint8_t *d = p->buf;
121 if ( AV_RL32(d) == ID_FORM &&
122 (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
123 return AVPROBE_SCORE_MAX;
127 static int iff_read_header(AVFormatContext *s,
128 AVFormatParameters *ap)
130 IffDemuxContext *iff = s->priv_data;
131 AVIOContext *pb = s->pb;
134 uint32_t chunk_id, data_size;
135 uint32_t screenmode = 0;
136 unsigned transparency = 0;
137 unsigned masking = 0; // no mask
139 st = av_new_stream(s, 0);
141 return AVERROR(ENOMEM);
143 st->codec->channels = 1;
145 // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
146 st->codec->codec_tag = avio_rl32(pb);
148 while(!url_feof(pb)) {
151 const char *metadata_tag = NULL;
152 chunk_id = avio_rl32(pb);
153 data_size = avio_rb32(pb);
154 orig_pos = avio_tell(pb);
158 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
161 return AVERROR_INVALIDDATA;
163 st->codec->sample_rate = avio_rb16(pb);
164 if (data_size >= 16) {
166 iff->svx8_compression = avio_r8(pb);
171 iff->body_pos = avio_tell(pb);
172 iff->body_size = data_size;
177 return AVERROR_INVALIDDATA;
178 st->codec->channels = (avio_rb32(pb) < 6) ? 1 : 2;
183 return AVERROR_INVALIDDATA;
184 screenmode = avio_rb32(pb);
188 st->codec->extradata_size = data_size + IFF_EXTRA_VIDEO_SIZE;
189 st->codec->extradata = av_malloc(data_size + IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
190 if (!st->codec->extradata)
191 return AVERROR(ENOMEM);
192 if (avio_read(pb, st->codec->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0)
197 iff->bitmap_compression = -1;
198 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
200 return AVERROR_INVALIDDATA;
201 st->codec->width = avio_rb16(pb);
202 st->codec->height = avio_rb16(pb);
203 avio_skip(pb, 4); // x, y offset
204 st->codec->bits_per_coded_sample = avio_r8(pb);
206 masking = avio_r8(pb);
208 iff->bitmap_compression = avio_r8(pb);
209 if (data_size >= 14) {
210 avio_skip(pb, 1); // padding
211 transparency = avio_rb16(pb);
213 if (data_size >= 16) {
214 st->sample_aspect_ratio.num = avio_r8(pb);
215 st->sample_aspect_ratio.den = avio_r8(pb);
220 case ID_TEXT: metadata_tag = "comment"; break;
221 case ID_AUTH: metadata_tag = "artist"; break;
222 case ID_COPYRIGHT: metadata_tag = "copyright"; break;
223 case ID_NAME: metadata_tag = "title"; break;
227 if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
228 av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
232 avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
235 avio_seek(pb, iff->body_pos, SEEK_SET);
237 switch(st->codec->codec_type) {
238 case AVMEDIA_TYPE_AUDIO:
239 av_set_pts_info(st, 32, 1, st->codec->sample_rate);
241 switch (iff->svx8_compression) {
243 st->codec->codec_id = CODEC_ID_8SVX_RAW;
246 st->codec->codec_id = CODEC_ID_8SVX_FIB;
249 st->codec->codec_id = CODEC_ID_8SVX_EXP;
252 av_log(s, AV_LOG_ERROR,
253 "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
257 st->codec->bits_per_coded_sample = iff->svx8_compression == COMP_NONE ? 8 : 4;
258 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
259 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
262 case AVMEDIA_TYPE_VIDEO:
263 iff->bpp = st->codec->bits_per_coded_sample;
264 if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
265 iff->ham = iff->bpp > 6 ? 6 : 4;
266 st->codec->bits_per_coded_sample = 24;
268 iff->flags = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
269 iff->masking = masking;
270 iff->transparency = transparency;
272 if (!st->codec->extradata) {
273 st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
274 st->codec->extradata = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
275 if (!st->codec->extradata)
276 return AVERROR(ENOMEM);
278 buf = st->codec->extradata;
279 bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
280 bytestream_put_byte(&buf, iff->bitmap_compression);
281 bytestream_put_byte(&buf, iff->bpp);
282 bytestream_put_byte(&buf, iff->ham);
283 bytestream_put_byte(&buf, iff->flags);
284 bytestream_put_be16(&buf, iff->transparency);
285 bytestream_put_byte(&buf, iff->masking);
287 switch (iff->bitmap_compression) {
289 st->codec->codec_id = CODEC_ID_IFF_ILBM;
291 case BITMAP_BYTERUN1:
292 st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
295 av_log(s, AV_LOG_ERROR,
296 "Unknown bitmap compression method '%d'\n", iff->bitmap_compression);
297 return AVERROR_INVALIDDATA;
307 static int iff_read_packet(AVFormatContext *s,
310 IffDemuxContext *iff = s->priv_data;
311 AVIOContext *pb = s->pb;
312 AVStream *st = s->streams[0];
315 if(iff->sent_bytes >= iff->body_size)
318 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
319 ret = av_get_packet(pb, pkt, iff->body_size);
320 } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
323 if (av_new_packet(pkt, iff->body_size + 2) < 0) {
324 return AVERROR(ENOMEM);
328 bytestream_put_be16(&buf, 2);
329 ret = avio_read(pb, buf, iff->body_size);
332 if(iff->sent_bytes == 0)
333 pkt->flags |= AV_PKT_FLAG_KEY;
334 iff->sent_bytes = iff->body_size;
336 pkt->stream_index = 0;
340 AVInputFormat ff_iff_demuxer = {
342 NULL_IF_CONFIG_SMALL("IFF format"),
343 sizeof(IffDemuxContext),