Statistics
| Branch: | Revision:

ffmpeg / libavformat / iff.c @ e63a3628

History | View | Annotate | Download (9.32 KB)

1
/*
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>
6
 *
7
 * This file is part of FFmpeg.
8
 *
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.
13
 *
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.
18
 *
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
22
 */
23

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

    
32
#include "libavutil/intreadwrite.h"
33
#include "avformat.h"
34

    
35
#define ID_8SVX       MKTAG('8','S','V','X')
36
#define ID_VHDR       MKTAG('V','H','D','R')
37
#define ID_ATAK       MKTAG('A','T','A','K')
38
#define ID_RLSE       MKTAG('R','L','S','E')
39
#define ID_CHAN       MKTAG('C','H','A','N')
40
#define ID_PBM        MKTAG('P','B','M',' ')
41
#define ID_ILBM       MKTAG('I','L','B','M')
42
#define ID_BMHD       MKTAG('B','M','H','D')
43
#define ID_CMAP       MKTAG('C','M','A','P')
44

    
45
#define ID_FORM       MKTAG('F','O','R','M')
46
#define ID_ANNO       MKTAG('A','N','N','O')
47
#define ID_AUTH       MKTAG('A','U','T','H')
48
#define ID_CHRS       MKTAG('C','H','R','S')
49
#define ID_COPYRIGHT  MKTAG('(','c',')',' ')
50
#define ID_CSET       MKTAG('C','S','E','T')
51
#define ID_FVER       MKTAG('F','V','E','R')
52
#define ID_NAME       MKTAG('N','A','M','E')
53
#define ID_TEXT       MKTAG('T','E','X','T')
54
#define ID_BODY       MKTAG('B','O','D','Y')
55
#define ID_ANNO       MKTAG('A','N','N','O')
56

    
57
#define LEFT    2
58
#define RIGHT   4
59
#define STEREO  6
60

    
61
#define PACKET_SIZE 1024
62

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

    
69
typedef enum {
70
    BITMAP_RAW,
71
    BITMAP_BYTERUN1
72
} bitmap_compression_type;
73

    
74
typedef struct {
75
    uint64_t  body_pos;
76
    uint32_t  body_size;
77
    uint32_t  sent_bytes;
78
    uint32_t  audio_frame_count;
79
} IffDemuxContext;
80

    
81

    
82
static void interleave_stereo(const uint8_t *src, uint8_t *dest, int size)
83
{
84
    uint8_t *end = dest + size;
85
    size = size>>1;
86

    
87
    while(dest < end) {
88
        *dest++ = *src;
89
        *dest++ = *(src+size);
90
        src++;
91
    }
92
}
93

    
94
/* Metadata string read */
95
static int get_metadata(AVFormatContext *s,
96
                        const char *const tag,
97
                        const unsigned data_size)
98
{
99
    uint8_t *buf = ((data_size + 1) == 0) ? NULL : av_malloc(data_size + 1);
100

    
101
    if (!buf)
102
        return AVERROR(ENOMEM);
103

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

    
113
static int iff_probe(AVProbeData *p)
114
{
115
    const uint8_t *d = p->buf;
116

    
117
    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) )
119
        return AVPROBE_SCORE_MAX;
120
    return 0;
121
}
122

    
123
static int iff_read_header(AVFormatContext *s,
124
                           AVFormatParameters *ap)
125
{
126
    IffDemuxContext *iff = s->priv_data;
127
    AVIOContext *pb = s->pb;
128
    AVStream *st;
129
    uint32_t chunk_id, data_size;
130
    int compression = -1;
131

    
132
    st = av_new_stream(s, 0);
133
    if (!st)
134
        return AVERROR(ENOMEM);
135

    
136
    st->codec->channels = 1;
137
    url_fskip(pb, 8);
138
    // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
139
    st->codec->codec_tag = avio_rl32(pb);
140

    
141
    while(!url_feof(pb)) {
142
        uint64_t orig_pos;
143
        int res;
144
        const char *metadata_tag = NULL;
145
        chunk_id = avio_rl32(pb);
146
        data_size = avio_rb32(pb);
147
        orig_pos = url_ftell(pb);
148

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

    
153
            if (data_size < 14)
154
                return AVERROR_INVALIDDATA;
155
            url_fskip(pb, 12);
156
            st->codec->sample_rate = avio_rb16(pb);
157
            if (data_size >= 16) {
158
                url_fskip(pb, 1);
159
                compression        = avio_r8(pb);
160
            }
161
            break;
162

    
163
        case ID_BODY:
164
            iff->body_pos = url_ftell(pb);
165
            iff->body_size = data_size;
166
            break;
167

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

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

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

    
202
        case ID_ANNO:
203
        case ID_TEXT:
204
            metadata_tag = "comment";
205
            break;
206

    
207
        case ID_AUTH:
208
            metadata_tag = "artist";
209
            break;
210

    
211
        case ID_COPYRIGHT:
212
            metadata_tag = "copyright";
213
            break;
214

    
215
        case ID_NAME:
216
            metadata_tag = "title";
217
            break;
218
        }
219

    
220
        if (metadata_tag) {
221
            if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
222
                av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
223
                return res;
224
            }
225
        }
226
        url_fskip(pb, data_size - (url_ftell(pb) - orig_pos) + (data_size & 1));
227
    }
228

    
229
    url_fseek(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);
234

    
235
        switch(compression) {
236
        case COMP_NONE:
237
            st->codec->codec_id = CODEC_ID_PCM_S8;
238
            break;
239
        case COMP_FIB:
240
            st->codec->codec_id = CODEC_ID_8SVX_FIB;
241
            break;
242
        case COMP_EXP:
243
            st->codec->codec_id = CODEC_ID_8SVX_EXP;
244
            break;
245
        default:
246
            av_log(s, AV_LOG_ERROR, "unknown compression method\n");
247
            return -1;
248
        }
249

    
250
        st->codec->bits_per_coded_sample = 8;
251
        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * st->codec->bits_per_coded_sample;
252
        st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
253
        break;
254

    
255
    case AVMEDIA_TYPE_VIDEO:
256
        switch (compression) {
257
        case BITMAP_RAW:
258
            st->codec->codec_id = CODEC_ID_IFF_ILBM;
259
            break;
260
        case BITMAP_BYTERUN1:
261
            st->codec->codec_id = CODEC_ID_IFF_BYTERUN1;
262
            break;
263
        default:
264
            av_log(s, AV_LOG_ERROR, "unknown compression method\n");
265
            return AVERROR_INVALIDDATA;
266
        }
267
        break;
268
    default:
269
        return -1;
270
    }
271

    
272
    return 0;
273
}
274

    
275
static int iff_read_packet(AVFormatContext *s,
276
                           AVPacket *pkt)
277
{
278
    IffDemuxContext *iff = s->priv_data;
279
    AVIOContext *pb = s->pb;
280
    AVStream *st = s->streams[0];
281
    int ret;
282

    
283
    if(iff->sent_bytes >= iff->body_size)
284
        return AVERROR(EIO);
285

    
286
    if(st->codec->channels == 2) {
287
        uint8_t sample_buffer[PACKET_SIZE];
288

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

    
301
    if(iff->sent_bytes == 0)
302
        pkt->flags |= AV_PKT_FLAG_KEY;
303

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

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