Statistics
| Branch: | Revision:

ffmpeg / libavformat / aiff.c @ b4dc27a8

History | View | Annotate | Download (13.1 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
    { 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(EIO);
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
static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
100
                             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
        switch (codec->codec_id) {
126
        case CODEC_ID_PCM_S16BE:
127
            codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
128
            codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
129
            break;
130
        case CODEC_ID_ADPCM_IMA_QT:
131
            codec->block_align = 34*codec->channels;
132
            codec->frame_size = 64;
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_sample);
141
        codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
142
    }
143

    
144
    if (!codec->codec_id)
145
        return AVERROR_INVALIDDATA;
146

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

    
152
    codec->bit_rate = codec->sample_rate * (codec->block_align << 3);
153

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

    
158
    return num_frames;
159
}
160

    
161
#ifdef CONFIG_MUXERS
162
typedef struct {
163
    offset_t form;
164
    offset_t frames;
165
    offset_t ssnd;
166
} AIFFOutputContext;
167

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

    
176
    /* First verify if format is ok */
177
    if (!enc->codec_tag) {
178
        return -1;
179
    }
180

    
181
    if (enc->codec_tag != MKTAG('N','O','N','E'))
182
        aifc = 1;
183

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

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

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

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

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

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

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

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

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

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

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

    
241
    return 0;
242
}
243

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

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

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

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

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

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

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

    
281
        put_flush_packet(pb);
282
    }
283

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

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

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

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

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

    
323
    filesize -= 4;
324

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

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

    
335
        filesize -= size + 8;
336

    
337
        switch (tag) {
338
            case MKTAG('C', 'O', 'M', 'M'):     /* Common chunk */
339
                /* Then for the complete header info */
340
                st->nb_frames = get_aiff_header (pb, st->codec, size, version);
341
                if (st->nb_frames < 0)
342
                        return st->nb_frames;
343
                if (offset > 0) // COMM is after SSND
344
                    goto got_sound;
345
                break;
346

    
347
            case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
348
                version = get_be32(pb);
349
                break;
350

    
351
            case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
352
                get_meta (pb, s->title, sizeof(s->title), size);
353
                break;
354

    
355
            case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
356
                get_meta (pb, s->author, sizeof(s->author), size);
357
                break;
358

    
359
            case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
360
                get_meta (pb, s->copyright, sizeof(s->copyright), size);
361
                break;
362

    
363
            case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
364
                get_meta (pb, s->comment, sizeof(s->comment), size);
365
                break;
366

    
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

    
380
            default: /* Jump */
381
                if (size & 1)   /* Always even aligned */
382
                    size++;
383
                url_fskip (pb, size);
384
        }
385
    }
386

    
387
    /* End of loop and didn't get sound */
388
    return AVERROR_INVALIDDATA;
389

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

    
395
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
396
    st->start_time = 0;
397
    st->duration = st->codec->frame_size ?
398
        st->nb_frames * st->codec->frame_size : st->nb_frames;
399

    
400
    /* Position the stream at the first block */
401
    url_fseek(pb, offset, SEEK_SET);
402

    
403
    return 0;
404
}
405

    
406
#define MAX_SIZE 4096
407

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

    
414
    /* End of stream may be reached */
415
    if (url_feof(s->pb))
416
        return AVERROR(EIO);
417

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

    
423
    /* Only one stream in an AIFF file */
424
    pkt->stream_index = 0;
425
    return 0;
426
}
427

    
428
static int aiff_read_close(AVFormatContext *s)
429
{
430
    return 0;
431
}
432

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

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

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