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