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>
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/avassert.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/dict.h"
35 #include "libavcodec/bytestream.h"
39 #define ID_8SVX MKTAG('8','S','V','X')
40 #define ID_16SV MKTAG('1','6','S','V')
41 #define ID_MAUD MKTAG('M','A','U','D')
42 #define ID_MHDR MKTAG('M','H','D','R')
43 #define ID_MDAT MKTAG('M','D','A','T')
44 #define ID_VHDR MKTAG('V','H','D','R')
45 #define ID_ATAK MKTAG('A','T','A','K')
46 #define ID_RLSE MKTAG('R','L','S','E')
47 #define ID_CHAN MKTAG('C','H','A','N')
48 #define ID_PBM MKTAG('P','B','M',' ')
49 #define ID_ILBM MKTAG('I','L','B','M')
50 #define ID_BMHD MKTAG('B','M','H','D')
51 #define ID_DGBL MKTAG('D','G','B','L')
52 #define ID_CAMG MKTAG('C','A','M','G')
53 #define ID_CMAP MKTAG('C','M','A','P')
54 #define ID_ACBM MKTAG('A','C','B','M')
55 #define ID_DEEP MKTAG('D','E','E','P')
57 #define ID_FORM MKTAG('F','O','R','M')
58 #define ID_ANNO MKTAG('A','N','N','O')
59 #define ID_AUTH MKTAG('A','U','T','H')
60 #define ID_CHRS MKTAG('C','H','R','S')
61 #define ID_COPYRIGHT MKTAG('(','c',')',' ')
62 #define ID_CSET MKTAG('C','S','E','T')
63 #define ID_FVER MKTAG('F','V','E','R')
64 #define ID_NAME MKTAG('N','A','M','E')
65 #define ID_TEXT MKTAG('T','E','X','T')
66 #define ID_ABIT MKTAG('A','B','I','T')
67 #define ID_BODY MKTAG('B','O','D','Y')
68 #define ID_DBOD MKTAG('D','B','O','D')
69 #define ID_DPEL MKTAG('D','P','E','L')
70 #define ID_DLOC MKTAG('D','L','O','C')
77 * This number of bytes if added at the beginning of each AVPacket
78 * which contain additional information about video properties
79 * which has to be shared between demuxer and decoder.
80 * This number may change between frames, e.g. the demuxer might
81 * set it to smallest possible size of 2 to indicate that there's
82 * no extradata changing in this frame.
84 #define IFF_EXTRA_VIDEO_SIZE 9
90 } svx8_compression_type;
96 svx8_compression_type svx8_compression;
98 unsigned maud_compression;
99 unsigned bitmap_compression; ///< delta compression method used
100 unsigned bpp; ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
101 unsigned ham; ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
102 unsigned flags; ///< 1 for EHB, 0 is no extra half darkening
103 unsigned transparency; ///< transparency color index in palette
104 unsigned masking; ///< masking method used
107 /* Metadata string read */
108 static int get_metadata(AVFormatContext *s,
109 const char *const tag,
110 const unsigned data_size)
112 uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
115 return AVERROR(ENOMEM);
117 if (avio_read(s->pb, buf, data_size) < 0) {
122 av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
126 static int iff_probe(AVProbeData *p)
128 const uint8_t *d = p->buf;
130 if ( AV_RL32(d) == ID_FORM &&
131 (AV_RL32(d+8) == ID_8SVX ||
132 AV_RL32(d+8) == ID_16SV ||
133 AV_RL32(d+8) == ID_MAUD ||
134 AV_RL32(d+8) == ID_PBM ||
135 AV_RL32(d+8) == ID_ACBM ||
136 AV_RL32(d+8) == ID_DEEP ||
137 AV_RL32(d+8) == ID_ILBM) )
138 return AVPROBE_SCORE_MAX;
142 static const uint8_t deep_rgb24[] = {0, 0, 0, 3, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
143 static const uint8_t deep_rgba[] = {0, 0, 0, 4, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
144 static const uint8_t deep_bgra[] = {0, 0, 0, 4, 0, 3, 0, 8, 0, 2, 0, 8, 0, 1, 0, 8};
145 static const uint8_t deep_argb[] = {0, 0, 0, 4, 0,17, 0, 8, 0, 1, 0, 8, 0, 2, 0, 8};
146 static const uint8_t deep_abgr[] = {0, 0, 0, 4, 0,17, 0, 8, 0, 3, 0, 8, 0, 2, 0, 8};
148 static int iff_read_header(AVFormatContext *s)
150 IffDemuxContext *iff = s->priv_data;
151 AVIOContext *pb = s->pb;
154 uint32_t chunk_id, data_size;
155 uint32_t screenmode = 0, num, den;
156 unsigned transparency = 0;
157 unsigned masking = 0; // no mask
161 st = avformat_new_stream(s, NULL);
163 return AVERROR(ENOMEM);
165 st->codec->channels = 1;
166 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
168 // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
169 st->codec->codec_tag = avio_rl32(pb);
170 iff->bitmap_compression = -1;
171 iff->svx8_compression = -1;
173 iff->maud_compression = -1;
175 while(!url_feof(pb)) {
178 const char *metadata_tag = NULL;
179 chunk_id = avio_rl32(pb);
180 data_size = avio_rb32(pb);
181 orig_pos = avio_tell(pb);
185 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
188 return AVERROR_INVALIDDATA;
190 st->codec->sample_rate = avio_rb16(pb);
191 if (data_size >= 16) {
193 iff->svx8_compression = avio_r8(pb);
198 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
201 return AVERROR_INVALIDDATA;
203 iff->maud_bits = avio_rb16(pb);
208 return AVERROR_INVALIDDATA;
210 st->codec->sample_rate = num / den;
211 st->codec->channels = avio_rb16(pb);
212 iff->maud_compression = avio_rb16(pb);
213 if (st->codec->channels == 1)
214 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
215 else if (st->codec->channels == 2)
216 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
223 iff->body_pos = avio_tell(pb);
224 iff->body_size = data_size;
229 return AVERROR_INVALIDDATA;
230 if (avio_rb32(pb) < 6) {
231 st->codec->channels = 1;
232 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
234 st->codec->channels = 2;
235 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
241 return AVERROR_INVALIDDATA;
242 screenmode = avio_rb32(pb);
246 st->codec->extradata_size = data_size + IFF_EXTRA_VIDEO_SIZE;
247 st->codec->extradata = av_malloc(data_size + IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
248 if (!st->codec->extradata)
249 return AVERROR(ENOMEM);
250 if (avio_read(pb, st->codec->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0)
255 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
257 return AVERROR_INVALIDDATA;
258 st->codec->width = avio_rb16(pb);
259 st->codec->height = avio_rb16(pb);
260 avio_skip(pb, 4); // x, y offset
261 st->codec->bits_per_coded_sample = avio_r8(pb);
263 masking = avio_r8(pb);
265 iff->bitmap_compression = avio_r8(pb);
266 if (data_size >= 14) {
267 avio_skip(pb, 1); // padding
268 transparency = avio_rb16(pb);
270 if (data_size >= 16) {
271 st->sample_aspect_ratio.num = avio_r8(pb);
272 st->sample_aspect_ratio.den = avio_r8(pb);
277 if (data_size < 4 || (data_size & 3))
278 return AVERROR_INVALIDDATA;
279 if ((fmt_size = avio_read(pb, fmt, sizeof(fmt))) < 0)
281 if (fmt_size == sizeof(deep_rgb24) && !memcmp(fmt, deep_rgb24, sizeof(deep_rgb24)))
282 st->codec->pix_fmt = AV_PIX_FMT_RGB24;
283 else if (fmt_size == sizeof(deep_rgba) && !memcmp(fmt, deep_rgba, sizeof(deep_rgba)))
284 st->codec->pix_fmt = AV_PIX_FMT_RGBA;
285 else if (fmt_size == sizeof(deep_bgra) && !memcmp(fmt, deep_bgra, sizeof(deep_bgra)))
286 st->codec->pix_fmt = AV_PIX_FMT_BGRA;
287 else if (fmt_size == sizeof(deep_argb) && !memcmp(fmt, deep_argb, sizeof(deep_argb)))
288 st->codec->pix_fmt = AV_PIX_FMT_ARGB;
289 else if (fmt_size == sizeof(deep_abgr) && !memcmp(fmt, deep_abgr, sizeof(deep_abgr)))
290 st->codec->pix_fmt = AV_PIX_FMT_ABGR;
292 av_log_ask_for_sample(s, "unsupported color format\n");
293 return AVERROR_PATCHWELCOME;
298 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
300 return AVERROR_INVALIDDATA;
301 st->codec->width = avio_rb16(pb);
302 st->codec->height = avio_rb16(pb);
303 iff->bitmap_compression = avio_rb16(pb);
304 st->sample_aspect_ratio.num = avio_r8(pb);
305 st->sample_aspect_ratio.den = avio_r8(pb);
306 st->codec->bits_per_coded_sample = 24;
311 return AVERROR_INVALIDDATA;
312 st->codec->width = avio_rb16(pb);
313 st->codec->height = avio_rb16(pb);
317 case ID_TEXT: metadata_tag = "comment"; break;
318 case ID_AUTH: metadata_tag = "artist"; break;
319 case ID_COPYRIGHT: metadata_tag = "copyright"; break;
320 case ID_NAME: metadata_tag = "title"; break;
324 if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
325 av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!\n", metadata_tag);
329 avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1));
332 avio_seek(pb, iff->body_pos, SEEK_SET);
334 switch(st->codec->codec_type) {
335 case AVMEDIA_TYPE_AUDIO:
336 avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
338 if (st->codec->codec_tag == ID_16SV)
339 st->codec->codec_id = AV_CODEC_ID_PCM_S16BE_PLANAR;
340 else if (st->codec->codec_tag == ID_MAUD) {
341 if (iff->maud_bits == 8 && !iff->maud_compression) {
342 st->codec->codec_id = AV_CODEC_ID_PCM_U8;
343 } else if (iff->maud_bits == 16 && !iff->maud_compression) {
344 st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
345 } else if (iff->maud_bits == 8 && iff->maud_compression == 2) {
346 st->codec->codec_id = AV_CODEC_ID_PCM_ALAW;
347 } else if (iff->maud_bits == 8 && iff->maud_compression == 3) {
348 st->codec->codec_id = AV_CODEC_ID_PCM_MULAW;
350 av_log_ask_for_sample(s, "unsupported compression %d and bit depth %d\n", iff->maud_compression, iff->maud_bits);
351 return AVERROR_PATCHWELCOME;
354 st->codec->bits_per_coded_sample =
355 av_get_bits_per_sample(st->codec->codec_id);
357 st->codec->block_align =
358 st->codec->bits_per_coded_sample * st->codec->channels / 8;
360 switch (iff->svx8_compression) {
362 st->codec->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
365 st->codec->codec_id = AV_CODEC_ID_8SVX_FIB;
368 st->codec->codec_id = AV_CODEC_ID_8SVX_EXP;
371 av_log(s, AV_LOG_ERROR,
372 "Unknown SVX8 compression method '%d'\n", iff->svx8_compression);
377 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
378 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
379 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
382 case AVMEDIA_TYPE_VIDEO:
383 iff->bpp = st->codec->bits_per_coded_sample;
384 if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
385 iff->ham = iff->bpp > 6 ? 6 : 4;
386 st->codec->bits_per_coded_sample = 24;
388 iff->flags = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
389 iff->masking = masking;
390 iff->transparency = transparency;
392 if (!st->codec->extradata) {
393 st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
394 st->codec->extradata = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
395 if (!st->codec->extradata)
396 return AVERROR(ENOMEM);
398 buf = st->codec->extradata;
399 bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
400 bytestream_put_byte(&buf, iff->bitmap_compression);
401 bytestream_put_byte(&buf, iff->bpp);
402 bytestream_put_byte(&buf, iff->ham);
403 bytestream_put_byte(&buf, iff->flags);
404 bytestream_put_be16(&buf, iff->transparency);
405 bytestream_put_byte(&buf, iff->masking);
406 st->codec->codec_id = AV_CODEC_ID_IFF_ILBM;
415 static int iff_read_packet(AVFormatContext *s,
418 IffDemuxContext *iff = s->priv_data;
419 AVIOContext *pb = s->pb;
420 AVStream *st = s->streams[0];
423 if(iff->sent_bytes >= iff->body_size)
426 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
427 if (st->codec->codec_tag == ID_MAUD) {
428 ret = av_get_packet(pb, pkt,
429 FFMIN(iff->body_size - iff->sent_bytes, 1024 * st->codec->block_align));
431 ret = av_get_packet(pb, pkt, iff->body_size);
433 } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
436 if (av_new_packet(pkt, iff->body_size + 2) < 0) {
437 return AVERROR(ENOMEM);
441 bytestream_put_be16(&buf, 2);
442 ret = avio_read(pb, buf, iff->body_size);
447 if(iff->sent_bytes == 0)
448 pkt->flags |= AV_PKT_FLAG_KEY;
451 iff->sent_bytes += ret;
452 pkt->stream_index = 0;
456 AVInputFormat ff_iff_demuxer = {
458 .long_name = NULL_IF_CONFIG_SMALL("IFF (Interchange File Format)"),
459 .priv_data_size = sizeof(IffDemuxContext),
460 .read_probe = iff_probe,
461 .read_header = iff_read_header,
462 .read_packet = iff_read_packet,