Statistics
| Branch: | Revision:

ffmpeg / libavformat / aiff.c @ 7caf0cc6

History | View | Annotate | Download (12 KB)

1 f11288da Michael Niedermayer
/*
2 7fbde343 Aurelien Jacobs
 * AIFF/AIFF-C muxer and demuxer
3 f11288da Michael Niedermayer
 * Copyright (c) 2006  Patrick Guimond
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 f11288da Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 f11288da Michael Niedermayer
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 f11288da Michael Niedermayer
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 f11288da Michael Niedermayer
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avformat.h"
22 e006c307 Diego Pettenò
#include "allformats.h"
23 9d9f4119 Måns Rullgård
#include "riff.h"
24 f11288da Michael Niedermayer
#include "intfloat_readwrite.h"
25
26 7caf0cc6 Michael Niedermayer
static const AVCodecTag codec_aiff_tags[] = {
27 f11288da Michael Niedermayer
    { CODEC_ID_PCM_S16BE, MKTAG('N','O','N','E') },
28
    { CODEC_ID_PCM_S8, MKTAG('N','O','N','E') },
29
    { CODEC_ID_PCM_S24BE, MKTAG('N','O','N','E') },
30
    { CODEC_ID_PCM_S32BE, MKTAG('N','O','N','E') },
31
    { CODEC_ID_PCM_ALAW, MKTAG('a','l','a','w') },
32
    { CODEC_ID_PCM_ALAW, MKTAG('A','L','A','W') },
33
    { CODEC_ID_PCM_MULAW, MKTAG('u','l','a','w') },
34
    { CODEC_ID_PCM_MULAW, MKTAG('U','L','A','W') },
35
    { CODEC_ID_MACE3, MKTAG('M','A','C','3') },
36
    { CODEC_ID_MACE6, MKTAG('M','A','C','6') },
37
    { CODEC_ID_GSM, MKTAG('G','S','M',' ') },
38
    { CODEC_ID_ADPCM_G726, MKTAG('G','7','2','6') },
39
    { 0, 0 },
40
};
41
42
#define AIFF                    0
43
#define AIFF_C_VERSION1         0xA2805140
44
45
static int aiff_codec_get_id (int bps)
46
{
47
    if (bps <= 8)
48
        return CODEC_ID_PCM_S8;
49
    if (bps <= 16)
50
        return CODEC_ID_PCM_S16BE;
51
    if (bps <= 24)
52
        return CODEC_ID_PCM_S24BE;
53
    if (bps <= 32)
54
        return CODEC_ID_PCM_S32BE;
55
56
    /* bigger than 32 isn't allowed  */
57
    return 0;
58
}
59
60
/* returns the size of the found tag */
61
static int get_tag(ByteIOContext *pb, uint32_t * tag)
62
{
63
    int size;
64
65
    if (url_feof(pb))
66
        return AVERROR_IO;
67
68
    *tag = get_le32(pb);
69
    size = get_be32(pb);
70
71
    if (size < 0)
72
        size = 0x7fffffff;
73
74
    return size;
75
}
76
77
/* Metadata string read */
78
static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
79
{
80
    int res;
81
82
    if (size > strsize-1)
83
        res = get_buffer(pb, (uint8_t*)str, strsize-1);
84
    else
85
        res = get_buffer(pb, (uint8_t*)str, size);
86
87
    if (res < 0)
88
        return;
89
90
    str[res] = 0;
91
    if (size & 1)
92
        size++;
93
    size -= res;
94
    if (size);
95
        url_fskip(pb, size);
96
}
97
98
/* Returns the number of sound data frames or negative on error */
99 7b49ce2e Stefan Huehner
static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
100 f11288da Michael Niedermayer
                             int size, unsigned version)
101
{
102
    AVExtFloat ext;
103
    double sample_rate;
104
    unsigned int num_frames;
105
106
107
    if (size & 1)
108
        size++;
109
110
    codec->codec_type = CODEC_TYPE_AUDIO;
111
    codec->channels = get_be16(pb);
112
    num_frames = get_be32(pb);
113
    codec->bits_per_sample = get_be16(pb);
114
115
    get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
116
    sample_rate = av_ext2dbl(ext);          /* 80 bits BE IEEE extended float */
117
    codec->sample_rate = sample_rate;
118
    size -= 18;
119
120
    /* Got an AIFF-C? */
121
    if (version == AIFF_C_VERSION1) {
122
        codec->codec_tag = get_le32(pb);
123
        codec->codec_id  = codec_get_id (codec_aiff_tags, codec->codec_tag);
124
125
        if (codec->codec_id == CODEC_ID_PCM_S16BE) {
126
            codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
127 d50756cd Baptiste Coudurier
            codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
128 f11288da Michael Niedermayer
        }
129
130
        size -= 4;
131
    } else {
132
        /* Need the codec type */
133
        codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
134 d50756cd Baptiste Coudurier
        codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
135 f11288da Michael Niedermayer
    }
136
137
    if (!codec->codec_id)
138
        return AVERROR_INVALIDDATA;
139
140
    /* Block align needs to be computed in all cases, as the definition
141
     * is specific to applications -> here we use the WAVE format definition */
142
    codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
143
144 777f4649 Baptiste Coudurier
    codec->bit_rate = codec->sample_rate * (codec->block_align << 3);
145 f11288da Michael Niedermayer
146
    /* Chunk is over */
147
    if (size)
148
        url_fseek(pb, size, SEEK_CUR);
149
150
    return num_frames;
151
}
152
153
#ifdef CONFIG_MUXERS
154
typedef struct {
155
    offset_t form;
156
    offset_t frames;
157
    offset_t ssnd;
158
} AIFFOutputContext;
159
160
static int aiff_write_header(AVFormatContext *s)
161
{
162
    AIFFOutputContext *aiff = s->priv_data;
163
    ByteIOContext *pb = &s->pb;
164
    AVCodecContext *enc = s->streams[0]->codec;
165
    AVExtFloat sample_rate;
166
167
    /* First verify if format is ok */
168
    enc->codec_tag = codec_get_tag(codec_aiff_tags, enc->codec_id);
169
    if (!enc->codec_tag) {
170
        av_free(aiff);
171
        return -1;
172
    }
173
174
    /* FORM AIFF header */
175
    put_tag(pb, "FORM");
176
    aiff->form = url_ftell(pb);
177
    put_be32(pb, 0);                    /* file length */
178
    put_tag(pb, "AIFC");
179
180
    /* Version chunk */
181
    put_tag(pb, "FVER");
182
    put_be32(pb, 4);
183
    put_be32(pb, 0xA2805140);
184
185
    /* Common chunk */
186
    put_tag(pb, "COMM");
187 8d41fe28 Baptiste Coudurier
    put_be32(pb, 24); /* size */
188 f11288da Michael Niedermayer
    put_be16(pb, enc->channels);        /* Number of channels */
189
190
    aiff->frames = url_ftell(pb);
191
    put_be32(pb, 0);                    /* Number of frames */
192
193
    if (!enc->bits_per_sample)
194 e27d9277 Baptiste Coudurier
        enc->bits_per_sample = av_get_bits_per_sample(enc->codec_id);
195
    if (!enc->bits_per_sample) {
196
        av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
197
        return -1;
198
    }
199
    if (!enc->block_align)
200
        enc->block_align = (enc->bits_per_sample * enc->channels) >> 3;
201
202 f11288da Michael Niedermayer
    put_be16(pb, enc->bits_per_sample); /* Sample size */
203
204
    sample_rate = av_dbl2ext((double)enc->sample_rate);
205
    put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
206
207
    put_le32(pb, enc->codec_tag);
208 8d41fe28 Baptiste Coudurier
    put_be16(pb, 0);
209 f11288da Michael Niedermayer
210
    /* Sound data chunk */
211
    put_tag(pb, "SSND");
212
    aiff->ssnd = url_ftell(pb);         /* Sound chunk size */
213
    put_be32(pb, 0);                    /* Sound samples data size */
214
    put_be32(pb, 0);                    /* Data offset */
215
    put_be32(pb, 0);                    /* Block-size (block align) */
216
217
    av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
218
219
    /* Data is starting here */
220
    put_flush_packet(pb);
221
222
    return 0;
223
}
224
225
static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
226
{
227
    ByteIOContext *pb = &s->pb;
228
    put_buffer(pb, pkt->data, pkt->size);
229
    return 0;
230
}
231
232
static int aiff_write_trailer(AVFormatContext *s)
233
{
234
    ByteIOContext *pb = &s->pb;
235
    AIFFOutputContext *aiff = s->priv_data;
236
    AVCodecContext *enc = s->streams[0]->codec;
237
238
    /* Chunks sizes must be even */
239
    offset_t file_size, end_size;
240
    end_size = file_size = url_ftell(pb);
241
    if (file_size & 1) {
242
        put_byte(pb, 0);
243
        end_size++;
244
    }
245
246
    if (!url_is_streamed(&s->pb)) {
247
        /* File length */
248
        url_fseek(pb, aiff->form, SEEK_SET);
249
        put_be32(pb, (uint32_t)(file_size - aiff->form - 4));
250
251
        /* Number of sample frames */
252
        url_fseek(pb, aiff->frames, SEEK_SET);
253
        put_be32(pb, ((uint32_t)(file_size-aiff->ssnd-12))/enc->block_align);
254
255
        /* Sound Data chunk size */
256
        url_fseek(pb, aiff->ssnd, SEEK_SET);
257
        put_be32(pb, (uint32_t)(file_size - aiff->ssnd - 4));
258
259
        /* return to the end */
260
        url_fseek(pb, end_size, SEEK_SET);
261
262
        put_flush_packet(pb);
263
    }
264
265
    return 0;
266
}
267
#endif //CONFIG_MUXERS
268
269
static int aiff_probe(AVProbeData *p)
270
{
271
    /* check file header */
272
    if (p->buf_size < 16)
273
        return 0;
274
    if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
275
        p->buf[2] == 'R' && p->buf[3] == 'M' &&
276
        p->buf[8] == 'A' && p->buf[9] == 'I' &&
277
        p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
278
        return AVPROBE_SCORE_MAX;
279
    else
280
        return 0;
281
}
282
283
/* aiff input */
284
static int aiff_read_header(AVFormatContext *s,
285
                            AVFormatParameters *ap)
286
{
287
    int size, filesize, offset;
288
    uint32_t tag;
289
    unsigned version = AIFF_C_VERSION1;
290
    ByteIOContext *pb = &s->pb;
291
    AVStream * st = s->streams[0];
292
293
    /* check FORM header */
294
    filesize = get_tag(pb, &tag);
295
    if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
296
        return AVERROR_INVALIDDATA;
297
298
    /* AIFF data type */
299
    tag = get_le32(pb);
300
    if (tag == MKTAG('A', 'I', 'F', 'F'))       /* Got an AIFF file */
301
        version = AIFF;
302
    else if (tag != MKTAG('A', 'I', 'F', 'C'))  /* An AIFF-C file then */
303
        return AVERROR_INVALIDDATA;
304
305
    filesize -= 4;
306
307
    st = av_new_stream(s, 0);
308
    if (!st)
309
        return AVERROR_NOMEM;
310
311
    while (filesize > 0) {
312
        /* parse different chunks */
313
        size = get_tag(pb, &tag);
314
        if (size < 0)
315
            return size;
316
317
        filesize -= size + 8;
318
319
        switch (tag) {
320
            case MKTAG('C', 'O', 'M', 'M'):     /* Common chunk */
321
                /* Then for the complete header info */
322
                st->nb_frames = get_aiff_header (pb, st->codec, size, version);
323
                if (st->nb_frames < 0)
324
                        return st->nb_frames;
325
                break;
326
327
            case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
328
                version = get_be32(pb);
329
                break;
330
331
            case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
332
                get_meta (pb, s->title, sizeof(s->title), size);
333
                break;
334
335
            case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
336
                get_meta (pb, s->author, sizeof(s->author), size);
337
                break;
338
339
            case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
340
                get_meta (pb, s->copyright, sizeof(s->copyright), size);
341
                break;
342
343
            case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
344
                get_meta (pb, s->comment, sizeof(s->comment), size);
345
                break;
346
347
            case MKTAG('S', 'S', 'N', 'D'):     /* Sampled sound chunk */
348
                get_be32(pb);               /* Block align... don't care */
349
                offset = get_be32(pb);      /* Offset of sound data */
350
                goto got_sound;
351
352
            default: /* Jump */
353
                if (size & 1)   /* Always even aligned */
354
                    size++;
355
                url_fskip (pb, size);
356
        }
357
    }
358
359
    /* End of loop and didn't get sound */
360
    return AVERROR_INVALIDDATA;
361
362
got_sound:
363
    /* Now positioned, get the sound data start and end */
364
    if (st->nb_frames)
365
        s->file_size = st->nb_frames * st->codec->block_align;
366
367
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
368
    st->start_time = 0;
369
    st->duration = st->nb_frames;
370
371
    /* Position the stream at the first block */
372
    url_fskip(pb, offset);
373
374
    return 0;
375
}
376
377
#define MAX_SIZE 4096
378
379
static int aiff_read_packet(AVFormatContext *s,
380
                            AVPacket *pkt)
381
{
382 a959f0ea Baptiste Coudurier
    AVStream *st = s->streams[0];
383
    int res;
384 f11288da Michael Niedermayer
385
    /* End of stream may be reached */
386
    if (url_feof(&s->pb))
387
        return AVERROR_IO;
388
389
    /* Now for that packet */
390 a959f0ea Baptiste Coudurier
    res = av_get_packet(&s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
391 f11288da Michael Niedermayer
    if (res < 0)
392
        return res;
393
394
    /* Only one stream in an AIFF file */
395
    pkt->stream_index = 0;
396
    return 0;
397
}
398
399
static int aiff_read_close(AVFormatContext *s)
400
{
401
    return 0;
402
}
403
404
static int aiff_read_seek(AVFormatContext *s,
405
                          int stream_index, int64_t timestamp, int flags)
406
{
407
    return pcm_read_seek(s, stream_index, timestamp, flags);
408
}
409
410 ff70e601 Måns Rullgård
#ifdef CONFIG_AIFF_DEMUXER
411
AVInputFormat aiff_demuxer = {
412 f11288da Michael Niedermayer
    "aiff",
413
    "Audio IFF",
414
    0,
415
    aiff_probe,
416
    aiff_read_header,
417
    aiff_read_packet,
418
    aiff_read_close,
419
    aiff_read_seek,
420
};
421 ff70e601 Måns Rullgård
#endif
422 f11288da Michael Niedermayer
423 8a85f46d Diego Biurrun
#ifdef CONFIG_AIFF_MUXER
424 ff70e601 Måns Rullgård
AVOutputFormat aiff_muxer = {
425 f11288da Michael Niedermayer
    "aiff",
426
    "Audio IFF",
427
    "audio/aiff",
428
    "aif,aiff,afc,aifc",
429
    sizeof(AIFFOutputContext),
430
    CODEC_ID_PCM_S16BE,
431
    CODEC_ID_NONE,
432
    aiff_write_header,
433
    aiff_write_packet,
434
    aiff_write_trailer,
435
};
436 ff70e601 Måns Rullgård
#endif