Statistics
| Branch: | Revision:

ffmpeg / libavformat / aiff.c @ 4649fa90

History | View | Annotate | Download (13.3 KB)

1
/*
2
 * AIFF/AIFF-C muxer and 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
#include "avformat.h"
22
#include "raw.h"
23
#include "riff.h"
24
#include "intfloat_readwrite.h"
25

    
26
static const AVCodecTag codec_aiff_tags[] = {
27
    { 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
    { CODEC_ID_PCM_S16LE, MKTAG('s','o','w','t') },
38
    { CODEC_ID_ADPCM_IMA_QT, MKTAG('i','m','a','4') },
39
    { CODEC_ID_QDM2, MKTAG('Q','D','M','2') },
40
    { 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
        return AVERROR(EIO);
68

    
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
    if (size)
96
        url_fskip(pb, size);
97
}
98

    
99
/* Returns the number of sound data frames or negative on error */
100
static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
101
                             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
        codec->codec_id  = codec_get_id(codec_aiff_tags, codec->codec_tag);
123

    
124
        switch (codec->codec_id) {
125
        case CODEC_ID_PCM_S16BE:
126
            codec->codec_id = aiff_codec_get_id(codec->bits_per_sample);
127
            codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
128
            break;
129
        case CODEC_ID_ADPCM_IMA_QT:
130
            codec->block_align = 34*codec->channels;
131
            codec->frame_size = 64;
132
            break;
133
        case CODEC_ID_MACE3:
134
        case CODEC_ID_MACE6:
135
            codec->frame_size = 6;
136
            break;
137
        default:
138
            break;
139
        }
140
        size -= 4;
141
    } else {
142
        /* Need the codec type */
143
        codec->codec_id = aiff_codec_get_id(codec->bits_per_sample);
144
        codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
145
    }
146

    
147
    if (!codec->codec_id)
148
        return AVERROR_INVALIDDATA;
149

    
150
    /* Block align needs to be computed in all cases, as the definition
151
     * is specific to applications -> here we use the WAVE format definition */
152
    if (!codec->block_align)
153
        codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
154

    
155
    codec->bit_rate = codec->sample_rate * (codec->block_align << 3);
156

    
157
    /* Chunk is over */
158
    if (size)
159
        url_fseek(pb, size, SEEK_CUR);
160

    
161
    return num_frames;
162
}
163

    
164
#ifdef CONFIG_MUXERS
165
typedef struct {
166
    offset_t form;
167
    offset_t frames;
168
    offset_t ssnd;
169
} AIFFOutputContext;
170

    
171
static int aiff_write_header(AVFormatContext *s)
172
{
173
    AIFFOutputContext *aiff = s->priv_data;
174
    ByteIOContext *pb = s->pb;
175
    AVCodecContext *enc = s->streams[0]->codec;
176
    AVExtFloat sample_rate;
177
    int aifc = 0;
178

    
179
    /* First verify if format is ok */
180
    if (!enc->codec_tag)
181
        return -1;
182
    if (enc->codec_tag != MKTAG('N','O','N','E'))
183
        aifc = 1;
184

    
185
    /* FORM AIFF header */
186
    put_tag(pb, "FORM");
187
    aiff->form = url_ftell(pb);
188
    put_be32(pb, 0);                    /* file length */
189
    put_tag(pb, aifc ? "AIFC" : "AIFF");
190

    
191
    if (aifc) { // compressed audio
192
        enc->bits_per_sample = 16;
193
        if (!enc->block_align) {
194
            av_log(s, AV_LOG_ERROR, "block align not set\n");
195
            return -1;
196
        }
197
        /* Version chunk */
198
        put_tag(pb, "FVER");
199
        put_be32(pb, 4);
200
        put_be32(pb, 0xA2805140);
201
    }
202

    
203
    /* Common chunk */
204
    put_tag(pb, "COMM");
205
    put_be32(pb, aifc ? 24 : 18); /* size */
206
    put_be16(pb, enc->channels);  /* Number of channels */
207

    
208
    aiff->frames = url_ftell(pb);
209
    put_be32(pb, 0);              /* Number of frames */
210

    
211
    if (!enc->bits_per_sample)
212
        enc->bits_per_sample = av_get_bits_per_sample(enc->codec_id);
213
    if (!enc->bits_per_sample) {
214
        av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
215
        return -1;
216
    }
217
    if (!enc->block_align)
218
        enc->block_align = (enc->bits_per_sample * enc->channels) >> 3;
219

    
220
    put_be16(pb, enc->bits_per_sample); /* Sample size */
221

    
222
    sample_rate = av_dbl2ext((double)enc->sample_rate);
223
    put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
224

    
225
    if (aifc) {
226
        put_le32(pb, enc->codec_tag);
227
        put_be16(pb, 0);
228
    }
229

    
230
    /* Sound data chunk */
231
    put_tag(pb, "SSND");
232
    aiff->ssnd = url_ftell(pb);         /* Sound chunk size */
233
    put_be32(pb, 0);                    /* Sound samples data size */
234
    put_be32(pb, 0);                    /* Data offset */
235
    put_be32(pb, 0);                    /* Block-size (block align) */
236

    
237
    av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
238

    
239
    /* Data is starting here */
240
    put_flush_packet(pb);
241

    
242
    return 0;
243
}
244

    
245
static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
246
{
247
    ByteIOContext *pb = s->pb;
248
    put_buffer(pb, pkt->data, pkt->size);
249
    return 0;
250
}
251

    
252
static int aiff_write_trailer(AVFormatContext *s)
253
{
254
    ByteIOContext *pb = s->pb;
255
    AIFFOutputContext *aiff = s->priv_data;
256
    AVCodecContext *enc = s->streams[0]->codec;
257

    
258
    /* Chunks sizes must be even */
259
    offset_t file_size, end_size;
260
    end_size = file_size = url_ftell(pb);
261
    if (file_size & 1) {
262
        put_byte(pb, 0);
263
        end_size++;
264
    }
265

    
266
    if (!url_is_streamed(s->pb)) {
267
        /* File length */
268
        url_fseek(pb, aiff->form, SEEK_SET);
269
        put_be32(pb, file_size - aiff->form - 4);
270

    
271
        /* Number of sample frames */
272
        url_fseek(pb, aiff->frames, SEEK_SET);
273
        put_be32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
274

    
275
        /* Sound Data chunk size */
276
        url_fseek(pb, aiff->ssnd, SEEK_SET);
277
        put_be32(pb, file_size - aiff->ssnd - 4);
278

    
279
        /* return to the end */
280
        url_fseek(pb, end_size, SEEK_SET);
281

    
282
        put_flush_packet(pb);
283
    }
284

    
285
    return 0;
286
}
287
#endif //CONFIG_MUXERS
288

    
289
static int aiff_probe(AVProbeData *p)
290
{
291
    /* check file header */
292
    if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
293
        p->buf[2] == 'R' && p->buf[3] == 'M' &&
294
        p->buf[8] == 'A' && p->buf[9] == 'I' &&
295
        p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
296
        return AVPROBE_SCORE_MAX;
297
    else
298
        return 0;
299
}
300

    
301
/* aiff input */
302
static int aiff_read_header(AVFormatContext *s,
303
                            AVFormatParameters *ap)
304
{
305
    int size, filesize;
306
    offset_t offset = 0;
307
    uint32_t tag;
308
    unsigned version = AIFF_C_VERSION1;
309
    ByteIOContext *pb = s->pb;
310
    AVStream * st = s->streams[0];
311

    
312
    /* check FORM header */
313
    filesize = get_tag(pb, &tag);
314
    if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
315
        return AVERROR_INVALIDDATA;
316

    
317
    /* AIFF data type */
318
    tag = get_le32(pb);
319
    if (tag == MKTAG('A', 'I', 'F', 'F'))       /* Got an AIFF file */
320
        version = AIFF;
321
    else if (tag != MKTAG('A', 'I', 'F', 'C'))  /* An AIFF-C file then */
322
        return AVERROR_INVALIDDATA;
323

    
324
    filesize -= 4;
325

    
326
    st = av_new_stream(s, 0);
327
    if (!st)
328
        return AVERROR(ENOMEM);
329

    
330
    while (filesize > 0) {
331
        /* parse different chunks */
332
        size = get_tag(pb, &tag);
333
        if (size < 0)
334
            return size;
335

    
336
        filesize -= size + 8;
337

    
338
        switch (tag) {
339
        case MKTAG('C', 'O', 'M', 'M'):     /* Common chunk */
340
            /* Then for the complete header info */
341
            st->nb_frames = get_aiff_header (pb, st->codec, size, version);
342
            if (st->nb_frames < 0)
343
                return st->nb_frames;
344
            if (offset > 0) // COMM is after SSND
345
                goto got_sound;
346
            break;
347
        case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
348
            version = get_be32(pb);
349
            break;
350
        case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
351
            get_meta (pb, s->title, sizeof(s->title), size);
352
            break;
353
        case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
354
            get_meta (pb, s->author, sizeof(s->author), size);
355
            break;
356
        case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
357
            get_meta (pb, s->copyright, sizeof(s->copyright), size);
358
            break;
359
        case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
360
            get_meta (pb, s->comment, sizeof(s->comment), size);
361
            break;
362
        case MKTAG('S', 'S', 'N', 'D'):     /* Sampled sound chunk */
363
            offset = get_be32(pb);      /* Offset of sound data */
364
            get_be32(pb);               /* BlockSize... don't care */
365
            offset += url_ftell(pb);    /* Compute absolute data offset */
366
            if (st->codec->codec_id)    /* Assume COMM already parsed */
367
                goto got_sound;
368
            if (url_is_streamed(pb)) {
369
                av_log(s, AV_LOG_ERROR, "file is not seekable\n");
370
                return -1;
371
            }
372
            url_fskip(pb, size - 8);
373
            break;
374
        case MKTAG('w', 'a', 'v', 'e'):
375
            if ((uint64_t)size > (1<<30))
376
                return -1;
377
            st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
378
            if (!st->codec->extradata)
379
                return AVERROR(ENOMEM);
380
            st->codec->extradata_size = size;
381
            get_buffer(pb, st->codec->extradata, size);
382
            break;
383
        default: /* Jump */
384
            if (size & 1)   /* Always even aligned */
385
                size++;
386
            url_fskip (pb, size);
387
        }
388
    }
389

    
390
    /* End of loop and didn't get sound */
391
    return AVERROR_INVALIDDATA;
392

    
393
got_sound:
394
    /* Now positioned, get the sound data start and end */
395
    if (st->nb_frames)
396
        s->file_size = st->nb_frames * st->codec->block_align;
397

    
398
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
399
    st->start_time = 0;
400
    st->duration = st->codec->frame_size ?
401
        st->nb_frames * st->codec->frame_size : st->nb_frames;
402

    
403
    /* Position the stream at the first block */
404
    url_fseek(pb, offset, SEEK_SET);
405

    
406
    return 0;
407
}
408

    
409
#define MAX_SIZE 4096
410

    
411
static int aiff_read_packet(AVFormatContext *s,
412
                            AVPacket *pkt)
413
{
414
    AVStream *st = s->streams[0];
415
    int res;
416

    
417
    /* End of stream may be reached */
418
    if (url_feof(s->pb))
419
        return AVERROR(EIO);
420

    
421
    /* Now for that packet */
422
    res = av_get_packet(s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
423
    if (res < 0)
424
        return res;
425

    
426
    /* Only one stream in an AIFF file */
427
    pkt->stream_index = 0;
428
    return 0;
429
}
430

    
431
static int aiff_read_seek(AVFormatContext *s,
432
                          int stream_index, int64_t timestamp, int flags)
433
{
434
    return pcm_read_seek(s, stream_index, timestamp, flags);
435
}
436

    
437
#ifdef CONFIG_AIFF_DEMUXER
438
AVInputFormat aiff_demuxer = {
439
    "aiff",
440
    "Audio IFF",
441
    0,
442
    aiff_probe,
443
    aiff_read_header,
444
    aiff_read_packet,
445
    NULL,
446
    aiff_read_seek,
447
    .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
448
};
449
#endif
450

    
451
#ifdef CONFIG_AIFF_MUXER
452
AVOutputFormat aiff_muxer = {
453
    "aiff",
454
    "Audio IFF",
455
    "audio/aiff",
456
    "aif,aiff,afc,aifc",
457
    sizeof(AIFFOutputContext),
458
    CODEC_ID_PCM_S16BE,
459
    CODEC_ID_NONE,
460
    aiff_write_header,
461
    aiff_write_packet,
462
    aiff_write_trailer,
463
    .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
464
};
465
#endif