]> git.sesse.net Git - ffmpeg/blob - libavformat/aiff.c
fix pcm_s24be demuxing, simplify
[ffmpeg] / libavformat / aiff.c
1 /*
2  * AIFF/AIFF-C encoder and decoder
3  * Copyright (c) 2006  Patrick Guimond
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 #include "avformat.h"
20 #include "avi.h"
21 #include "intfloat_readwrite.h"
22
23 const CodecTag codec_aiff_tags[] = {
24     { CODEC_ID_PCM_S16BE, MKTAG('N','O','N','E') },
25     { CODEC_ID_PCM_S8, MKTAG('N','O','N','E') },
26     { CODEC_ID_PCM_S24BE, MKTAG('N','O','N','E') },
27     { CODEC_ID_PCM_S32BE, MKTAG('N','O','N','E') },
28     { CODEC_ID_PCM_ALAW, MKTAG('a','l','a','w') },
29     { CODEC_ID_PCM_ALAW, MKTAG('A','L','A','W') },
30     { CODEC_ID_PCM_MULAW, MKTAG('u','l','a','w') },
31     { CODEC_ID_PCM_MULAW, MKTAG('U','L','A','W') },
32     { CODEC_ID_MACE3, MKTAG('M','A','C','3') },
33     { CODEC_ID_MACE6, MKTAG('M','A','C','6') },
34     { CODEC_ID_GSM, MKTAG('G','S','M',' ') },
35     { CODEC_ID_ADPCM_G726, MKTAG('G','7','2','6') },
36     { 0, 0 },
37 };
38
39 #define AIFF                    0
40 #define AIFF_C_VERSION1         0xA2805140
41
42 static int aiff_codec_get_id (int bps)
43 {
44     if (bps <= 8)
45         return CODEC_ID_PCM_S8;
46     if (bps <= 16)
47         return CODEC_ID_PCM_S16BE;
48     if (bps <= 24)
49         return CODEC_ID_PCM_S24BE;
50     if (bps <= 32)
51         return CODEC_ID_PCM_S32BE;
52
53     /* bigger than 32 isn't allowed  */
54     return 0;
55 }
56
57 /* returns the size of the found tag */
58 static int get_tag(ByteIOContext *pb, uint32_t * tag)
59 {
60     int size;
61
62     if (url_feof(pb))
63         return AVERROR_IO;
64
65     *tag = get_le32(pb);
66     size = get_be32(pb);
67
68     if (size < 0)
69         size = 0x7fffffff;
70
71     return size;
72 }
73
74 /* Metadata string read */
75 static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
76 {
77     int res;
78
79     if (size > strsize-1)
80         res = get_buffer(pb, (uint8_t*)str, strsize-1);
81     else
82         res = get_buffer(pb, (uint8_t*)str, size);
83
84     if (res < 0)
85         return;
86
87     str[res] = 0;
88     if (size & 1)
89         size++;
90     size -= res;
91     if (size);
92         url_fskip(pb, size);
93 }
94
95 /* Returns the number of bits per second */
96 static int fix_bps(int codec_id)
97 {
98     switch (codec_id) {
99         case CODEC_ID_PCM_S8:
100                 return 8;
101         case CODEC_ID_PCM_S16BE:
102                 return 16;
103         case CODEC_ID_PCM_S24BE:
104                 return 24;
105         case CODEC_ID_PCM_S32BE:
106                 return 32;
107     }
108
109     return -1;
110 }
111
112 /* Returns the number of sound data frames or negative on error */
113 unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
114                              int size, unsigned version)
115 {
116     AVExtFloat ext;
117     double sample_rate;
118     unsigned int num_frames;
119
120
121     if (size & 1)
122         size++;
123
124     codec->codec_type = CODEC_TYPE_AUDIO;
125     codec->channels = get_be16(pb);
126     num_frames = get_be32(pb);
127     codec->bits_per_sample = get_be16(pb);
128
129     get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
130     sample_rate = av_ext2dbl(ext);          /* 80 bits BE IEEE extended float */
131     codec->sample_rate = sample_rate;
132     size -= 18;
133
134     /* Got an AIFF-C? */
135     if (version == AIFF_C_VERSION1) {
136         codec->codec_tag = get_le32(pb);
137         codec->codec_id  = codec_get_id (codec_aiff_tags, codec->codec_tag);
138
139         if (codec->codec_id == CODEC_ID_PCM_S16BE) {
140             codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
141             codec->bits_per_sample = fix_bps(codec->codec_id);
142         }
143
144         size -= 4;
145     } else {
146         /* Need the codec type */
147         codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
148         codec->bits_per_sample = fix_bps(codec->codec_id);
149     }
150
151     if (!codec->codec_id)
152         return AVERROR_INVALIDDATA;
153
154     /* Block align needs to be computed in all cases, as the definition
155      * is specific to applications -> here we use the WAVE format definition */
156     codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
157
158     codec->bit_rate = codec->sample_rate * codec->block_align;
159
160     /* Chunk is over */
161     if (size)
162         url_fseek(pb, size, SEEK_CUR);
163
164     return num_frames;
165 }
166
167 #ifdef CONFIG_MUXERS
168 typedef struct {
169     offset_t form;
170     offset_t frames;
171     offset_t ssnd;
172 } AIFFOutputContext;
173
174 static int aiff_write_header(AVFormatContext *s)
175 {
176     AIFFOutputContext *aiff = s->priv_data;
177     ByteIOContext *pb = &s->pb;
178     AVCodecContext *enc = s->streams[0]->codec;
179     AVExtFloat sample_rate;
180     int coder_len;
181
182     /* First verify if format is ok */
183     enc->codec_tag = codec_get_tag(codec_aiff_tags, enc->codec_id);
184     if (!enc->codec_tag) {
185         av_free(aiff);
186         return -1;
187     }
188
189     coder_len = strlen(enc->codec->name);
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     if (coder_len & 1)                  /* Common chunk is of var size */
205         put_be32(pb, 23+coder_len);
206     else
207         put_be32(pb, 24+coder_len);
208     put_be16(pb, enc->channels);        /* Number of channels */
209
210     aiff->frames = url_ftell(pb);
211     put_be32(pb, 0);                    /* Number of frames */
212
213     if (!enc->bits_per_sample)
214         enc->bits_per_sample = (enc->block_align<<3) / enc->channels;
215     put_be16(pb, enc->bits_per_sample); /* Sample size */
216
217     sample_rate = av_dbl2ext((double)enc->sample_rate);
218     put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
219
220     put_le32(pb, enc->codec_tag);
221     if (coder_len & 1) {
222         put_byte(pb, coder_len);
223         put_buffer(pb, (uint8_t*)enc->codec->name, coder_len);
224     } else {
225         put_byte(pb, coder_len+1);
226         put_buffer(pb, (uint8_t*)enc->codec->name, coder_len);
227         put_byte(pb, 0);
228     }
229
230     /* Sound data chunk */
231     put_tag(pb, "SSND");
232     aiff->ssnd = url_ftell(pb);         /* Sound chunk size */
233     put_be32(pb, 0);                    /* Sound samples data size */
234     put_be32(pb, 0);                    /* Data offset */
235     put_be32(pb, 0);                    /* Block-size (block align) */
236
237     av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
238
239     /* Data is starting here */
240     put_flush_packet(pb);
241
242     return 0;
243 }
244
245 static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
246 {
247     ByteIOContext *pb = &s->pb;
248     put_buffer(pb, pkt->data, pkt->size);
249     return 0;
250 }
251
252 static int aiff_write_trailer(AVFormatContext *s)
253 {
254     ByteIOContext *pb = &s->pb;
255     AIFFOutputContext *aiff = s->priv_data;
256     AVCodecContext *enc = s->streams[0]->codec;
257
258     /* Chunks sizes must be even */
259     offset_t file_size, end_size;
260     end_size = file_size = url_ftell(pb);
261     if (file_size & 1) {
262         put_byte(pb, 0);
263         end_size++;
264     }
265
266     if (!url_is_streamed(&s->pb)) {
267         /* File length */
268         url_fseek(pb, aiff->form, SEEK_SET);
269         put_be32(pb, (uint32_t)(file_size - aiff->form - 4));
270
271         /* Number of sample frames */
272         url_fseek(pb, aiff->frames, SEEK_SET);
273         put_be32(pb, ((uint32_t)(file_size-aiff->ssnd-12))/enc->block_align);
274
275         /* Sound Data chunk size */
276         url_fseek(pb, aiff->ssnd, SEEK_SET);
277         put_be32(pb, (uint32_t)(file_size - aiff->ssnd - 4));
278
279         /* return to the end */
280         url_fseek(pb, end_size, SEEK_SET);
281
282         put_flush_packet(pb);
283     }
284
285     return 0;
286 }
287 #endif //CONFIG_MUXERS
288
289 static int aiff_probe(AVProbeData *p)
290 {
291     /* check file header */
292     if (p->buf_size < 16)
293         return 0;
294     if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
295         p->buf[2] == 'R' && p->buf[3] == 'M' &&
296         p->buf[8] == 'A' && p->buf[9] == 'I' &&
297         p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
298         return AVPROBE_SCORE_MAX;
299     else
300         return 0;
301 }
302
303 /* aiff input */
304 static int aiff_read_header(AVFormatContext *s,
305                             AVFormatParameters *ap)
306 {
307     int size, filesize, offset;
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_NOMEM;
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                 break;
346
347             case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
348                 version = get_be32(pb);
349                 break;
350
351             case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
352                 get_meta (pb, s->title, sizeof(s->title), size);
353                 break;
354
355             case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
356                 get_meta (pb, s->author, sizeof(s->author), size);
357                 break;
358
359             case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
360                 get_meta (pb, s->copyright, sizeof(s->copyright), size);
361                 break;
362
363             case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
364                 get_meta (pb, s->comment, sizeof(s->comment), size);
365                 break;
366
367             case MKTAG('S', 'S', 'N', 'D'):     /* Sampled sound chunk */
368                 get_be32(pb);               /* Block align... don't care */
369                 offset = get_be32(pb);      /* Offset of sound data */
370                 goto got_sound;
371
372             default: /* Jump */
373                 if (size & 1)   /* Always even aligned */
374                     size++;
375                 url_fskip (pb, size);
376         }
377     }
378
379     /* End of loop and didn't get sound */
380     return AVERROR_INVALIDDATA;
381
382 got_sound:
383     /* Now positioned, get the sound data start and end */
384     if (st->nb_frames)
385         s->file_size = st->nb_frames * st->codec->block_align;
386
387     av_set_pts_info(st, 64, 1, st->codec->sample_rate);
388     st->start_time = 0;
389     st->duration = st->nb_frames;
390
391     /* Position the stream at the first block */
392     url_fskip(pb, offset);
393
394     return 0;
395 }
396
397 #define MAX_SIZE 4096
398
399 static int aiff_read_packet(AVFormatContext *s,
400                             AVPacket *pkt)
401 {
402     AVStream *st = s->streams[0];
403     int res;
404
405     /* End of stream may be reached */
406     if (url_feof(&s->pb))
407         return AVERROR_IO;
408
409     /* Now for that packet */
410     res = av_get_packet(&s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
411     if (res < 0)
412         return res;
413
414     /* Only one stream in an AIFF file */
415     pkt->stream_index = 0;
416     return 0;
417 }
418
419 static int aiff_read_close(AVFormatContext *s)
420 {
421     return 0;
422 }
423
424 static int aiff_read_seek(AVFormatContext *s,
425                           int stream_index, int64_t timestamp, int flags)
426 {
427     return pcm_read_seek(s, stream_index, timestamp, flags);
428 }
429
430
431 static AVInputFormat aiff_iformat = {
432     "aiff",
433     "Audio IFF",
434     0,
435     aiff_probe,
436     aiff_read_header,
437     aiff_read_packet,
438     aiff_read_close,
439     aiff_read_seek,
440 };
441
442 #ifdef CONFIG_MUXERS
443 static AVOutputFormat aiff_oformat = {
444     "aiff",
445     "Audio IFF",
446     "audio/aiff",
447     "aif,aiff,afc,aifc",
448     sizeof(AIFFOutputContext),
449     CODEC_ID_PCM_S16BE,
450     CODEC_ID_NONE,
451     aiff_write_header,
452     aiff_write_packet,
453     aiff_write_trailer,
454 };
455 #endif //CONFIG_MUXERS
456
457 int ff_aiff_init(void)
458 {
459     av_register_input_format(&aiff_iformat);
460 #ifdef CONFIG_MUXERS
461     av_register_output_format(&aiff_oformat);
462 #endif //CONFIG_MUXERS
463     return 0;
464 }
465