Statistics
| Branch: | Revision:

ffmpeg / libavformat / aiffdec.c @ cea65433

History | View | Annotate | Download (9.61 KB)

1
/*
2
 * AIFF/AIFF-C demuxer
3
 * Copyright (c) 2006  Patrick Guimond
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "libavutil/intfloat_readwrite.h"
23
#include "avformat.h"
24
#include "raw.h"
25
#include "aiff.h"
26

    
27
#define AIFF                    0
28
#define AIFF_C_VERSION1         0xA2805140
29

    
30
typedef struct {
31
    int64_t data_end;
32
} AIFFInputContext;
33

    
34
static enum CodecID aiff_codec_get_id(int bps)
35
{
36
    if (bps <= 8)
37
        return CODEC_ID_PCM_S8;
38
    if (bps <= 16)
39
        return CODEC_ID_PCM_S16BE;
40
    if (bps <= 24)
41
        return CODEC_ID_PCM_S24BE;
42
    if (bps <= 32)
43
        return CODEC_ID_PCM_S32BE;
44

    
45
    /* bigger than 32 isn't allowed  */
46
    return CODEC_ID_NONE;
47
}
48

    
49
/* returns the size of the found tag */
50
static int get_tag(ByteIOContext *pb, uint32_t * tag)
51
{
52
    int size;
53

    
54
    if (url_feof(pb))
55
        return AVERROR(EIO);
56

    
57
    *tag = get_le32(pb);
58
    size = get_be32(pb);
59

    
60
    if (size < 0)
61
        size = 0x7fffffff;
62

    
63
    return size;
64
}
65

    
66
/* Metadata string read */
67
static void get_meta(AVFormatContext *s, const char *key, int size)
68
{
69
    uint8_t str[1024];
70
    int res = get_buffer(s->pb, str, FFMIN(sizeof(str)-1, size));
71
    if (res < 0)
72
        return;
73

    
74
    str[res] = 0;
75
    if (size & 1)
76
        size++;
77
    size -= res;
78
    if (size)
79
        url_fskip(s->pb, size);
80

    
81
    av_metadata_set(&s->metadata, key, str);
82
}
83

    
84
/* Returns the number of sound data frames or negative on error */
85
static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
86
                             int size, unsigned version)
87
{
88
    AVExtFloat ext;
89
    double sample_rate;
90
    unsigned int num_frames;
91

    
92
    if (size & 1)
93
        size++;
94
    codec->codec_type = CODEC_TYPE_AUDIO;
95
    codec->channels = get_be16(pb);
96
    num_frames = get_be32(pb);
97
    codec->bits_per_coded_sample = get_be16(pb);
98

    
99
    get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
100
    sample_rate = av_ext2dbl(ext);          /* 80 bits BE IEEE extended float */
101
    codec->sample_rate = sample_rate;
102
    size -= 18;
103

    
104
    /* Got an AIFF-C? */
105
    if (version == AIFF_C_VERSION1) {
106
        codec->codec_tag = get_le32(pb);
107
        codec->codec_id  = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
108

    
109
        switch (codec->codec_id) {
110
        case CODEC_ID_PCM_S16BE:
111
            codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
112
            codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
113
            break;
114
        case CODEC_ID_ADPCM_IMA_QT:
115
            codec->block_align = 34*codec->channels;
116
            codec->frame_size = 64;
117
            break;
118
        case CODEC_ID_MACE3:
119
            codec->block_align = 2*codec->channels;
120
            codec->frame_size = 6;
121
            break;
122
        case CODEC_ID_MACE6:
123
            codec->block_align = 1*codec->channels;
124
            codec->frame_size = 6;
125
            break;
126
        case CODEC_ID_GSM:
127
            codec->block_align = 33;
128
            codec->frame_size = 160;
129
            break;
130
        case CODEC_ID_QCELP:
131
            codec->block_align = 35;
132
            codec->frame_size= 160;
133
            break;
134
        default:
135
            break;
136
        }
137
        size -= 4;
138
    } else {
139
        /* Need the codec type */
140
        codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
141
        codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
142
    }
143

    
144
    /* Block align needs to be computed in all cases, as the definition
145
     * is specific to applications -> here we use the WAVE format definition */
146
    if (!codec->block_align)
147
        codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
148

    
149
    codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
150
                       codec->sample_rate) * (codec->block_align << 3);
151

    
152
    /* Chunk is over */
153
    if (size)
154
        url_fseek(pb, size, SEEK_CUR);
155

    
156
    return num_frames;
157
}
158

    
159
static int aiff_probe(AVProbeData *p)
160
{
161
    /* check file header */
162
    if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
163
        p->buf[2] == 'R' && p->buf[3] == 'M' &&
164
        p->buf[8] == 'A' && p->buf[9] == 'I' &&
165
        p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
166
        return AVPROBE_SCORE_MAX;
167
    else
168
        return 0;
169
}
170

    
171
/* aiff input */
172
static int aiff_read_header(AVFormatContext *s,
173
                            AVFormatParameters *ap)
174
{
175
    int size, filesize;
176
    int64_t offset = 0;
177
    uint32_t tag;
178
    unsigned version = AIFF_C_VERSION1;
179
    ByteIOContext *pb = s->pb;
180
    AVStream * st;
181
    AIFFInputContext *aiff = s->priv_data;
182

    
183
    /* check FORM header */
184
    filesize = get_tag(pb, &tag);
185
    if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
186
        return AVERROR_INVALIDDATA;
187

    
188
    /* AIFF data type */
189
    tag = get_le32(pb);
190
    if (tag == MKTAG('A', 'I', 'F', 'F'))       /* Got an AIFF file */
191
        version = AIFF;
192
    else if (tag != MKTAG('A', 'I', 'F', 'C'))  /* An AIFF-C file then */
193
        return AVERROR_INVALIDDATA;
194

    
195
    filesize -= 4;
196

    
197
    st = av_new_stream(s, 0);
198
    if (!st)
199
        return AVERROR(ENOMEM);
200

    
201
    while (filesize > 0) {
202
        /* parse different chunks */
203
        size = get_tag(pb, &tag);
204
        if (size < 0)
205
            return size;
206

    
207
        filesize -= size + 8;
208

    
209
        switch (tag) {
210
        case MKTAG('C', 'O', 'M', 'M'):     /* Common chunk */
211
            /* Then for the complete header info */
212
            st->nb_frames = get_aiff_header(pb, st->codec, size, version);
213
            if (st->nb_frames < 0)
214
                return st->nb_frames;
215
            if (offset > 0) // COMM is after SSND
216
                goto got_sound;
217
            break;
218
        case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
219
            version = get_be32(pb);
220
            break;
221
        case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
222
            get_meta(s, "title"    , size);
223
            break;
224
        case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
225
            get_meta(s, "author"   , size);
226
            break;
227
        case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
228
            get_meta(s, "copyright", size);
229
            break;
230
        case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
231
            get_meta(s, "comment"  , size);
232
            break;
233
        case MKTAG('S', 'S', 'N', 'D'):     /* Sampled sound chunk */
234
            aiff->data_end = url_ftell(pb) + size;
235
            offset = get_be32(pb);      /* Offset of sound data */
236
            get_be32(pb);               /* BlockSize... don't care */
237
            offset += url_ftell(pb);    /* Compute absolute data offset */
238
            if (st->codec->block_align)    /* Assume COMM already parsed */
239
                goto got_sound;
240
            if (url_is_streamed(pb)) {
241
                av_log(s, AV_LOG_ERROR, "file is not seekable\n");
242
                return -1;
243
            }
244
            url_fskip(pb, size - 8);
245
            break;
246
        case MKTAG('w', 'a', 'v', 'e'):
247
            if ((uint64_t)size > (1<<30))
248
                return -1;
249
            st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
250
            if (!st->codec->extradata)
251
                return AVERROR(ENOMEM);
252
            st->codec->extradata_size = size;
253
            get_buffer(pb, st->codec->extradata, size);
254
            break;
255
        default: /* Jump */
256
            if (size & 1)   /* Always even aligned */
257
                size++;
258
            url_fskip (pb, size);
259
        }
260
    }
261

    
262
    if (!st->codec->block_align) {
263
        av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
264
        return -1;
265
    }
266

    
267
got_sound:
268
    /* Now positioned, get the sound data start and end */
269
    if (st->nb_frames)
270
        s->file_size = st->nb_frames * st->codec->block_align;
271

    
272
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
273
    st->start_time = 0;
274
    st->duration = st->codec->frame_size ?
275
        st->nb_frames * st->codec->frame_size : st->nb_frames;
276

    
277
    /* Position the stream at the first block */
278
    url_fseek(pb, offset, SEEK_SET);
279

    
280
    return 0;
281
}
282

    
283
#define MAX_SIZE 4096
284

    
285
static int aiff_read_packet(AVFormatContext *s,
286
                            AVPacket *pkt)
287
{
288
    AVStream *st = s->streams[0];
289
    AIFFInputContext *aiff = s->priv_data;
290
    int64_t max_size;
291
    int res, size;
292

    
293
    /* calculate size of remaining data */
294
    max_size = aiff->data_end - url_ftell(s->pb);
295
    if (max_size <= 0)
296
        return AVERROR_EOF;
297

    
298
    /* Now for that packet */
299
    if (st->codec->block_align >= 33) // GSM, QCLP, IMA4
300
        size = st->codec->block_align;
301
    else
302
        size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
303
    size = FFMIN(max_size, size);
304
    res = av_get_packet(s->pb, pkt, size);
305
    if (res < 0)
306
        return res;
307

    
308
    /* Only one stream in an AIFF file */
309
    pkt->stream_index = 0;
310
    return 0;
311
}
312

    
313
AVInputFormat aiff_demuxer = {
314
    "aiff",
315
    NULL_IF_CONFIG_SMALL("Audio IFF"),
316
    sizeof(AIFFInputContext),
317
    aiff_probe,
318
    aiff_read_header,
319
    aiff_read_packet,
320
    NULL,
321
    pcm_read_seek,
322
    .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
323
};