iff.c 9.4 KB
Newer Older
J
Jai Menon 已提交
1
/*
D
Diego Biurrun 已提交
2
 * IFF (.iff) file demuxer
J
Jai Menon 已提交
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>
J
Jai Menon 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
 */

/**
25
 * @file
D
Diego Biurrun 已提交
26
 * IFF file demuxer
J
Jai Menon 已提交
27 28 29 30 31
 * by Jaikrishnan Menon
 * for more information on the .iff file format, visit:
 * http://wiki.multimedia.cx/index.php?title=IFF
 */

32
#include "libavutil/intreadwrite.h"
J
Jai Menon 已提交
33 34 35 36 37 38 39
#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')
40 41 42 43
#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')
J
Jai Menon 已提交
44 45 46 47 48 49 50 51 52 53 54

#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')
55
#define ID_ANNO       MKTAG('A','N','N','O')
J
Jai Menon 已提交
56 57 58 59 60 61 62

#define LEFT    2
#define RIGHT   4
#define STEREO  6

#define PACKET_SIZE 1024

63 64 65 66 67 68 69 70 71 72
typedef enum {
    COMP_NONE,
    COMP_FIB,
    COMP_EXP
} svx8_compression_type;

typedef enum {
    BITMAP_RAW,
    BITMAP_BYTERUN1
} bitmap_compression_type;
J
Jai Menon 已提交
73 74

typedef struct {
75
    uint64_t  body_pos;
J
Jai Menon 已提交
76 77 78 79 80
    uint32_t  body_size;
    uint32_t  sent_bytes;
    uint32_t  audio_frame_count;
} IffDemuxContext;

J
Jai Menon 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93

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++;
    }
}

S
Sebastian Vater 已提交
94 95 96 97 98 99 100 101 102 103
/* 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);

104
    if (avio_read(s->pb, buf, data_size) < 0) {
S
Sebastian Vater 已提交
105 106 107 108 109 110 111 112
        av_free(buf);
        return AVERROR(EIO);
    }
    buf[data_size] = 0;
    av_metadata_set2(&s->metadata, tag, buf, AV_METADATA_DONT_STRDUP_VAL);
    return 0;
}

J
Jai Menon 已提交
113 114 115 116 117
static int iff_probe(AVProbeData *p)
{
    const uint8_t *d = p->buf;

    if ( AV_RL32(d)   == ID_FORM &&
118
         (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
J
Jai Menon 已提交
119 120 121 122 123 124 125 126
        return AVPROBE_SCORE_MAX;
    return 0;
}

static int iff_read_header(AVFormatContext *s,
                           AVFormatParameters *ap)
{
    IffDemuxContext *iff = s->priv_data;
127
    AVIOContext *pb = s->pb;
J
Jai Menon 已提交
128 129
    AVStream *st;
    uint32_t chunk_id, data_size;
130
    int compression = -1;
J
Jai Menon 已提交
131 132 133

    st = av_new_stream(s, 0);
    if (!st)
S
Sebastian Vater 已提交
134
        return AVERROR(ENOMEM);
J
Jai Menon 已提交
135 136

    st->codec->channels = 1;
137
    avio_seek(pb, 8, SEEK_CUR);
138
    // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
139
    st->codec->codec_tag = avio_rl32(pb);
J
Jai Menon 已提交
140

A
Anton Khirnov 已提交
141
    while(!pb->eof_reached) {
142
        uint64_t orig_pos;
S
Sebastian Vater 已提交
143 144
        int res;
        const char *metadata_tag = NULL;
145 146
        chunk_id = avio_rl32(pb);
        data_size = avio_rb32(pb);
147
        orig_pos = avio_tell(pb);
J
Jai Menon 已提交
148 149 150

        switch(chunk_id) {
        case ID_VHDR:
151
            st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
152 153 154

            if (data_size < 14)
                return AVERROR_INVALIDDATA;
155
            avio_seek(pb, 12, SEEK_CUR);
156
            st->codec->sample_rate = avio_rb16(pb);
157
            if (data_size >= 16) {
158
                avio_seek(pb, 1, SEEK_CUR);
159
                compression        = avio_r8(pb);
160
            }
J
Jai Menon 已提交
161 162 163
            break;

        case ID_BODY:
164
            iff->body_pos = avio_tell(pb);
J
Jai Menon 已提交
165 166 167 168
            iff->body_size = data_size;
            break;

        case ID_CHAN:
169 170
            if (data_size < 4)
                return AVERROR_INVALIDDATA;
171
            st->codec->channels = (avio_rb32(pb) < 6) ? 1 : 2;
J
Jai Menon 已提交
172 173
            break;

174 175 176 177 178
        case ID_CMAP:
            st->codec->extradata_size = data_size;
            st->codec->extradata      = av_malloc(data_size);
            if (!st->codec->extradata)
                return AVERROR(ENOMEM);
179
            if (avio_read(pb, st->codec->extradata, data_size) < 0)
180 181 182 183
                return AVERROR(EIO);
            break;

        case ID_BMHD:
184
            st->codec->codec_type            = AVMEDIA_TYPE_VIDEO;
185 186
            if (data_size <= 8)
                return AVERROR_INVALIDDATA;
187 188
            st->codec->width                 = avio_rb16(pb);
            st->codec->height                = avio_rb16(pb);
189
            avio_seek(pb, 4, SEEK_CUR); // x, y offset
190
            st->codec->bits_per_coded_sample = avio_r8(pb);
191
            if (data_size >= 11) {
192
                avio_seek(pb, 1, SEEK_CUR); // masking
193
                compression                  = avio_r8(pb);
194 195
            }
            if (data_size >= 16) {
196
                avio_seek(pb, 3, SEEK_CUR); // paddding, transparent
197 198
                st->sample_aspect_ratio.num  = avio_r8(pb);
                st->sample_aspect_ratio.den  = avio_r8(pb);
199
            }
200 201
            break;

202
        case ID_ANNO:
S
Sebastian Vater 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216
        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";
217
            break;
J
Jai Menon 已提交
218
        }
219

S
Sebastian Vater 已提交
220 221
        if (metadata_tag) {
            if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
222
                av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
S
Sebastian Vater 已提交
223 224 225
                return res;
            }
        }
226
        avio_seek(pb, data_size - (avio_tell(pb) - orig_pos) + (data_size & 1), SEEK_CUR);
J
Jai Menon 已提交
227 228
    }

A
Anton Khirnov 已提交
229
    avio_seek(pb, iff->body_pos, SEEK_SET);
230

231
    switch(st->codec->codec_type) {
232
    case AVMEDIA_TYPE_AUDIO:
233
        av_set_pts_info(st, 32, 1, st->codec->sample_rate);
J
Jai Menon 已提交
234

235 236 237 238 239 240 241 242 243 244 245
        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:
246
            av_log(s, AV_LOG_ERROR, "unknown compression method\n");
247 248
            return -1;
        }
J
Jai Menon 已提交
249

250 251 252 253
        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;
254

255
    case AVMEDIA_TYPE_VIDEO:
256 257
        switch (compression) {
        case BITMAP_RAW:
S
Sebastian Vater 已提交
258
            st->codec->codec_id = CODEC_ID_IFF_ILBM;
259 260 261 262 263 264 265 266 267 268 269 270
            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;
    }
J
Jai Menon 已提交
271 272 273 274 275 276 277 278

    return 0;
}

static int iff_read_packet(AVFormatContext *s,
                           AVPacket *pkt)
{
    IffDemuxContext *iff = s->priv_data;
279
    AVIOContext *pb = s->pb;
280
    AVStream *st = s->streams[0];
J
Jai Menon 已提交
281 282
    int ret;

283
    if(iff->sent_bytes >= iff->body_size)
J
Jai Menon 已提交
284
        return AVERROR(EIO);
J
Jai Menon 已提交
285

286
    if(st->codec->channels == 2) {
J
Jai Menon 已提交
287 288
        uint8_t sample_buffer[PACKET_SIZE];

289
        ret = avio_read(pb, sample_buffer, PACKET_SIZE);
J
Jai Menon 已提交
290
        if(av_new_packet(pkt, PACKET_SIZE) < 0) {
291
            av_log(s, AV_LOG_ERROR, "cannot allocate packet\n");
J
Jai Menon 已提交
292 293 294
            return AVERROR(ENOMEM);
        }
        interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
295
    } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
296 297
        ret = av_get_packet(pb, pkt, iff->body_size);
    } else {
J
Jai Menon 已提交
298 299
        ret = av_get_packet(pb, pkt, PACKET_SIZE);
    }
J
Jai Menon 已提交
300 301

    if(iff->sent_bytes == 0)
302
        pkt->flags |= AV_PKT_FLAG_KEY;
J
Jai Menon 已提交
303

304
    if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
P
Peter Ross 已提交
305
        iff->sent_bytes += PACKET_SIZE;
306 307 308
    } else {
        iff->sent_bytes = iff->body_size;
    }
J
Jai Menon 已提交
309
    pkt->stream_index = 0;
310
    if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
P
Peter Ross 已提交
311
        pkt->pts = iff->audio_frame_count;
312
        iff->audio_frame_count += ret / st->codec->channels;
313
    }
J
Jai Menon 已提交
314 315 316
    return ret;
}

317
AVInputFormat ff_iff_demuxer = {
J
Jai Menon 已提交
318
    "IFF",
319
    NULL_IF_CONFIG_SMALL("IFF format"),
J
Jai Menon 已提交
320 321 322 323 324
    sizeof(IffDemuxContext),
    iff_probe,
    iff_read_header,
    iff_read_packet,
};