~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavformat/aiff.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "allformats.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_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
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
        if (codec->codec_id == 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
        }
 
129
 
 
130
        size -= 4;
 
131
    } else {
 
132
        /* Need the codec type */
 
133
        codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
 
134
        codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
 
135
    }
 
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
    codec->bit_rate = codec->sample_rate * (codec->block_align << 3);
 
145
 
 
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
    int aifc = 0;
 
167
 
 
168
    /* First verify if format is ok */
 
169
    if (!enc->codec_tag) {
 
170
        return -1;
 
171
    }
 
172
 
 
173
    if (enc->codec_tag != MKTAG('N','O','N','E'))
 
174
        aifc = 1;
 
175
 
 
176
    /* FORM AIFF header */
 
177
    put_tag(pb, "FORM");
 
178
    aiff->form = url_ftell(pb);
 
179
    put_be32(pb, 0);                    /* file length */
 
180
    put_tag(pb, aifc ? "AIFC" : "AIFF");
 
181
 
 
182
    if (aifc) {
 
183
        /* Version chunk */
 
184
        put_tag(pb, "FVER");
 
185
        put_be32(pb, 4);
 
186
        put_be32(pb, 0xA2805140);
 
187
    }
 
188
 
 
189
    /* Common chunk */
 
190
    put_tag(pb, "COMM");
 
191
    put_be32(pb, aifc ? 24 : 18); /* size */
 
192
    put_be16(pb, enc->channels);        /* Number of channels */
 
193
 
 
194
    aiff->frames = url_ftell(pb);
 
195
    put_be32(pb, 0);                    /* Number of frames */
 
196
 
 
197
    if (!enc->bits_per_sample)
 
198
        enc->bits_per_sample = av_get_bits_per_sample(enc->codec_id);
 
199
    if (!enc->bits_per_sample) {
 
200
        av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
 
201
        return -1;
 
202
    }
 
203
    if (!enc->block_align)
 
204
        enc->block_align = (enc->bits_per_sample * enc->channels) >> 3;
 
205
 
 
206
    put_be16(pb, enc->bits_per_sample); /* Sample size */
 
207
 
 
208
    sample_rate = av_dbl2ext((double)enc->sample_rate);
 
209
    put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
 
210
 
 
211
    if (aifc) {
 
212
        put_le32(pb, enc->codec_tag);
 
213
        put_be16(pb, 0);
 
214
    }
 
215
 
 
216
    /* Sound data chunk */
 
217
    put_tag(pb, "SSND");
 
218
    aiff->ssnd = url_ftell(pb);         /* Sound chunk size */
 
219
    put_be32(pb, 0);                    /* Sound samples data size */
 
220
    put_be32(pb, 0);                    /* Data offset */
 
221
    put_be32(pb, 0);                    /* Block-size (block align) */
 
222
 
 
223
    av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
 
224
 
 
225
    /* Data is starting here */
 
226
    put_flush_packet(pb);
 
227
 
 
228
    return 0;
 
229
}
 
230
 
 
231
static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
 
232
{
 
233
    ByteIOContext *pb = &s->pb;
 
234
    put_buffer(pb, pkt->data, pkt->size);
 
235
    return 0;
 
236
}
 
237
 
 
238
static int aiff_write_trailer(AVFormatContext *s)
 
239
{
 
240
    ByteIOContext *pb = &s->pb;
 
241
    AIFFOutputContext *aiff = s->priv_data;
 
242
    AVCodecContext *enc = s->streams[0]->codec;
 
243
 
 
244
    /* Chunks sizes must be even */
 
245
    offset_t file_size, end_size;
 
246
    end_size = file_size = url_ftell(pb);
 
247
    if (file_size & 1) {
 
248
        put_byte(pb, 0);
 
249
        end_size++;
 
250
    }
 
251
 
 
252
    if (!url_is_streamed(&s->pb)) {
 
253
        /* File length */
 
254
        url_fseek(pb, aiff->form, SEEK_SET);
 
255
        put_be32(pb, (uint32_t)(file_size - aiff->form - 4));
 
256
 
 
257
        /* Number of sample frames */
 
258
        url_fseek(pb, aiff->frames, SEEK_SET);
 
259
        put_be32(pb, ((uint32_t)(file_size-aiff->ssnd-12))/enc->block_align);
 
260
 
 
261
        /* Sound Data chunk size */
 
262
        url_fseek(pb, aiff->ssnd, SEEK_SET);
 
263
        put_be32(pb, (uint32_t)(file_size - aiff->ssnd - 4));
 
264
 
 
265
        /* return to the end */
 
266
        url_fseek(pb, end_size, SEEK_SET);
 
267
 
 
268
        put_flush_packet(pb);
 
269
    }
 
270
 
 
271
    return 0;
 
272
}
 
273
#endif //CONFIG_MUXERS
 
274
 
 
275
static int aiff_probe(AVProbeData *p)
 
276
{
 
277
    /* check file header */
 
278
    if (p->buf_size < 16)
 
279
        return 0;
 
280
    if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
 
281
        p->buf[2] == 'R' && p->buf[3] == 'M' &&
 
282
        p->buf[8] == 'A' && p->buf[9] == 'I' &&
 
283
        p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
 
284
        return AVPROBE_SCORE_MAX;
 
285
    else
 
286
        return 0;
 
287
}
 
288
 
 
289
/* aiff input */
 
290
static int aiff_read_header(AVFormatContext *s,
 
291
                            AVFormatParameters *ap)
 
292
{
 
293
    int size, filesize, offset;
 
294
    uint32_t tag;
 
295
    unsigned version = AIFF_C_VERSION1;
 
296
    ByteIOContext *pb = &s->pb;
 
297
    AVStream * st = s->streams[0];
 
298
 
 
299
    /* check FORM header */
 
300
    filesize = get_tag(pb, &tag);
 
301
    if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
 
302
        return AVERROR_INVALIDDATA;
 
303
 
 
304
    /* AIFF data type */
 
305
    tag = get_le32(pb);
 
306
    if (tag == MKTAG('A', 'I', 'F', 'F'))       /* Got an AIFF file */
 
307
        version = AIFF;
 
308
    else if (tag != MKTAG('A', 'I', 'F', 'C'))  /* An AIFF-C file then */
 
309
        return AVERROR_INVALIDDATA;
 
310
 
 
311
    filesize -= 4;
 
312
 
 
313
    st = av_new_stream(s, 0);
 
314
    if (!st)
 
315
        return AVERROR_NOMEM;
 
316
 
 
317
    while (filesize > 0) {
 
318
        /* parse different chunks */
 
319
        size = get_tag(pb, &tag);
 
320
        if (size < 0)
 
321
            return size;
 
322
 
 
323
        filesize -= size + 8;
 
324
 
 
325
        switch (tag) {
 
326
            case MKTAG('C', 'O', 'M', 'M'):     /* Common chunk */
 
327
                /* Then for the complete header info */
 
328
                st->nb_frames = get_aiff_header (pb, st->codec, size, version);
 
329
                if (st->nb_frames < 0)
 
330
                        return st->nb_frames;
 
331
                break;
 
332
 
 
333
            case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
 
334
                version = get_be32(pb);
 
335
                break;
 
336
 
 
337
            case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
 
338
                get_meta (pb, s->title, sizeof(s->title), size);
 
339
                break;
 
340
 
 
341
            case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
 
342
                get_meta (pb, s->author, sizeof(s->author), size);
 
343
                break;
 
344
 
 
345
            case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
 
346
                get_meta (pb, s->copyright, sizeof(s->copyright), size);
 
347
                break;
 
348
 
 
349
            case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
 
350
                get_meta (pb, s->comment, sizeof(s->comment), size);
 
351
                break;
 
352
 
 
353
            case MKTAG('S', 'S', 'N', 'D'):     /* Sampled sound chunk */
 
354
                get_be32(pb);               /* Block align... don't care */
 
355
                offset = get_be32(pb);      /* Offset of sound data */
 
356
                goto got_sound;
 
357
 
 
358
            default: /* Jump */
 
359
                if (size & 1)   /* Always even aligned */
 
360
                    size++;
 
361
                url_fskip (pb, size);
 
362
        }
 
363
    }
 
364
 
 
365
    /* End of loop and didn't get sound */
 
366
    return AVERROR_INVALIDDATA;
 
367
 
 
368
got_sound:
 
369
    /* Now positioned, get the sound data start and end */
 
370
    if (st->nb_frames)
 
371
        s->file_size = st->nb_frames * st->codec->block_align;
 
372
 
 
373
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
 
374
    st->start_time = 0;
 
375
    st->duration = st->nb_frames;
 
376
 
 
377
    /* Position the stream at the first block */
 
378
    url_fskip(pb, offset);
 
379
 
 
380
    return 0;
 
381
}
 
382
 
 
383
#define MAX_SIZE 4096
 
384
 
 
385
static int aiff_read_packet(AVFormatContext *s,
 
386
                            AVPacket *pkt)
 
387
{
 
388
    AVStream *st = s->streams[0];
 
389
    int res;
 
390
 
 
391
    /* End of stream may be reached */
 
392
    if (url_feof(&s->pb))
 
393
        return AVERROR_IO;
 
394
 
 
395
    /* Now for that packet */
 
396
    res = av_get_packet(&s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
 
397
    if (res < 0)
 
398
        return res;
 
399
 
 
400
    /* Only one stream in an AIFF file */
 
401
    pkt->stream_index = 0;
 
402
    return 0;
 
403
}
 
404
 
 
405
static int aiff_read_close(AVFormatContext *s)
 
406
{
 
407
    return 0;
 
408
}
 
409
 
 
410
static int aiff_read_seek(AVFormatContext *s,
 
411
                          int stream_index, int64_t timestamp, int flags)
 
412
{
 
413
    return pcm_read_seek(s, stream_index, timestamp, flags);
 
414
}
 
415
 
 
416
#ifdef CONFIG_AIFF_DEMUXER
 
417
AVInputFormat aiff_demuxer = {
 
418
    "aiff",
 
419
    "Audio IFF",
 
420
    0,
 
421
    aiff_probe,
 
422
    aiff_read_header,
 
423
    aiff_read_packet,
 
424
    aiff_read_close,
 
425
    aiff_read_seek,
 
426
    .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
 
427
};
 
428
#endif
 
429
 
 
430
#ifdef CONFIG_AIFF_MUXER
 
431
AVOutputFormat aiff_muxer = {
 
432
    "aiff",
 
433
    "Audio IFF",
 
434
    "audio/aiff",
 
435
    "aif,aiff,afc,aifc",
 
436
    sizeof(AIFFOutputContext),
 
437
    CODEC_ID_PCM_S16BE,
 
438
    CODEC_ID_NONE,
 
439
    aiff_write_header,
 
440
    aiff_write_packet,
 
441
    aiff_write_trailer,
 
442
    .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
 
443
};
 
444
#endif