2 * AIFF/AIFF-C encoder and decoder
3 * Copyright (c) 2006 Patrick Guimond
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.
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.
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
21 #include "intfloat_readwrite.h"
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') },
40 #define AIFF_C_VERSION1 0xA2805140
42 static int aiff_codec_get_id (int bps)
45 return CODEC_ID_PCM_S8;
47 return CODEC_ID_PCM_S16BE;
49 return CODEC_ID_PCM_S24BE;
51 return CODEC_ID_PCM_S32BE;
53 /* bigger than 32 isn't allowed */
57 /* returns the size of the found tag */
58 static int get_tag(ByteIOContext *pb, uint32_t * tag)
74 /* Metadata string read */
75 static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
80 res = get_buffer(pb, (uint8_t*)str, strsize-1);
82 res = get_buffer(pb, (uint8_t*)str, size);
95 /* Returns the number of bits per second */
96 static int fix_bps(int codec_id)
101 case CODEC_ID_PCM_S16BE:
103 case CODEC_ID_PCM_S24BE:
105 case CODEC_ID_PCM_S32BE:
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)
118 unsigned int num_frames;
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);
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;
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);
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);
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);
151 if (!codec->codec_id)
152 return AVERROR_INVALIDDATA;
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;
158 codec->bit_rate = codec->sample_rate * codec->block_align;
162 url_fseek(pb, size, SEEK_CUR);
174 static int aiff_write_header(AVFormatContext *s)
176 AIFFOutputContext *aiff = s->priv_data;
177 ByteIOContext *pb = &s->pb;
178 AVCodecContext *enc = s->streams[0]->codec;
179 AVExtFloat sample_rate;
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) {
189 coder_len = strlen(enc->codec->name);
191 /* FORM AIFF header */
193 aiff->form = url_ftell(pb);
194 put_be32(pb, 0); /* file length */
200 put_be32(pb, 0xA2805140);
204 if (coder_len & 1) /* Common chunk is of var size */
205 put_be32(pb, 23+coder_len);
207 put_be32(pb, 24+coder_len);
208 put_be16(pb, enc->channels); /* Number of channels */
210 aiff->frames = url_ftell(pb);
211 put_be32(pb, 0); /* Number of frames */
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 */
217 sample_rate = av_dbl2ext((double)enc->sample_rate);
218 put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
220 put_le32(pb, enc->codec_tag);
222 put_byte(pb, coder_len);
223 put_buffer(pb, (uint8_t*)enc->codec->name, coder_len);
225 put_byte(pb, coder_len+1);
226 put_buffer(pb, (uint8_t*)enc->codec->name, coder_len);
230 /* Sound data chunk */
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) */
237 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
239 /* Data is starting here */
240 put_flush_packet(pb);
245 static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
247 ByteIOContext *pb = &s->pb;
248 put_buffer(pb, pkt->data, pkt->size);
252 static int aiff_write_trailer(AVFormatContext *s)
254 ByteIOContext *pb = &s->pb;
255 AIFFOutputContext *aiff = s->priv_data;
256 AVCodecContext *enc = s->streams[0]->codec;
258 /* Chunks sizes must be even */
259 offset_t file_size, end_size;
260 end_size = file_size = url_ftell(pb);
266 if (!url_is_streamed(&s->pb)) {
268 url_fseek(pb, aiff->form, SEEK_SET);
269 put_be32(pb, (uint32_t)(file_size - aiff->form - 4));
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);
275 /* Sound Data chunk size */
276 url_fseek(pb, aiff->ssnd, SEEK_SET);
277 put_be32(pb, (uint32_t)(file_size - aiff->ssnd - 4));
279 /* return to the end */
280 url_fseek(pb, end_size, SEEK_SET);
282 put_flush_packet(pb);
287 #endif //CONFIG_MUXERS
289 static int aiff_probe(AVProbeData *p)
291 /* check file header */
292 if (p->buf_size < 16)
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;
304 static int aiff_read_header(AVFormatContext *s,
305 AVFormatParameters *ap)
307 int size, filesize, offset;
309 unsigned version = AIFF_C_VERSION1;
310 ByteIOContext *pb = &s->pb;
311 AVStream * st = s->streams[0];
313 /* check FORM header */
314 filesize = get_tag(pb, &tag);
315 if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
316 return AVERROR_INVALIDDATA;
320 if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
322 else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
323 return AVERROR_INVALIDDATA;
327 st = av_new_stream(s, 0);
329 return AVERROR_NOMEM;
331 while (filesize > 0) {
332 /* parse different chunks */
333 size = get_tag(pb, &tag);
337 filesize -= size + 8;
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;
347 case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
348 version = get_be32(pb);
351 case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
352 get_meta (pb, s->title, sizeof(s->title), size);
355 case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
356 get_meta (pb, s->author, sizeof(s->author), size);
359 case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
360 get_meta (pb, s->copyright, sizeof(s->copyright), size);
363 case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
364 get_meta (pb, s->comment, sizeof(s->comment), size);
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 */
373 if (size & 1) /* Always even aligned */
375 url_fskip (pb, size);
379 /* End of loop and didn't get sound */
380 return AVERROR_INVALIDDATA;
383 /* Now positioned, get the sound data start and end */
385 s->file_size = st->nb_frames * st->codec->block_align;
387 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
389 st->duration = st->nb_frames;
391 /* Position the stream at the first block */
392 url_fskip(pb, offset);
397 #define MAX_SIZE 4096
399 static int aiff_read_packet(AVFormatContext *s,
402 AVStream *st = s->streams[0];
405 /* End of stream may be reached */
406 if (url_feof(&s->pb))
409 /* Now for that packet */
410 res = av_get_packet(&s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
414 /* Only one stream in an AIFF file */
415 pkt->stream_index = 0;
419 static int aiff_read_close(AVFormatContext *s)
424 static int aiff_read_seek(AVFormatContext *s,
425 int stream_index, int64_t timestamp, int flags)
427 return pcm_read_seek(s, stream_index, timestamp, flags);
431 static AVInputFormat aiff_iformat = {
443 static AVOutputFormat aiff_oformat = {
448 sizeof(AIFFOutputContext),
455 #endif //CONFIG_MUXERS
457 int ff_aiff_init(void)
459 av_register_input_format(&aiff_iformat);
461 av_register_output_format(&aiff_oformat);
462 #endif //CONFIG_MUXERS