Statistics
| Branch: | Revision:

ffmpeg / libavformat / iff.c @ e356fc57

History | View | Annotate | Download (9.39 KB)

1 3a027f5e Jai Menon
/*
2 4477dedc Diego Biurrun
 * IFF (.iff) file demuxer
3 3a027f5e Jai Menon
 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
4 02c4f626 Peter Ross
 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
5 46dcabf8 Sebastian Vater
 * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
6 3a027f5e Jai Menon
 *
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 ba87f080 Diego Biurrun
 * @file
26 4477dedc Diego Biurrun
 * IFF file demuxer
27 3a027f5e Jai Menon
 * 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 6a5d31ac Diego Biurrun
#include "libavutil/intreadwrite.h"
33 3a027f5e Jai Menon
#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 02c4f626 Peter Ross
#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 3a027f5e Jai Menon
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 899605f1 Peter Ross
#define ID_ANNO       MKTAG('A','N','N','O')
56 3a027f5e Jai Menon
57
#define LEFT    2
58
#define RIGHT   4
59
#define STEREO  6
60
61
#define PACKET_SIZE 1024
62
63 e5953464 Sebastian Vater
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 3a027f5e Jai Menon
74
typedef struct {
75 46dcabf8 Sebastian Vater
    uint64_t  body_pos;
76 3a027f5e Jai Menon
    uint32_t  body_size;
77
    uint32_t  sent_bytes;
78
    uint32_t  audio_frame_count;
79
} IffDemuxContext;
80
81 86b2d47f Jai Menon
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 8af7dbce Sebastian Vater
/* 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 b7effd4e Anton Khirnov
    if (avio_read(s->pb, buf, data_size) < 0) {
105 8af7dbce Sebastian Vater
        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 3a027f5e Jai Menon
static int iff_probe(AVProbeData *p)
114
{
115
    const uint8_t *d = p->buf;
116
117
    if ( AV_RL32(d)   == ID_FORM &&
118 02c4f626 Peter Ross
         (AV_RL32(d+8) == ID_8SVX || AV_RL32(d+8) == ID_PBM || AV_RL32(d+8) == ID_ILBM) )
119 3a027f5e Jai Menon
        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 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
128 3a027f5e Jai Menon
    AVStream *st;
129
    uint32_t chunk_id, data_size;
130 02c4f626 Peter Ross
    int compression = -1;
131 3a027f5e Jai Menon
132
    st = av_new_stream(s, 0);
133
    if (!st)
134 4f5e6ef1 Sebastian Vater
        return AVERROR(ENOMEM);
135 3a027f5e Jai Menon
136
    st->codec->channels = 1;
137 e356fc57 Anton Khirnov
    avio_seek(pb, 8, SEEK_CUR);
138 02c4f626 Peter Ross
    // codec_tag used by ByteRun1 decoder to distinguish progressive (PBM) and interlaced (ILBM) content
139 b7effd4e Anton Khirnov
    st->codec->codec_tag = avio_rl32(pb);
140 3a027f5e Jai Menon
141 46dcabf8 Sebastian Vater
    while(!url_feof(pb)) {
142
        uint64_t orig_pos;
143 8af7dbce Sebastian Vater
        int res;
144
        const char *metadata_tag = NULL;
145 b7effd4e Anton Khirnov
        chunk_id = avio_rl32(pb);
146
        data_size = avio_rb32(pb);
147 46dcabf8 Sebastian Vater
        orig_pos = url_ftell(pb);
148 3a027f5e Jai Menon
149
        switch(chunk_id) {
150
        case ID_VHDR:
151 72415b2a Stefano Sabatini
            st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
152 46dcabf8 Sebastian Vater
153
            if (data_size < 14)
154
                return AVERROR_INVALIDDATA;
155 e356fc57 Anton Khirnov
            avio_seek(pb, 12, SEEK_CUR);
156 b7effd4e Anton Khirnov
            st->codec->sample_rate = avio_rb16(pb);
157 46dcabf8 Sebastian Vater
            if (data_size >= 16) {
158 e356fc57 Anton Khirnov
                avio_seek(pb, 1, SEEK_CUR);
159 b7effd4e Anton Khirnov
                compression        = avio_r8(pb);
160 46dcabf8 Sebastian Vater
            }
161 3a027f5e Jai Menon
            break;
162
163
        case ID_BODY:
164 46dcabf8 Sebastian Vater
            iff->body_pos = url_ftell(pb);
165 3a027f5e Jai Menon
            iff->body_size = data_size;
166
            break;
167
168
        case ID_CHAN:
169 46dcabf8 Sebastian Vater
            if (data_size < 4)
170
                return AVERROR_INVALIDDATA;
171 b7effd4e Anton Khirnov
            st->codec->channels = (avio_rb32(pb) < 6) ? 1 : 2;
172 3a027f5e Jai Menon
            break;
173
174 02c4f626 Peter Ross
        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 b7effd4e Anton Khirnov
            if (avio_read(pb, st->codec->extradata, data_size) < 0)
180 02c4f626 Peter Ross
                return AVERROR(EIO);
181
            break;
182
183
        case ID_BMHD:
184 72415b2a Stefano Sabatini
            st->codec->codec_type            = AVMEDIA_TYPE_VIDEO;
185 46dcabf8 Sebastian Vater
            if (data_size <= 8)
186
                return AVERROR_INVALIDDATA;
187 b7effd4e Anton Khirnov
            st->codec->width                 = avio_rb16(pb);
188
            st->codec->height                = avio_rb16(pb);
189 e356fc57 Anton Khirnov
            avio_seek(pb, 4, SEEK_CUR); // x, y offset
190 b7effd4e Anton Khirnov
            st->codec->bits_per_coded_sample = avio_r8(pb);
191 46dcabf8 Sebastian Vater
            if (data_size >= 11) {
192 e356fc57 Anton Khirnov
                avio_seek(pb, 1, SEEK_CUR); // masking
193 b7effd4e Anton Khirnov
                compression                  = avio_r8(pb);
194 46dcabf8 Sebastian Vater
            }
195
            if (data_size >= 16) {
196 e356fc57 Anton Khirnov
                avio_seek(pb, 3, SEEK_CUR); // paddding, transparent
197 b7effd4e Anton Khirnov
                st->sample_aspect_ratio.num  = avio_r8(pb);
198
                st->sample_aspect_ratio.den  = avio_r8(pb);
199 46dcabf8 Sebastian Vater
            }
200 02c4f626 Peter Ross
            break;
201
202 899605f1 Peter Ross
        case ID_ANNO:
203 8af7dbce Sebastian Vater
        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 899605f1 Peter Ross
            break;
218 3a027f5e Jai Menon
        }
219 46dcabf8 Sebastian Vater
220 8af7dbce Sebastian Vater
        if (metadata_tag) {
221
            if ((res = get_metadata(s, metadata_tag, data_size)) < 0) {
222 ea15d200 Peter Ross
                av_log(s, AV_LOG_ERROR, "cannot allocate metadata tag %s!", metadata_tag);
223 8af7dbce Sebastian Vater
                return res;
224
            }
225
        }
226 e356fc57 Anton Khirnov
        avio_seek(pb, data_size - (url_ftell(pb) - orig_pos) + (data_size & 1), SEEK_CUR);
227 3a027f5e Jai Menon
    }
228
229 6b4aa5da Anton Khirnov
    avio_seek(pb, iff->body_pos, SEEK_SET);
230 46dcabf8 Sebastian Vater
231 02c4f626 Peter Ross
    switch(st->codec->codec_type) {
232 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_AUDIO:
233 e5953464 Sebastian Vater
        av_set_pts_info(st, 32, 1, st->codec->sample_rate);
234 3a027f5e Jai Menon
235 e5953464 Sebastian Vater
        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 ea15d200 Peter Ross
            av_log(s, AV_LOG_ERROR, "unknown compression method\n");
247 e5953464 Sebastian Vater
            return -1;
248
        }
249 3a027f5e Jai Menon
250 e5953464 Sebastian Vater
        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 02c4f626 Peter Ross
255 72415b2a Stefano Sabatini
    case AVMEDIA_TYPE_VIDEO:
256 02c4f626 Peter Ross
        switch (compression) {
257
        case BITMAP_RAW:
258 299ac811 Sebastian Vater
            st->codec->codec_id = CODEC_ID_IFF_ILBM;
259 02c4f626 Peter Ross
            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 3a027f5e Jai Menon
272
    return 0;
273
}
274
275
static int iff_read_packet(AVFormatContext *s,
276
                           AVPacket *pkt)
277
{
278
    IffDemuxContext *iff = s->priv_data;
279 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
280 02c4f626 Peter Ross
    AVStream *st = s->streams[0];
281 3a027f5e Jai Menon
    int ret;
282
283 02c4f626 Peter Ross
    if(iff->sent_bytes >= iff->body_size)
284 3a027f5e Jai Menon
        return AVERROR(EIO);
285 86b2d47f Jai Menon
286 0613ece5 Sebastian Vater
    if(st->codec->channels == 2) {
287 86b2d47f Jai Menon
        uint8_t sample_buffer[PACKET_SIZE];
288
289 b7effd4e Anton Khirnov
        ret = avio_read(pb, sample_buffer, PACKET_SIZE);
290 86b2d47f Jai Menon
        if(av_new_packet(pkt, PACKET_SIZE) < 0) {
291 ea15d200 Peter Ross
            av_log(s, AV_LOG_ERROR, "cannot allocate packet\n");
292 86b2d47f Jai Menon
            return AVERROR(ENOMEM);
293
        }
294
        interleave_stereo(sample_buffer, pkt->data, PACKET_SIZE);
295 0613ece5 Sebastian Vater
    } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
296 02c4f626 Peter Ross
        ret = av_get_packet(pb, pkt, iff->body_size);
297
    } else {
298 86b2d47f Jai Menon
        ret = av_get_packet(pb, pkt, PACKET_SIZE);
299
    }
300 3a027f5e Jai Menon
301
    if(iff->sent_bytes == 0)
302 cc947f04 Jean-Daniel Dupas
        pkt->flags |= AV_PKT_FLAG_KEY;
303 3a027f5e Jai Menon
304 0613ece5 Sebastian Vater
    if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
305 451b353d Peter Ross
        iff->sent_bytes += PACKET_SIZE;
306 02c4f626 Peter Ross
    } else {
307
        iff->sent_bytes = iff->body_size;
308
    }
309 3a027f5e Jai Menon
    pkt->stream_index = 0;
310 0613ece5 Sebastian Vater
    if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
311 451b353d Peter Ross
        pkt->pts = iff->audio_frame_count;
312 0613ece5 Sebastian Vater
        iff->audio_frame_count += ret / st->codec->channels;
313 02c4f626 Peter Ross
    }
314 3a027f5e Jai Menon
    return ret;
315
}
316
317 c6610a21 Diego Elio Pettenò
AVInputFormat ff_iff_demuxer = {
318 3a027f5e Jai Menon
    "IFF",
319 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("IFF format"),
320 3a027f5e Jai Menon
    sizeof(IffDemuxContext),
321
    iff_probe,
322
    iff_read_header,
323
    iff_read_packet,
324
};