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"
33
#include "libavcodec/iff.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_CMAP MKTAG('C','M','A','P')
46
#define ID_FORM MKTAG('F','O','R','M')
47
#define ID_ANNO MKTAG('A','N','N','O')
48
#define ID_AUTH MKTAG('A','U','T','H')
49
#define ID_CHRS MKTAG('C','H','R','S')
50
#define ID_COPYRIGHT MKTAG('(','c',')',' ')
51
#define ID_CSET MKTAG('C','S','E','T')
52
#define ID_FVER MKTAG('F','V','E','R')
53
#define ID_NAME MKTAG('N','A','M','E')
54
#define ID_TEXT MKTAG('T','E','X','T')
55
#define ID_BODY MKTAG('B','O','D','Y')
56
#define ID_ANNO MKTAG('A','N','N','O')
62
#define PACKET_SIZE 1024
68
} svx8_compression_type;
73
} bitmap_compression_type;
79
uint32_t audio_frame_count;
83
static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
85
uint8_t *end = dest + size;
90
*dest++ = *(src+size);
95
static int iff_probe(AVProbeData *p)
97
const uint8_t *d = p->buf;
99
if ( AV_RL32(d) == ID_FORM &&
100
(AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
101
return AVPROBE_SCORE_MAX;
105
static int iff_read_header(AVFormatContext *s,
106
AVFormatParameters *ap)
108
IffDemuxContext *iff = s->priv_data;
109
ByteIOContext *pb = s->pb;
111
uint32_t chunk_id, data_size;
112
int compression = -1;
115
st = av_new_stream(s, 0);
117
return AVERROR(ENOMEM);
119
st->codec->channels = 1;
121
// codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
122
st->codec->codec_tag = get_le32(pb);
124
while(!url_feof(pb)) {
126
chunk_id = get_le32(pb);
127
data_size = get_be32(pb);
128
orig_pos = url_ftell(pb);
132
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
135
return AVERROR_INVALIDDATA;
137
st->codec->sample_rate = get_be16(pb);
138
if (data_size >= 16) {
140
compression = get_byte(pb);
145
iff->body_pos = url_ftell(pb);
146
iff->body_size = data_size;
151
return AVERROR_INVALIDDATA;
152
st->codec->channels = (get_be32(pb) < 6) ? 1 : 2;
156
st->codec->extradata_size = data_size;
157
st->codec->extradata = av_malloc(data_size);
158
if (!st->codec->extradata)
159
return AVERROR(ENOMEM);
160
if (get_buffer(pb, st->codec->extradata, data_size) < 0)
165
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
167
return AVERROR_INVALIDDATA;
168
st->codec->width = get_be16(pb);
169
st->codec->height = get_be16(pb);
170
url_fskip(pb, 4); // x, y offset
171
st->codec->bits_per_coded_sample = get_byte(pb);
172
if (data_size >= 11) {
173
url_fskip(pb, 1); // masking
174
compression = get_byte(pb);
176
if (data_size >= 16) {
177
url_fskip(pb, 3); // paddding, transparent
178
st->sample_aspect_ratio.num = get_byte(pb);
179
st->sample_aspect_ratio.den = get_byte(pb);
184
buf = av_malloc(data_size + 1);
187
get_buffer(pb, buf, data_size);
189
av_metadata_set2(&s->metadata, "comment", buf, AV_METADATA_DONT_STRDUP_VAL);
193
url_fskip(pb, data_size - (url_ftell(pb) - orig_pos) + (data_size & 1));
196
url_fseek(pb, iff->body_pos, SEEK_SET);
198
switch(st->codec->codec_type) {
199
case AVMEDIA_TYPE_AUDIO:
200
av_set_pts_info(st, 32, 1, st->codec->sample_rate);
202
switch(compression) {
204
st->codec->codec_id = CODEC_ID_PCM_S8;
207
st->codec->codec_id = CODEC_ID_8SVX_FIB;
210
st->codec->codec_id = CODEC_ID_8SVX_EXP;
213
av_log(s, AV_LOG_ERROR, "iff: unknown compression method\n");
217
st->codec->bits_per_coded_sample = 8;
218
st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
219
st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
222
case AVMEDIA_TYPE_VIDEO:
223
switch (compression) {
225
if (st->codec->codec_tag == ID_ILBM) {
226
st->codec->codec_id = CODEC_ID_IFF_ILBM;
228
st->codec->codec_id = CODEC_ID_RAWVIDEO;
229
st->codec->pix_fmt = PIX_FMT_PAL8;
230
st->codec->codec_tag = 0;
233
case BITMAP_BYTERUN1:
234
st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
237
av_log(s, AV_LOG_ERROR, "unknown compression method\n");
238
return AVERROR_INVALIDDATA;
248
static int iff_read_packet(AVFormatContext *s,
251
IffDemuxContext *iff = s->priv_data;
252
ByteIOContext *pb = s->pb;
253
AVStream *st = s->streams[0];
256
if(iff->sent_bytes >= iff->body_size)
259
if(s->streams[0]->codec->channels == 2) {
260
uint8_t sample_buffer[PACKET_SIZE];
262
ret = get_buffer(pb, sample_buffer, PACKET_SIZE);
263
if(av_new_packet(pkt, PACKET_SIZE) < 0) {
264
av_log(s, AV_LOG_ERROR, "iff: cannot allocate packet \n");
265
return AVERROR(ENOMEM);
267
interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
268
} else if (s->streams[0]->codec->codec_id == CODEC_ID_RAWVIDEO) {
269
if(av_new_packet(pkt, iff->body_size + AVPALETTE_SIZE) < 0) {
270
return AVERROR(ENOMEM);
273
ret = ff_cmap_read_palette(st->codec, (uint32_t*)(pkt->data + iff->body_size));
276
av_freep(&st->codec->extradata);
277
st->codec->extradata_size = 0;
279
ret = get_buffer(pb, pkt->data, iff->body_size);
280
} else if (s->streams[0]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
281
ret = av_get_packet(pb, pkt, iff->body_size);
283
ret = av_get_packet(pb, pkt, PACKET_SIZE);
286
if(iff->sent_bytes == 0)
287
pkt->flags |= AV_PKT_FLAG_KEY;
289
if(s->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
290
iff->sent_bytes += PACKET_SIZE;
292
iff->sent_bytes = iff->body_size;
294
pkt->stream_index = 0;
295
if(s->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
296
pkt->pts = iff->audio_frame_count;
297
iff->audio_frame_count += ret / s->streams[0]->codec->channels;
302
AVInputFormat iff_demuxer = {
304
NULL_IF_CONFIG_SMALL("IFF format"),
305
sizeof(IffDemuxContext),