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