]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/aiff.c
6f1d9cd191efb55f629cece59245652a6e5fc35f
[frescor/ffmpeg.git] / 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 #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     { CODEC_ID_PCM_S16LE, MKTAG('s','o','w','t') },
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
108     if (size & 1)
109         size++;
110
111     codec->codec_type = CODEC_TYPE_AUDIO;
112     codec->channels = get_be16(pb);
113     num_frames = get_be32(pb);
114     codec->bits_per_sample = get_be16(pb);
115
116     get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
117     sample_rate = av_ext2dbl(ext);          /* 80 bits BE IEEE extended float */
118     codec->sample_rate = sample_rate;
119     size -= 18;
120
121     /* Got an AIFF-C? */
122     if (version == AIFF_C_VERSION1) {
123         codec->codec_tag = get_le32(pb);
124         codec->codec_id  = codec_get_id (codec_aiff_tags, codec->codec_tag);
125
126         if (codec->codec_id == 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         }
130
131         size -= 4;
132     } else {
133         /* Need the codec type */
134         codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
135         codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
136     }
137
138     if (!codec->codec_id)
139         return AVERROR_INVALIDDATA;
140
141     /* Block align needs to be computed in all cases, as the definition
142      * is specific to applications -> here we use the WAVE format definition */
143     codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
144
145     codec->bit_rate = codec->sample_rate * (codec->block_align << 3);
146
147     /* Chunk is over */
148     if (size)
149         url_fseek(pb, size, SEEK_CUR);
150
151     return num_frames;
152 }
153
154 #ifdef CONFIG_MUXERS
155 typedef struct {
156     offset_t form;
157     offset_t frames;
158     offset_t ssnd;
159 } AIFFOutputContext;
160
161 static int aiff_write_header(AVFormatContext *s)
162 {
163     AIFFOutputContext *aiff = s->priv_data;
164     ByteIOContext *pb = &s->pb;
165     AVCodecContext *enc = s->streams[0]->codec;
166     AVExtFloat sample_rate;
167     int aifc = 0;
168
169     /* First verify if format is ok */
170     if (!enc->codec_tag) {
171         return -1;
172     }
173
174     if (enc->codec_tag != MKTAG('N','O','N','E'))
175         aifc = 1;
176
177     /* FORM AIFF header */
178     put_tag(pb, "FORM");
179     aiff->form = url_ftell(pb);
180     put_be32(pb, 0);                    /* file length */
181     put_tag(pb, aifc ? "AIFC" : "AIFF");
182
183     if (aifc) {
184         /* Version chunk */
185         put_tag(pb, "FVER");
186         put_be32(pb, 4);
187         put_be32(pb, 0xA2805140);
188     }
189
190     /* Common chunk */
191     put_tag(pb, "COMM");
192     put_be32(pb, aifc ? 24 : 18); /* size */
193     put_be16(pb, enc->channels);        /* Number of channels */
194
195     aiff->frames = url_ftell(pb);
196     put_be32(pb, 0);                    /* Number of frames */
197
198     if (!enc->bits_per_sample)
199         enc->bits_per_sample = av_get_bits_per_sample(enc->codec_id);
200     if (!enc->bits_per_sample) {
201         av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
202         return -1;
203     }
204     if (!enc->block_align)
205         enc->block_align = (enc->bits_per_sample * enc->channels) >> 3;
206
207     put_be16(pb, enc->bits_per_sample); /* Sample size */
208
209     sample_rate = av_dbl2ext((double)enc->sample_rate);
210     put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
211
212     if (aifc) {
213         put_le32(pb, enc->codec_tag);
214         put_be16(pb, 0);
215     }
216
217     /* Sound data chunk */
218     put_tag(pb, "SSND");
219     aiff->ssnd = url_ftell(pb);         /* Sound chunk size */
220     put_be32(pb, 0);                    /* Sound samples data size */
221     put_be32(pb, 0);                    /* Data offset */
222     put_be32(pb, 0);                    /* Block-size (block align) */
223
224     av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
225
226     /* Data is starting here */
227     put_flush_packet(pb);
228
229     return 0;
230 }
231
232 static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
233 {
234     ByteIOContext *pb = &s->pb;
235     put_buffer(pb, pkt->data, pkt->size);
236     return 0;
237 }
238
239 static int aiff_write_trailer(AVFormatContext *s)
240 {
241     ByteIOContext *pb = &s->pb;
242     AIFFOutputContext *aiff = s->priv_data;
243     AVCodecContext *enc = s->streams[0]->codec;
244
245     /* Chunks sizes must be even */
246     offset_t file_size, end_size;
247     end_size = file_size = url_ftell(pb);
248     if (file_size & 1) {
249         put_byte(pb, 0);
250         end_size++;
251     }
252
253     if (!url_is_streamed(&s->pb)) {
254         /* File length */
255         url_fseek(pb, aiff->form, SEEK_SET);
256         put_be32(pb, (uint32_t)(file_size - aiff->form - 4));
257
258         /* Number of sample frames */
259         url_fseek(pb, aiff->frames, SEEK_SET);
260         put_be32(pb, ((uint32_t)(file_size-aiff->ssnd-12))/enc->block_align);
261
262         /* Sound Data chunk size */
263         url_fseek(pb, aiff->ssnd, SEEK_SET);
264         put_be32(pb, (uint32_t)(file_size - aiff->ssnd - 4));
265
266         /* return to the end */
267         url_fseek(pb, end_size, SEEK_SET);
268
269         put_flush_packet(pb);
270     }
271
272     return 0;
273 }
274 #endif //CONFIG_MUXERS
275
276 static int aiff_probe(AVProbeData *p)
277 {
278     /* check file header */
279     if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
280         p->buf[2] == 'R' && p->buf[3] == 'M' &&
281         p->buf[8] == 'A' && p->buf[9] == 'I' &&
282         p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
283         return AVPROBE_SCORE_MAX;
284     else
285         return 0;
286 }
287
288 /* aiff input */
289 static int aiff_read_header(AVFormatContext *s,
290                             AVFormatParameters *ap)
291 {
292     int size, filesize;
293     offset_t offset = 0;
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(ENOMEM);
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                 if (offset > 0) // COMM is after SSND
332                     goto got_sound;
333                 break;
334
335             case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
336                 version = get_be32(pb);
337                 break;
338
339             case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
340                 get_meta (pb, s->title, sizeof(s->title), size);
341                 break;
342
343             case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
344                 get_meta (pb, s->author, sizeof(s->author), size);
345                 break;
346
347             case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
348                 get_meta (pb, s->copyright, sizeof(s->copyright), size);
349                 break;
350
351             case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
352                 get_meta (pb, s->comment, sizeof(s->comment), size);
353                 break;
354
355             case MKTAG('S', 'S', 'N', 'D'):     /* Sampled sound chunk */
356                 offset = get_be32(pb);      /* Offset of sound data */
357                 get_be32(pb);               /* BlockSize... don't care */
358                 offset += url_ftell(pb);    /* Compute absolute data offset */
359                 if (st->codec->codec_id)    /* Assume COMM already parsed */
360                     goto got_sound;
361                 if (url_is_streamed(pb)) {
362                     av_log(s, AV_LOG_ERROR, "file is not seekable\n");
363                     return -1;
364                 }
365                 url_fskip(pb, size - 8);
366                 break;
367
368             default: /* Jump */
369                 if (size & 1)   /* Always even aligned */
370                     size++;
371                 url_fskip (pb, size);
372         }
373     }
374
375     /* End of loop and didn't get sound */
376     return AVERROR_INVALIDDATA;
377
378 got_sound:
379     /* Now positioned, get the sound data start and end */
380     if (st->nb_frames)
381         s->file_size = st->nb_frames * st->codec->block_align;
382
383     av_set_pts_info(st, 64, 1, st->codec->sample_rate);
384     st->start_time = 0;
385     st->duration = st->nb_frames;
386
387     /* Position the stream at the first block */
388     url_fseek(pb, offset, SEEK_SET);
389
390     return 0;
391 }
392
393 #define MAX_SIZE 4096
394
395 static int aiff_read_packet(AVFormatContext *s,
396                             AVPacket *pkt)
397 {
398     AVStream *st = s->streams[0];
399     int res;
400
401     /* End of stream may be reached */
402     if (url_feof(&s->pb))
403         return AVERROR(EIO);
404
405     /* Now for that packet */
406     res = av_get_packet(&s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
407     if (res < 0)
408         return res;
409
410     /* Only one stream in an AIFF file */
411     pkt->stream_index = 0;
412     return 0;
413 }
414
415 static int aiff_read_close(AVFormatContext *s)
416 {
417     return 0;
418 }
419
420 static int aiff_read_seek(AVFormatContext *s,
421                           int stream_index, int64_t timestamp, int flags)
422 {
423     return pcm_read_seek(s, stream_index, timestamp, flags);
424 }
425
426 #ifdef CONFIG_AIFF_DEMUXER
427 AVInputFormat aiff_demuxer = {
428     "aiff",
429     "Audio IFF",
430     0,
431     aiff_probe,
432     aiff_read_header,
433     aiff_read_packet,
434     aiff_read_close,
435     aiff_read_seek,
436     .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
437 };
438 #endif
439
440 #ifdef CONFIG_AIFF_MUXER
441 AVOutputFormat aiff_muxer = {
442     "aiff",
443     "Audio IFF",
444     "audio/aiff",
445     "aif,aiff,afc,aifc",
446     sizeof(AIFFOutputContext),
447     CODEC_ID_PCM_S16BE,
448     CODEC_ID_NONE,
449     aiff_write_header,
450     aiff_write_packet,
451     aiff_write_trailer,
452     .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
453 };
454 #endif