Statistics
| Branch: | Revision:

ffmpeg / libavformat / aiff.c @ 112249de

History | View | Annotate | Download (13.6 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 7241a6bb Aurelien Jacobs
#include "raw.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_MULAW, MKTAG('u','l','a','w') },
33
    { CODEC_ID_MACE3, MKTAG('M','A','C','3') },
34
    { CODEC_ID_MACE6, MKTAG('M','A','C','6') },
35
    { CODEC_ID_GSM, MKTAG('G','S','M',' ') },
36
    { CODEC_ID_ADPCM_G726, MKTAG('G','7','2','6') },
37 13544287 Kostya Shishkov
    { CODEC_ID_PCM_S16LE, MKTAG('s','o','w','t') },
38 3a957df4 Baptiste Coudurier
    { CODEC_ID_ADPCM_IMA_QT, MKTAG('i','m','a','4') },
39 b8d3daca Baptiste Coudurier
    { CODEC_ID_QDM2, MKTAG('Q','D','M','2') },
40 f11288da Michael Niedermayer
    { 0, 0 },
41
};
42
43
#define AIFF                    0
44
#define AIFF_C_VERSION1         0xA2805140
45
46
static int aiff_codec_get_id (int bps)
47
{
48
    if (bps <= 8)
49
        return CODEC_ID_PCM_S8;
50
    if (bps <= 16)
51
        return CODEC_ID_PCM_S16BE;
52
    if (bps <= 24)
53
        return CODEC_ID_PCM_S24BE;
54
    if (bps <= 32)
55
        return CODEC_ID_PCM_S32BE;
56
57
    /* bigger than 32 isn't allowed  */
58
    return 0;
59
}
60
61
/* returns the size of the found tag */
62
static int get_tag(ByteIOContext *pb, uint32_t * tag)
63
{
64
    int size;
65
66
    if (url_feof(pb))
67 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
68 f11288da Michael Niedermayer
69
    *tag = get_le32(pb);
70
    size = get_be32(pb);
71
72
    if (size < 0)
73
        size = 0x7fffffff;
74
75
    return size;
76
}
77
78
/* Metadata string read */
79
static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
80
{
81
    int res;
82
83
    if (size > strsize-1)
84
        res = get_buffer(pb, (uint8_t*)str, strsize-1);
85
    else
86
        res = get_buffer(pb, (uint8_t*)str, size);
87
88
    if (res < 0)
89
        return;
90
91
    str[res] = 0;
92
    if (size & 1)
93
        size++;
94
    size -= res;
95 a492ad9f Baptiste Coudurier
    if (size)
96 f11288da Michael Niedermayer
        url_fskip(pb, size);
97
}
98
99
/* Returns the number of sound data frames or negative on error */
100 7b49ce2e Stefan Huehner
static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
101 f11288da Michael Niedermayer
                             int size, unsigned version)
102
{
103
    AVExtFloat ext;
104
    double sample_rate;
105
    unsigned int num_frames;
106
107
    if (size & 1)
108
        size++;
109
    codec->codec_type = CODEC_TYPE_AUDIO;
110
    codec->channels = get_be16(pb);
111
    num_frames = get_be32(pb);
112
    codec->bits_per_sample = get_be16(pb);
113
114
    get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
115
    sample_rate = av_ext2dbl(ext);          /* 80 bits BE IEEE extended float */
116
    codec->sample_rate = sample_rate;
117
    size -= 18;
118
119
    /* Got an AIFF-C? */
120
    if (version == AIFF_C_VERSION1) {
121
        codec->codec_tag = get_le32(pb);
122 9df21f24 Baptiste Coudurier
        codec->codec_id  = codec_get_id(codec_aiff_tags, codec->codec_tag);
123 f11288da Michael Niedermayer
124 3a957df4 Baptiste Coudurier
        switch (codec->codec_id) {
125
        case CODEC_ID_PCM_S16BE:
126 9df21f24 Baptiste Coudurier
            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 3a957df4 Baptiste Coudurier
            break;
129
        case CODEC_ID_ADPCM_IMA_QT:
130
            codec->block_align = 34*codec->channels;
131 e0130159 Baptiste Coudurier
            codec->frame_size = 64;
132 3a957df4 Baptiste Coudurier
            break;
133 4649fa90 Baptiste Coudurier
        case CODEC_ID_MACE3:
134 112249de Baptiste Coudurier
            codec->block_align = 2*codec->channels;
135
            codec->frame_size = 6;
136
            break;
137 4649fa90 Baptiste Coudurier
        case CODEC_ID_MACE6:
138 112249de Baptiste Coudurier
            codec->block_align = 1*codec->channels;
139 4649fa90 Baptiste Coudurier
            codec->frame_size = 6;
140
            break;
141 3a957df4 Baptiste Coudurier
        default:
142
            break;
143 f11288da Michael Niedermayer
        }
144
        size -= 4;
145
    } else {
146
        /* Need the codec type */
147 9df21f24 Baptiste Coudurier
        codec->codec_id = aiff_codec_get_id(codec->bits_per_sample);
148 d50756cd Baptiste Coudurier
        codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
149 f11288da Michael Niedermayer
    }
150
151
    if (!codec->codec_id)
152
        return AVERROR_INVALIDDATA;
153
154
    /* Block align needs to be computed in all cases, as the definition
155
     * is specific to applications -> here we use the WAVE format definition */
156 3a957df4 Baptiste Coudurier
    if (!codec->block_align)
157
        codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
158 f11288da Michael Niedermayer
159 28da7981 Baptiste Coudurier
    codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
160
                       codec->sample_rate) * (codec->block_align << 3);
161 f11288da Michael Niedermayer
162
    /* Chunk is over */
163
    if (size)
164
        url_fseek(pb, size, SEEK_CUR);
165
166
    return num_frames;
167
}
168
169
#ifdef CONFIG_MUXERS
170
typedef struct {
171
    offset_t form;
172
    offset_t frames;
173
    offset_t ssnd;
174
} AIFFOutputContext;
175
176
static int aiff_write_header(AVFormatContext *s)
177
{
178
    AIFFOutputContext *aiff = s->priv_data;
179 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
180 f11288da Michael Niedermayer
    AVCodecContext *enc = s->streams[0]->codec;
181
    AVExtFloat sample_rate;
182 400615b6 Baptiste Coudurier
    int aifc = 0;
183 f11288da Michael Niedermayer
184
    /* First verify if format is ok */
185 9df21f24 Baptiste Coudurier
    if (!enc->codec_tag)
186 f11288da Michael Niedermayer
        return -1;
187 400615b6 Baptiste Coudurier
    if (enc->codec_tag != MKTAG('N','O','N','E'))
188
        aifc = 1;
189
190 f11288da Michael Niedermayer
    /* FORM AIFF header */
191
    put_tag(pb, "FORM");
192
    aiff->form = url_ftell(pb);
193
    put_be32(pb, 0);                    /* file length */
194 400615b6 Baptiste Coudurier
    put_tag(pb, aifc ? "AIFC" : "AIFF");
195 f11288da Michael Niedermayer
196 e13c0783 Baptiste Coudurier
    if (aifc) { // compressed audio
197
        enc->bits_per_sample = 16;
198
        if (!enc->block_align) {
199
            av_log(s, AV_LOG_ERROR, "block align not set\n");
200
            return -1;
201
        }
202 8d97aedb Baptiste Coudurier
        /* Version chunk */
203
        put_tag(pb, "FVER");
204
        put_be32(pb, 4);
205
        put_be32(pb, 0xA2805140);
206 400615b6 Baptiste Coudurier
    }
207 f11288da Michael Niedermayer
208
    /* Common chunk */
209
    put_tag(pb, "COMM");
210 400615b6 Baptiste Coudurier
    put_be32(pb, aifc ? 24 : 18); /* size */
211 9df21f24 Baptiste Coudurier
    put_be16(pb, enc->channels);  /* Number of channels */
212 f11288da Michael Niedermayer
213
    aiff->frames = url_ftell(pb);
214 9df21f24 Baptiste Coudurier
    put_be32(pb, 0);              /* Number of frames */
215 f11288da Michael Niedermayer
216
    if (!enc->bits_per_sample)
217 e27d9277 Baptiste Coudurier
        enc->bits_per_sample = av_get_bits_per_sample(enc->codec_id);
218
    if (!enc->bits_per_sample) {
219
        av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
220
        return -1;
221
    }
222
    if (!enc->block_align)
223
        enc->block_align = (enc->bits_per_sample * enc->channels) >> 3;
224
225 f11288da Michael Niedermayer
    put_be16(pb, enc->bits_per_sample); /* Sample size */
226
227
    sample_rate = av_dbl2ext((double)enc->sample_rate);
228
    put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
229
230 400615b6 Baptiste Coudurier
    if (aifc) {
231 8d97aedb Baptiste Coudurier
        put_le32(pb, enc->codec_tag);
232
        put_be16(pb, 0);
233 400615b6 Baptiste Coudurier
    }
234 f11288da Michael Niedermayer
235
    /* Sound data chunk */
236
    put_tag(pb, "SSND");
237
    aiff->ssnd = url_ftell(pb);         /* Sound chunk size */
238
    put_be32(pb, 0);                    /* Sound samples data size */
239
    put_be32(pb, 0);                    /* Data offset */
240
    put_be32(pb, 0);                    /* Block-size (block align) */
241
242
    av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
243
244
    /* Data is starting here */
245
    put_flush_packet(pb);
246
247
    return 0;
248
}
249
250
static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
251
{
252 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
253 f11288da Michael Niedermayer
    put_buffer(pb, pkt->data, pkt->size);
254
    return 0;
255
}
256
257
static int aiff_write_trailer(AVFormatContext *s)
258
{
259 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
260 f11288da Michael Niedermayer
    AIFFOutputContext *aiff = s->priv_data;
261
    AVCodecContext *enc = s->streams[0]->codec;
262
263
    /* Chunks sizes must be even */
264
    offset_t file_size, end_size;
265
    end_size = file_size = url_ftell(pb);
266
    if (file_size & 1) {
267
        put_byte(pb, 0);
268
        end_size++;
269
    }
270
271 899681cd Björn Axelsson
    if (!url_is_streamed(s->pb)) {
272 f11288da Michael Niedermayer
        /* File length */
273
        url_fseek(pb, aiff->form, SEEK_SET);
274 c6f53812 Baptiste Coudurier
        put_be32(pb, file_size - aiff->form - 4);
275 f11288da Michael Niedermayer
276
        /* Number of sample frames */
277
        url_fseek(pb, aiff->frames, SEEK_SET);
278 c6f53812 Baptiste Coudurier
        put_be32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
279 f11288da Michael Niedermayer
280
        /* Sound Data chunk size */
281
        url_fseek(pb, aiff->ssnd, SEEK_SET);
282 c6f53812 Baptiste Coudurier
        put_be32(pb, file_size - aiff->ssnd - 4);
283 f11288da Michael Niedermayer
284
        /* return to the end */
285
        url_fseek(pb, end_size, SEEK_SET);
286
287
        put_flush_packet(pb);
288
    }
289
290
    return 0;
291
}
292
#endif //CONFIG_MUXERS
293
294
static int aiff_probe(AVProbeData *p)
295
{
296
    /* check file header */
297
    if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
298
        p->buf[2] == 'R' && p->buf[3] == 'M' &&
299
        p->buf[8] == 'A' && p->buf[9] == 'I' &&
300
        p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
301
        return AVPROBE_SCORE_MAX;
302
    else
303
        return 0;
304
}
305
306
/* aiff input */
307
static int aiff_read_header(AVFormatContext *s,
308
                            AVFormatParameters *ap)
309
{
310 620d1d78 Baptiste Coudurier
    int size, filesize;
311
    offset_t offset = 0;
312 f11288da Michael Niedermayer
    uint32_t tag;
313
    unsigned version = AIFF_C_VERSION1;
314 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
315 f11288da Michael Niedermayer
    AVStream * st = s->streams[0];
316
317
    /* check FORM header */
318
    filesize = get_tag(pb, &tag);
319
    if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
320
        return AVERROR_INVALIDDATA;
321
322
    /* AIFF data type */
323
    tag = get_le32(pb);
324
    if (tag == MKTAG('A', 'I', 'F', 'F'))       /* Got an AIFF file */
325
        version = AIFF;
326
    else if (tag != MKTAG('A', 'I', 'F', 'C'))  /* An AIFF-C file then */
327
        return AVERROR_INVALIDDATA;
328
329
    filesize -= 4;
330
331
    st = av_new_stream(s, 0);
332
    if (!st)
333 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
334 f11288da Michael Niedermayer
335
    while (filesize > 0) {
336
        /* parse different chunks */
337
        size = get_tag(pb, &tag);
338
        if (size < 0)
339
            return size;
340
341
        filesize -= size + 8;
342
343
        switch (tag) {
344 54d8fd20 Baptiste Coudurier
        case MKTAG('C', 'O', 'M', 'M'):     /* Common chunk */
345
            /* Then for the complete header info */
346
            st->nb_frames = get_aiff_header (pb, st->codec, size, version);
347
            if (st->nb_frames < 0)
348
                return st->nb_frames;
349
            if (offset > 0) // COMM is after SSND
350
                goto got_sound;
351
            break;
352
        case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
353
            version = get_be32(pb);
354
            break;
355
        case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
356
            get_meta (pb, s->title, sizeof(s->title), size);
357
            break;
358
        case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
359
            get_meta (pb, s->author, sizeof(s->author), size);
360
            break;
361
        case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
362
            get_meta (pb, s->copyright, sizeof(s->copyright), size);
363
            break;
364
        case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
365
            get_meta (pb, s->comment, sizeof(s->comment), size);
366
            break;
367
        case MKTAG('S', 'S', 'N', 'D'):     /* Sampled sound chunk */
368
            offset = get_be32(pb);      /* Offset of sound data */
369
            get_be32(pb);               /* BlockSize... don't care */
370
            offset += url_ftell(pb);    /* Compute absolute data offset */
371
            if (st->codec->codec_id)    /* Assume COMM already parsed */
372
                goto got_sound;
373
            if (url_is_streamed(pb)) {
374
                av_log(s, AV_LOG_ERROR, "file is not seekable\n");
375
                return -1;
376
            }
377
            url_fskip(pb, size - 8);
378
            break;
379 b8d3daca Baptiste Coudurier
        case MKTAG('w', 'a', 'v', 'e'):
380 9c7fd997 Baptiste Coudurier
            if ((uint64_t)size > (1<<30))
381
                return -1;
382 b8d3daca Baptiste Coudurier
            st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
383
            if (!st->codec->extradata)
384
                return AVERROR(ENOMEM);
385
            st->codec->extradata_size = size;
386
            get_buffer(pb, st->codec->extradata, size);
387
            break;
388 54d8fd20 Baptiste Coudurier
        default: /* Jump */
389
            if (size & 1)   /* Always even aligned */
390
                size++;
391
            url_fskip (pb, size);
392 f11288da Michael Niedermayer
        }
393
    }
394
395
    /* End of loop and didn't get sound */
396
    return AVERROR_INVALIDDATA;
397
398
got_sound:
399
    /* Now positioned, get the sound data start and end */
400
    if (st->nb_frames)
401
        s->file_size = st->nb_frames * st->codec->block_align;
402
403
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
404
    st->start_time = 0;
405 e0130159 Baptiste Coudurier
    st->duration = st->codec->frame_size ?
406
        st->nb_frames * st->codec->frame_size : st->nb_frames;
407 f11288da Michael Niedermayer
408
    /* Position the stream at the first block */
409 620d1d78 Baptiste Coudurier
    url_fseek(pb, offset, SEEK_SET);
410 f11288da Michael Niedermayer
411
    return 0;
412
}
413
414
#define MAX_SIZE 4096
415
416
static int aiff_read_packet(AVFormatContext *s,
417
                            AVPacket *pkt)
418
{
419 a959f0ea Baptiste Coudurier
    AVStream *st = s->streams[0];
420
    int res;
421 f11288da Michael Niedermayer
422
    /* End of stream may be reached */
423 899681cd Björn Axelsson
    if (url_feof(s->pb))
424 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
425 f11288da Michael Niedermayer
426
    /* Now for that packet */
427 899681cd Björn Axelsson
    res = av_get_packet(s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
428 f11288da Michael Niedermayer
    if (res < 0)
429
        return res;
430
431
    /* Only one stream in an AIFF file */
432
    pkt->stream_index = 0;
433
    return 0;
434
}
435
436
static int aiff_read_seek(AVFormatContext *s,
437
                          int stream_index, int64_t timestamp, int flags)
438
{
439
    return pcm_read_seek(s, stream_index, timestamp, flags);
440
}
441
442 ff70e601 Måns Rullgård
#ifdef CONFIG_AIFF_DEMUXER
443
AVInputFormat aiff_demuxer = {
444 f11288da Michael Niedermayer
    "aiff",
445
    "Audio IFF",
446
    0,
447
    aiff_probe,
448
    aiff_read_header,
449
    aiff_read_packet,
450 4c638f0c Baptiste Coudurier
    NULL,
451 f11288da Michael Niedermayer
    aiff_read_seek,
452 6c77805f Michael Niedermayer
    .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
453 f11288da Michael Niedermayer
};
454 ff70e601 Måns Rullgård
#endif
455 f11288da Michael Niedermayer
456 8a85f46d Diego Biurrun
#ifdef CONFIG_AIFF_MUXER
457 ff70e601 Måns Rullgård
AVOutputFormat aiff_muxer = {
458 f11288da Michael Niedermayer
    "aiff",
459
    "Audio IFF",
460
    "audio/aiff",
461
    "aif,aiff,afc,aifc",
462
    sizeof(AIFFOutputContext),
463
    CODEC_ID_PCM_S16BE,
464
    CODEC_ID_NONE,
465
    aiff_write_header,
466
    aiff_write_packet,
467
    aiff_write_trailer,
468 6c77805f Michael Niedermayer
    .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
469 f11288da Michael Niedermayer
};
470 ff70e601 Måns Rullgård
#endif