2008-03-30 21:17:01 +02:00
|
|
|
/*
|
2008-03-31 19:31:11 +02:00
|
|
|
* IFF (.iff) file demuxer
|
2008-03-30 21:17:01 +02:00
|
|
|
* Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
|
2010-02-03 10:57:21 +01:00
|
|
|
* Copyright (c) 2010 Peter Ross <pross@xvid.org>
|
2010-04-27 16:03:47 +02:00
|
|
|
* Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
|
2008-03-30 21:17:01 +02:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2008-03-31 19:31:11 +02:00
|
|
|
* IFF file demuxer
|
2008-03-30 21:17:01 +02:00
|
|
|
* by Jaikrishnan Menon
|
|
|
|
* for more information on the .iff file format, visit:
|
|
|
|
* http://wiki.multimedia.cx/index.php?title=IFF
|
|
|
|
*/
|
|
|
|
|
2009-01-11 23:19:48 +01:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2010-03-08 03:36:00 +01:00
|
|
|
#include "libavcodec/iff.h"
|
2008-03-30 21:17:01 +02:00
|
|
|
#include "avformat.h"
|
|
|
|
|
|
|
|
#define ID_8SVX MKTAG('8','S','V','X')
|
|
|
|
#define ID_VHDR MKTAG('V','H','D','R')
|
|
|
|
#define ID_ATAK MKTAG('A','T','A','K')
|
|
|
|
#define ID_RLSE MKTAG('R','L','S','E')
|
|
|
|
#define ID_CHAN MKTAG('C','H','A','N')
|
2010-02-03 10:57:21 +01:00
|
|
|
#define ID_PBM MKTAG('P','B','M',' ')
|
|
|
|
#define ID_ILBM MKTAG('I','L','B','M')
|
|
|
|
#define ID_BMHD MKTAG('B','M','H','D')
|
|
|
|
#define ID_CMAP MKTAG('C','M','A','P')
|
2008-03-30 21:17:01 +02:00
|
|
|
|
|
|
|
#define ID_FORM MKTAG('F','O','R','M')
|
|
|
|
#define ID_ANNO MKTAG('A','N','N','O')
|
|
|
|
#define ID_AUTH MKTAG('A','U','T','H')
|
|
|
|
#define ID_CHRS MKTAG('C','H','R','S')
|
|
|
|
#define ID_COPYRIGHT MKTAG('(','c',')',' ')
|
|
|
|
#define ID_CSET MKTAG('C','S','E','T')
|
|
|
|
#define ID_FVER MKTAG('F','V','E','R')
|
|
|
|
#define ID_NAME MKTAG('N','A','M','E')
|
|
|
|
#define ID_TEXT MKTAG('T','E','X','T')
|
|
|
|
#define ID_BODY MKTAG('B','O','D','Y')
|
2010-02-16 10:55:56 +01:00
|
|
|
#define ID_ANNO MKTAG('A','N','N','O')
|
2008-03-30 21:17:01 +02:00
|
|
|
|
|
|
|
#define LEFT 2
|
|
|
|
#define RIGHT 4
|
|
|
|
#define STEREO 6
|
|
|
|
|
|
|
|
#define PACKET_SIZE 1024
|
|
|
|
|
2010-04-20 20:40:45 +02:00
|
|
|
typedef enum {
|
|
|
|
COMP_NONE,
|
|
|
|
COMP_FIB,
|
|
|
|
COMP_EXP
|
|
|
|
} svx8_compression_type;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
BITMAP_RAW,
|
|
|
|
BITMAP_BYTERUN1
|
|
|
|
} bitmap_compression_type;
|
2008-03-30 21:17:01 +02:00
|
|
|
|
|
|
|
typedef struct {
|
2010-04-27 16:03:47 +02:00
|
|
|
uint64_t body_pos;
|
2008-03-30 21:17:01 +02:00
|
|
|
uint32_t body_size;
|
|
|
|
uint32_t sent_bytes;
|
|
|
|
uint32_t audio_frame_count;
|
|
|
|
} IffDemuxContext;
|
|
|
|
|
2008-05-09 13:25:27 +02:00
|
|
|
|
|
|
|
static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
|
|
|
|
{
|
|
|
|
uint8_t *end = dest + size;
|
|
|
|
size = size>>1;
|
|
|
|
|
|
|
|
while(dest < end) {
|
|
|
|
*dest++ = *src;
|
|
|
|
*dest++ = *(src+size);
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-30 21:17:01 +02:00
|
|
|
static int iff_probe(AVProbeData *p)
|
|
|
|
{
|
|
|
|
const uint8_t *d = p->buf;
|
|
|
|
|
|
|
|
if ( AV_RL32(d) == ID_FORM &&
|
2010-02-03 10:57:21 +01:00
|
|
|
(AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
|
2008-03-30 21:17:01 +02:00
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iff_read_header(AVFormatContext *s,
|
|
|
|
AVFormatParameters *ap)
|
|
|
|
{
|
|
|
|
IffDemuxContext *iff = s->priv_data;
|
|
|
|
ByteIOContext *pb = s->pb;
|
|
|
|
AVStream *st;
|
|
|
|
uint32_t chunk_id, data_size;
|
2010-02-03 10:57:21 +01:00
|
|
|
int compression = -1;
|
2010-02-16 10:55:56 +01:00
|
|
|
char *buf;
|
2008-03-30 21:17:01 +02:00
|
|
|
|
|
|
|
st = av_new_stream(s, 0);
|
|
|
|
if (!st)
|
2010-04-27 16:04:11 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2008-03-30 21:17:01 +02:00
|
|
|
|
|
|
|
st->codec->channels = 1;
|
2010-02-03 10:57:21 +01:00
|
|
|
url_fskip(pb, 8);
|
|
|
|
// codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
|
|
|
|
st->codec->codec_tag = get_le32(pb);
|
2008-03-30 21:17:01 +02:00
|
|
|
|
2010-04-27 16:03:47 +02:00
|
|
|
while(!url_feof(pb)) {
|
|
|
|
uint64_t orig_pos;
|
2008-03-30 21:17:01 +02:00
|
|
|
chunk_id = get_le32(pb);
|
|
|
|
data_size = get_be32(pb);
|
2010-04-27 16:03:47 +02:00
|
|
|
orig_pos = url_ftell(pb);
|
2008-03-30 21:17:01 +02:00
|
|
|
|
|
|
|
switch(chunk_id) {
|
|
|
|
case ID_VHDR:
|
2010-03-31 01:30:55 +02:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
|
2010-04-27 16:03:47 +02:00
|
|
|
|
|
|
|
if (data_size < 14)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2008-03-30 21:17:01 +02:00
|
|
|
url_fskip(pb, 12);
|
|
|
|
st->codec->sample_rate = get_be16(pb);
|
2010-04-27 16:03:47 +02:00
|
|
|
if (data_size >= 16) {
|
2010-04-27 16:04:11 +02:00
|
|
|
url_fskip(pb, 1);
|
|
|
|
compression = get_byte(pb);
|
2010-04-27 16:03:47 +02:00
|
|
|
}
|
2008-03-30 21:17:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ID_BODY:
|
2010-04-27 16:03:47 +02:00
|
|
|
iff->body_pos = url_ftell(pb);
|
2008-03-30 21:17:01 +02:00
|
|
|
iff->body_size = data_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ID_CHAN:
|
2010-04-27 16:03:47 +02:00
|
|
|
if (data_size < 4)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2008-03-30 21:17:01 +02:00
|
|
|
st->codec->channels = (get_be32(pb) < 6) ? 1 : 2;
|
|
|
|
break;
|
|
|
|
|
2010-02-03 10:57:21 +01:00
|
|
|
case ID_CMAP:
|
|
|
|
st->codec->extradata_size = data_size;
|
|
|
|
st->codec->extradata = av_malloc(data_size);
|
|
|
|
if (!st->codec->extradata)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
if (get_buffer(pb, st->codec->extradata, data_size) < 0)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ID_BMHD:
|
2010-03-31 01:30:55 +02:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
2010-04-27 16:03:47 +02:00
|
|
|
if (data_size <= 8)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2010-02-03 10:57:21 +01:00
|
|
|
st->codec->width = get_be16(pb);
|
|
|
|
st->codec->height = get_be16(pb);
|
|
|
|
url_fskip(pb, 4); // x, y offset
|
|
|
|
st->codec->bits_per_coded_sample = get_byte(pb);
|
2010-04-27 16:03:47 +02:00
|
|
|
if (data_size >= 11) {
|
2010-04-27 16:04:11 +02:00
|
|
|
url_fskip(pb, 1); // masking
|
|
|
|
compression = get_byte(pb);
|
2010-04-27 16:03:47 +02:00
|
|
|
}
|
|
|
|
if (data_size >= 16) {
|
2010-04-27 16:04:11 +02:00
|
|
|
url_fskip(pb, 3); // paddding, transparent
|
|
|
|
st->sample_aspect_ratio.num = get_byte(pb);
|
|
|
|
st->sample_aspect_ratio.den = get_byte(pb);
|
2010-04-27 16:03:47 +02:00
|
|
|
}
|
2010-02-03 10:57:21 +01:00
|
|
|
break;
|
|
|
|
|
2010-02-16 10:55:56 +01:00
|
|
|
case ID_ANNO:
|
|
|
|
buf = av_malloc(data_size + 1);
|
|
|
|
if (!buf)
|
|
|
|
break;
|
|
|
|
get_buffer(pb, buf, data_size);
|
|
|
|
buf[data_size] = 0;
|
|
|
|
av_metadata_set2(&s->metadata, "comment", buf, AV_METADATA_DONT_STRDUP_VAL);
|
|
|
|
break;
|
2008-03-30 21:17:01 +02:00
|
|
|
}
|
2010-04-27 16:03:47 +02:00
|
|
|
|
|
|
|
url_fskip(pb, data_size - (url_ftell(pb) - orig_pos) + (data_size & 1));
|
2008-03-30 21:17:01 +02:00
|
|
|
}
|
|
|
|
|
2010-04-27 16:03:47 +02:00
|
|
|
url_fseek(pb, iff->body_pos, SEEK_SET);
|
|
|
|
|
2010-02-03 10:57:21 +01:00
|
|
|
switch(st->codec->codec_type) {
|
2010-03-31 01:30:55 +02:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2010-04-20 20:40:45 +02:00
|
|
|
av_set_pts_info(st, 32, 1, st->codec->sample_rate);
|
2008-03-30 21:17:01 +02:00
|
|
|
|
2010-04-20 20:40:45 +02:00
|
|
|
switch(compression) {
|
|
|
|
case COMP_NONE:
|
|
|
|
st->codec->codec_id = CODEC_ID_PCM_S8;
|
|
|
|
break;
|
|
|
|
case COMP_FIB:
|
|
|
|
st->codec->codec_id = CODEC_ID_8SVX_FIB;
|
|
|
|
break;
|
|
|
|
case COMP_EXP:
|
|
|
|
st->codec->codec_id = CODEC_ID_8SVX_EXP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(s, AV_LOG_ERROR, "iff: unknown compression method\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2008-03-30 21:17:01 +02:00
|
|
|
|
2010-04-20 20:40:45 +02:00
|
|
|
st->codec->bits_per_coded_sample = 8;
|
|
|
|
st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
|
|
|
|
st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
|
|
|
|
break;
|
2010-02-03 10:57:21 +01:00
|
|
|
|
2010-03-31 01:30:55 +02:00
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2010-02-03 10:57:21 +01:00
|
|
|
switch (compression) {
|
|
|
|
case BITMAP_RAW:
|
|
|
|
if (st->codec->codec_tag == ID_ILBM) {
|
|
|
|
st->codec->codec_id = CODEC_ID_IFF_ILBM;
|
|
|
|
} else {
|
|
|
|
st->codec->codec_id = CODEC_ID_RAWVIDEO;
|
|
|
|
st->codec->pix_fmt = PIX_FMT_PAL8;
|
|
|
|
st->codec->codec_tag = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BITMAP_BYTERUN1:
|
|
|
|
st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(s, AV_LOG_ERROR, "unknown compression method\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2008-03-30 21:17:01 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iff_read_packet(AVFormatContext *s,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
|
|
|
IffDemuxContext *iff = s->priv_data;
|
|
|
|
ByteIOContext *pb = s->pb;
|
2010-02-03 10:57:21 +01:00
|
|
|
AVStream *st = s->streams[0];
|
2008-03-30 21:17:01 +02:00
|
|
|
int ret;
|
|
|
|
|
2010-02-03 10:57:21 +01:00
|
|
|
if(iff->sent_bytes >= iff->body_size)
|
2008-03-30 21:17:01 +02:00
|
|
|
return AVERROR(EIO);
|
2008-05-09 13:25:27 +02:00
|
|
|
|
|
|
|
if(s->streams[0]->codec->channels == 2) {
|
|
|
|
uint8_t sample_buffer[PACKET_SIZE];
|
|
|
|
|
|
|
|
ret = get_buffer(pb, sample_buffer, PACKET_SIZE);
|
|
|
|
if(av_new_packet(pkt, PACKET_SIZE) < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "iff: cannot allocate packet \n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
|
2010-02-03 10:57:21 +01:00
|
|
|
} else if (s->streams[0]->codec->codec_id == CODEC_ID_RAWVIDEO) {
|
|
|
|
if(av_new_packet(pkt, iff->body_size + AVPALETTE_SIZE) < 0) {
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ff_cmap_read_palette(st->codec, (uint32_t*)(pkt->data + iff->body_size));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
av_freep(&st->codec->extradata);
|
|
|
|
st->codec->extradata_size = 0;
|
|
|
|
|
|
|
|
ret = get_buffer(pb, pkt->data, iff->body_size);
|
2010-03-31 01:30:55 +02:00
|
|
|
} else if (s->streams[0]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
|
2010-02-03 10:57:21 +01:00
|
|
|
ret = av_get_packet(pb, pkt, iff->body_size);
|
|
|
|
} else {
|
2008-05-09 13:25:27 +02:00
|
|
|
ret = av_get_packet(pb, pkt, PACKET_SIZE);
|
|
|
|
}
|
2008-03-30 21:17:01 +02:00
|
|
|
|
|
|
|
if(iff->sent_bytes == 0)
|
2010-03-31 14:29:58 +02:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2008-03-30 21:17:01 +02:00
|
|
|
|
2010-03-31 01:30:55 +02:00
|
|
|
if(s->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
|
2010-02-03 10:58:14 +01:00
|
|
|
iff->sent_bytes += PACKET_SIZE;
|
2010-02-03 10:57:21 +01:00
|
|
|
} else {
|
|
|
|
iff->sent_bytes = iff->body_size;
|
|
|
|
}
|
2008-03-30 21:17:01 +02:00
|
|
|
pkt->stream_index = 0;
|
2010-03-31 01:30:55 +02:00
|
|
|
if(s->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
|
2010-02-03 10:58:14 +01:00
|
|
|
pkt->pts = iff->audio_frame_count;
|
|
|
|
iff->audio_frame_count += ret / s->streams[0]->codec->channels;
|
2010-02-03 10:57:21 +01:00
|
|
|
}
|
2008-03-30 21:17:01 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVInputFormat iff_demuxer = {
|
|
|
|
"IFF",
|
2008-06-03 18:20:54 +02:00
|
|
|
NULL_IF_CONFIG_SMALL("IFF format"),
|
2008-03-30 21:17:01 +02:00
|
|
|
sizeof(IffDemuxContext),
|
|
|
|
iff_probe,
|
|
|
|
iff_read_header,
|
|
|
|
iff_read_packet,
|
|
|
|
};
|