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