3a027f5e |
/*
* Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net> |
02c4f626 |
* Copyright (c) 2010 Peter Ross <pross@xvid.org> |
46dcabf8 |
* Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com> |
3a027f5e |
*
* 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
*/
/** |
ba87f080 |
* @file |
4477dedc |
* IFF file demuxer |
3a027f5e |
* by Jaikrishnan Menon
* for more information on the .iff file format, visit:
* http://wiki.multimedia.cx/index.php?title=IFF
*/
|
18313287 |
#include "libavcodec/bytestream.h" |
6a5d31ac |
#include "libavutil/intreadwrite.h" |
d2d67e42 |
#include "libavutil/dict.h" |
3a027f5e |
#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') |
02c4f626 |
#define ID_PBM MKTAG('P','B','M',' ')
#define ID_ILBM MKTAG('I','L','B','M')
#define ID_BMHD MKTAG('B','M','H','D') |
18313287 |
#define ID_CAMG MKTAG('C','A','M','G') |
02c4f626 |
#define ID_CMAP MKTAG('C','M','A','P') |
3a027f5e |
#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') |
899605f1 |
#define ID_ANNO MKTAG('A','N','N','O') |
3a027f5e |
#define LEFT 2
#define RIGHT 4
#define STEREO 6
|
18313287 |
/**
* This number of bytes if added at the beginning of each AVPacket
* which contain additional information about video properties
* which has to be shared between demuxer and decoder.
* This number may change between frames, e.g. the demuxer might
* set it to smallest possible size of 2 to indicate that there's
* no extradata changing in this frame.
*/
#define IFF_EXTRA_VIDEO_SIZE 9
|
e5953464 |
typedef enum {
COMP_NONE,
COMP_FIB,
COMP_EXP
} svx8_compression_type;
typedef enum {
BITMAP_RAW,
BITMAP_BYTERUN1
} bitmap_compression_type; |
3a027f5e |
typedef struct { |
46dcabf8 |
uint64_t body_pos; |
3a027f5e |
uint32_t body_size;
uint32_t sent_bytes;
uint32_t audio_frame_count; |
1af99b02 |
svx8_compression_type svx8_compression;
bitmap_compression_type bitmap_compression; ///< delta compression method used |
18313287 |
unsigned bpp; ///< bits per plane to decode (differs from bits_per_coded_sample if HAM)
unsigned ham; ///< 0 if non-HAM or number of hold bits (6 for bpp > 6, 4 otherwise)
unsigned flags; ///< 1 for EHB, 0 is no extra half darkening
unsigned transparency; ///< transparency color index in palette
unsigned masking; ///< masking method used |
3a027f5e |
} IffDemuxContext;
|
8af7dbce |
/* Metadata string read */
static int get_metadata(AVFormatContext *s,
const char *const tag,
const unsigned data_size)
{
uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
if (!buf)
return AVERROR(ENOMEM);
|
e63a3628 |
if (avio_read(s->pb, buf, data_size) < 0) { |
8af7dbce |
av_free(buf);
return AVERROR(EIO);
}
buf[data_size] = 0; |
d2d67e42 |
av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL); |
8af7dbce |
return 0;
}
|
3a027f5e |
static int iff_probe(AVProbeData *p)
{
const uint8_t *d = p->buf;
if ( AV_RL32(d) == ID_FORM && |
02c4f626 |
(AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) ) |
3a027f5e |
return AVPROBE_SCORE_MAX;
return 0;
}
static int iff_read_header(AVFormatContext *s,
AVFormatParameters *ap)
{
IffDemuxContext *iff = s->priv_data; |
471fe57e |
AVIOContext *pb = s->pb; |
3a027f5e |
AVStream *st; |
18313287 |
uint8_t *buf; |
3a027f5e |
uint32_t chunk_id, data_size; |
18313287 |
uint32_t screenmode = 0;
unsigned transparency = 0;
unsigned masking = 0; // no mask |
3a027f5e |
st = av_new_stream(s, 0);
if (!st) |
4f5e6ef1 |
return AVERROR(ENOMEM); |
3a027f5e |
st->codec->channels = 1; |
45a8a02a |
avio_skip(pb, 8); |
02c4f626 |
// codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content |
e63a3628 |
st->codec->codec_tag = avio_rl32(pb); |
3a027f5e |
|
46dcabf8 |
while(!url_feof(pb)) {
uint64_t orig_pos; |
8af7dbce |
int res;
const char *metadata_tag = NULL; |
e63a3628 |
chunk_id = avio_rl32(pb);
data_size = avio_rb32(pb); |
384c9c2f |
orig_pos = avio_tell(pb); |
3a027f5e |
switch(chunk_id) {
case ID_VHDR: |
72415b2a |
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
46dcabf8 |
if (data_size < 14)
return AVERROR_INVALIDDATA; |
45a8a02a |
avio_skip(pb, 12); |
e63a3628 |
st->codec->sample_rate = avio_rb16(pb); |
46dcabf8 |
if (data_size >= 16) { |
45a8a02a |
avio_skip(pb, 1); |
1af99b02 |
iff->svx8_compression = avio_r8(pb); |
46dcabf8 |
} |
3a027f5e |
break;
case ID_BODY: |
384c9c2f |
iff->body_pos = avio_tell(pb); |
3a027f5e |
iff->body_size = data_size;
break;
case ID_CHAN: |
46dcabf8 |
if (data_size < 4)
return AVERROR_INVALIDDATA; |
e63a3628 |
st->codec->channels = (avio_rb32(pb) < 6) ? 1 : 2; |
3a027f5e |
break;
|
18313287 |
case ID_CAMG:
if (data_size < 4)
return AVERROR_INVALIDDATA;
screenmode = avio_rb32(pb);
break;
|
02c4f626 |
case ID_CMAP: |
18313287 |
st->codec->extradata_size = data_size + IFF_EXTRA_VIDEO_SIZE;
st->codec->extradata = av_malloc(data_size + IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE); |
02c4f626 |
if (!st->codec->extradata)
return AVERROR(ENOMEM); |
18313287 |
if (avio_read(pb, st->codec->extradata + IFF_EXTRA_VIDEO_SIZE, data_size) < 0) |
02c4f626 |
return AVERROR(EIO);
break;
case ID_BMHD: |
1af99b02 |
iff->bitmap_compression = -1; |
72415b2a |
st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
46dcabf8 |
if (data_size <= 8)
return AVERROR_INVALIDDATA; |
e63a3628 |
st->codec->width = avio_rb16(pb);
st->codec->height = avio_rb16(pb); |
45a8a02a |
avio_skip(pb, 4); // x, y offset |
e63a3628 |
st->codec->bits_per_coded_sample = avio_r8(pb); |
18313287 |
if (data_size >= 10)
masking = avio_r8(pb);
if (data_size >= 11) |
1af99b02 |
iff->bitmap_compression = avio_r8(pb); |
18313287 |
if (data_size >= 14) {
avio_skip(pb, 1); // padding
transparency = avio_rb16(pb); |
46dcabf8 |
}
if (data_size >= 16) { |
e63a3628 |
st->sample_aspect_ratio.num = avio_r8(pb);
st->sample_aspect_ratio.den = avio_r8(pb); |
46dcabf8 |
} |
02c4f626 |
break;
|
899605f1 |
case ID_ANNO: |
d8353256 |
case ID_TEXT: metadata_tag = "comment"; break;
case ID_AUTH: metadata_tag = "artist"; break;
case ID_COPYRIGHT: metadata_tag = "copyright"; break;
case ID_NAME: metadata_tag = "title"; break; |
3a027f5e |
} |
46dcabf8 |
|
8af7dbce |
if (metadata_tag) {
if ((res = get_metadata(s, metadata_tag, data_size)) < 0) { |
ea15d200 |
av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag); |
8af7dbce |
return res;
}
} |
45a8a02a |
avio_skip(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1)); |
3a027f5e |
}
|
f59d8ff8 |
avio_seek(pb, iff->body_pos, SEEK_SET); |
46dcabf8 |
|
02c4f626 |
switch(st->codec->codec_type) { |
72415b2a |
case AVMEDIA_TYPE_AUDIO: |
e5953464 |
av_set_pts_info(st, 32, 1, st->codec->sample_rate); |
3a027f5e |
|
1af99b02 |
switch (iff->svx8_compression) { |
e5953464 |
case COMP_NONE: |
e280a4da |
st->codec->codec_id = CODEC_ID_8SVX_RAW; |
e5953464 |
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: |
1af99b02 |
av_log(s, AV_LOG_ERROR,
"Unknown SVX8 compression method '%d'\n", iff->svx8_compression); |
e5953464 |
return -1;
} |
3a027f5e |
|
34f590b2 |
st->codec->bits_per_coded_sample = iff->svx8_compression == COMP_NONE ? 8 : 4; |
e5953464 |
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; |
02c4f626 |
|
72415b2a |
case AVMEDIA_TYPE_VIDEO: |
18313287 |
iff->bpp = st->codec->bits_per_coded_sample;
if ((screenmode & 0x800 /* Hold And Modify */) && iff->bpp <= 8) {
iff->ham = iff->bpp > 6 ? 6 : 4;
st->codec->bits_per_coded_sample = 24;
}
iff->flags = (screenmode & 0x80 /* Extra HalfBrite */) && iff->bpp <= 8;
iff->masking = masking;
iff->transparency = transparency;
if (!st->codec->extradata) {
st->codec->extradata_size = IFF_EXTRA_VIDEO_SIZE;
st->codec->extradata = av_malloc(IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
if (!st->codec->extradata)
return AVERROR(ENOMEM);
}
buf = st->codec->extradata;
bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE); |
1af99b02 |
bytestream_put_byte(&buf, iff->bitmap_compression); |
18313287 |
bytestream_put_byte(&buf, iff->bpp);
bytestream_put_byte(&buf, iff->ham);
bytestream_put_byte(&buf, iff->flags);
bytestream_put_be16(&buf, iff->transparency);
bytestream_put_byte(&buf, iff->masking);
|
1af99b02 |
switch (iff->bitmap_compression) { |
02c4f626 |
case BITMAP_RAW: |
299ac811 |
st->codec->codec_id = CODEC_ID_IFF_ILBM; |
02c4f626 |
break;
case BITMAP_BYTERUN1:
st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
break;
default: |
1af99b02 |
av_log(s, AV_LOG_ERROR,
"Unknown bitmap compression method '%d'\n", iff->bitmap_compression); |
02c4f626 |
return AVERROR_INVALIDDATA;
}
break;
default:
return -1;
} |
3a027f5e |
return 0;
}
static int iff_read_packet(AVFormatContext *s,
AVPacket *pkt)
{
IffDemuxContext *iff = s->priv_data; |
471fe57e |
AVIOContext *pb = s->pb; |
02c4f626 |
AVStream *st = s->streams[0]; |
3a027f5e |
int ret;
|
02c4f626 |
if(iff->sent_bytes >= iff->body_size) |
3a027f5e |
return AVERROR(EIO); |
86b2d47f |
|
e280a4da |
if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
ret = av_get_packet(pb, pkt, iff->body_size); |
0613ece5 |
} else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { |
18313287 |
uint8_t *buf;
if (av_new_packet(pkt, iff->body_size + 2) < 0) {
return AVERROR(ENOMEM);
}
buf = pkt->data;
bytestream_put_be16(&buf, 2);
ret = avio_read(pb, buf, iff->body_size); |
b4434475 |
} else {
av_abort(); |
86b2d47f |
} |
3a027f5e |
if(iff->sent_bytes == 0) |
cc947f04 |
pkt->flags |= AV_PKT_FLAG_KEY; |
e280a4da |
iff->sent_bytes = iff->body_size; |
3a027f5e |
pkt->stream_index = 0;
return ret;
}
|
66355be3 |
AVInputFormat ff_iff_demuxer = { |
3a027f5e |
"IFF", |
bde15e74 |
NULL_IF_CONFIG_SMALL("IFF format"), |
3a027f5e |
sizeof(IffDemuxContext),
iff_probe,
iff_read_header,
iff_read_packet,
}; |