Statistics
| Branch: | Revision:

ffmpeg / libavformat / aiff.c @ 470bce2b

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