iff.c 9.0 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
 * This file is part of Libav.
J
Jai Menon 已提交
8
 *
9
 * Libav is free software; you can redistribute it and/or
J
Jai Menon 已提交
10 11 12 13
 * 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.
 *
14
 * Libav is distributed in the hope that it will be useful,
J
Jai Menon 已提交
15 16 17 18 19
 * 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
20
 * License along with Libav; if not, write to the Free Software
J
Jai Menon 已提交
21 22 23 24
 * 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 33
#include <inttypes.h>

J
Justin Ruggles 已提交
34
#include "libavutil/channel_layout.h"
35
#include "libavutil/intreadwrite.h"
36
#include "libavutil/dict.h"
J
Jai Menon 已提交
37
#include "avformat.h"
38
#include "internal.h"
J
Jai Menon 已提交
39 40 41 42 43 44

#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')
45 46 47 48
#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 已提交
49 50 51 52 53 54 55 56 57 58 59

#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')
60
#define ID_ANNO       MKTAG('A','N','N','O')
J
Jai Menon 已提交
61 62 63 64 65

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

66 67 68 69 70 71 72 73 74 75
typedef enum {
    COMP_NONE,
    COMP_FIB,
    COMP_EXP
} svx8_compression_type;

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

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

J
Jai Menon 已提交
83

S
Sebastian Vater 已提交
84 85 86 87 88 89 90 91 92 93
/* 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);

94
    if (avio_read(s->pb, buf, data_size) < 0) {
S
Sebastian Vater 已提交
95 96 97 98
        av_free(buf);
        return AVERROR(EIO);
    }
    buf[data_size] = 0;
99
    av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
S
Sebastian Vater 已提交
100 101 102
    return 0;
}

J
Jai Menon 已提交
103 104 105 106 107
static int iff_probe(AVProbeData *p)
{
    const uint8_t *d = p->buf;

    if ( AV_RL32(d)   == ID_FORM &&
108
         (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
J
Jai Menon 已提交
109 110 111 112
        return AVPROBE_SCORE_MAX;
    return 0;
}

113
static int iff_read_header(AVFormatContext *s)
J
Jai Menon 已提交
114 115
{
    IffDemuxContext *iff = s->priv_data;
116
    AVIOContext *pb = s->pb;
J
Jai Menon 已提交
117 118
    AVStream *st;
    uint32_t chunk_id, data_size;
119
    int compression = -1;
J
Jai Menon 已提交
120

121
    st = avformat_new_stream(s, NULL);
J
Jai Menon 已提交
122
    if (!st)
S
Sebastian Vater 已提交
123
        return AVERROR(ENOMEM);
J
Jai Menon 已提交
124 125

    st->codec->channels = 1;
J
Justin Ruggles 已提交
126
    st->codec->channel_layout = AV_CH_LAYOUT_MONO;
127
    avio_skip(pb, 8);
128
    // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
129
    st->codec->codec_tag = avio_rl32(pb);
J
Jai Menon 已提交
130

A
Anton Khirnov 已提交
131
    while(!pb->eof_reached) {
132
        uint64_t orig_pos;
S
Sebastian Vater 已提交
133 134
        int res;
        const char *metadata_tag = NULL;
135 136
        chunk_id = avio_rl32(pb);
        data_size = avio_rb32(pb);
137
        orig_pos = avio_tell(pb);
J
Jai Menon 已提交
138 139 140

        switch(chunk_id) {
        case ID_VHDR:
141
            st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
142 143 144

            if (data_size < 14)
                return AVERROR_INVALIDDATA;
145
            avio_skip(pb, 12);
146
            st->codec->sample_rate = avio_rb16(pb);
147
            if (data_size >= 16) {
148
                avio_skip(pb, 1);
149
                compression        = avio_r8(pb);
150
            }
J
Jai Menon 已提交
151 152 153
            break;

        case ID_BODY:
154
            iff->body_pos = avio_tell(pb);
J
Jai Menon 已提交
155 156 157 158
            iff->body_size = data_size;
            break;

        case ID_CHAN:
159 160
            if (data_size < 4)
                return AVERROR_INVALIDDATA;
J
Justin Ruggles 已提交
161 162 163 164 165 166 167
            if (avio_rb32(pb) < 6) {
                st->codec->channels       = 1;
                st->codec->channel_layout = AV_CH_LAYOUT_MONO;
            } else {
                st->codec->channels       = 2;
                st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
            }
J
Jai Menon 已提交
168 169
            break;

170
        case ID_CMAP:
171
            if (data_size < 3 || data_size > 768 || data_size % 3) {
172
                 av_log(s, AV_LOG_ERROR, "Invalid CMAP chunk size %"PRIu32"\n",
173 174 175
                        data_size);
                 return AVERROR_INVALIDDATA;
            }
176 177 178 179
            st->codec->extradata_size = data_size;
            st->codec->extradata      = av_malloc(data_size);
            if (!st->codec->extradata)
                return AVERROR(ENOMEM);
180
            if (avio_read(pb, st->codec->extradata, data_size) < 0)
181 182 183 184
                return AVERROR(EIO);
            break;

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

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

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

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

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

236 237
        switch(compression) {
        case COMP_NONE:
238
            st->codec->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
239 240
            break;
        case COMP_FIB:
241
            st->codec->codec_id = AV_CODEC_ID_8SVX_FIB;
242 243
            break;
        case COMP_EXP:
244
            st->codec->codec_id = AV_CODEC_ID_8SVX_EXP;
245 246
            break;
        default:
247
            av_log(s, AV_LOG_ERROR, "unknown compression method\n");
248 249
            return -1;
        }
J
Jai Menon 已提交
250

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

256
    case AVMEDIA_TYPE_VIDEO:
257 258
        switch (compression) {
        case BITMAP_RAW:
259
            st->codec->codec_id = AV_CODEC_ID_IFF_ILBM;
260 261
            break;
        case BITMAP_BYTERUN1:
262
            st->codec->codec_id = AV_CODEC_ID_IFF_BYTERUN1;
263 264 265 266 267 268 269 270 271
            break;
        default:
            av_log(s, AV_LOG_ERROR, "unknown compression method\n");
            return AVERROR_INVALIDDATA;
        }
        break;
    default:
        return -1;
    }
J
Jai Menon 已提交
272 273 274 275 276 277 278 279

    return 0;
}

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

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

286 287 288
    ret = av_get_packet(pb, pkt, iff->body_size);
    if (ret < 0)
        return ret;
J
Jai Menon 已提交
289 290

    if(iff->sent_bytes == 0)
291
        pkt->flags |= AV_PKT_FLAG_KEY;
292
    iff->sent_bytes = iff->body_size;
J
Jai Menon 已提交
293 294 295 296 297

    pkt->stream_index = 0;
    return ret;
}

298
AVInputFormat ff_iff_demuxer = {
299
    .name           = "iff",
300
    .long_name      = NULL_IF_CONFIG_SMALL("IFF (Interchange File Format)"),
301 302 303 304
    .priv_data_size = sizeof(IffDemuxContext),
    .read_probe     = iff_probe,
    .read_header    = iff_read_header,
    .read_packet    = iff_read_packet,
J
Jai Menon 已提交
305
};