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