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
20 #include "allformats.h"
22 #include "intfloat_readwrite.h"
24 const CodecTag codec_aiff_tags[] = {
25 { CODEC_ID_PCM_S16BE, MKTAG('N','O','N','E') },
26 { CODEC_ID_PCM_S8, MKTAG('N','O','N','E') },
27 { CODEC_ID_PCM_S24BE, MKTAG('N','O','N','E') },
28 { CODEC_ID_PCM_S32BE, MKTAG('N','O','N','E') },
29 { CODEC_ID_PCM_ALAW, MKTAG('a','l','a','w') },
30 { CODEC_ID_PCM_ALAW, MKTAG('A','L','A','W') },
31 { CODEC_ID_PCM_MULAW, MKTAG('u','l','a','w') },
32 { CODEC_ID_PCM_MULAW, MKTAG('U','L','A','W') },
33 { CODEC_ID_MACE3, MKTAG('M','A','C','3') },
34 { CODEC_ID_MACE6, MKTAG('M','A','C','6') },
35 { CODEC_ID_GSM, MKTAG('G','S','M',' ') },
36 { CODEC_ID_ADPCM_G726, MKTAG('G','7','2','6') },
41 #define AIFF_C_VERSION1 0xA2805140
43 static int aiff_codec_get_id (int bps)
46 return CODEC_ID_PCM_S8;
48 return CODEC_ID_PCM_S16BE;
50 return CODEC_ID_PCM_S24BE;
52 return CODEC_ID_PCM_S32BE;
54 /* bigger than 32 isn't allowed */
58 /* returns the size of the found tag */
59 static int get_tag(ByteIOContext *pb, uint32_t * tag)
75 /* Metadata string read */
76 static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
81 res = get_buffer(pb, (uint8_t*)str, strsize-1);
83 res = get_buffer(pb, (uint8_t*)str, size);
96 /* Returns the number of bits per second */
97 static int fix_bps(int codec_id)
100 case CODEC_ID_PCM_S8:
102 case CODEC_ID_PCM_S16BE:
104 case CODEC_ID_PCM_S24BE:
106 case CODEC_ID_PCM_S32BE:
113 /* Returns the number of sound data frames or negative on error */
114 static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
115 int size, unsigned version)
119 unsigned int num_frames;
125 codec->codec_type = CODEC_TYPE_AUDIO;
126 codec->channels = get_be16(pb);
127 num_frames = get_be32(pb);
128 codec->bits_per_sample = get_be16(pb);
130 get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
131 sample_rate = av_ext2dbl(ext); /* 80 bits BE IEEE extended float */
132 codec->sample_rate = sample_rate;
136 if (version == AIFF_C_VERSION1) {
137 codec->codec_tag = get_le32(pb);
138 codec->codec_id = codec_get_id (codec_aiff_tags, codec->codec_tag);
140 if (codec->codec_id == CODEC_ID_PCM_S16BE) {
141 codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
142 codec->bits_per_sample = fix_bps(codec->codec_id);
147 /* Need the codec type */
148 codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
149 codec->bits_per_sample = fix_bps(codec->codec_id);
152 if (!codec->codec_id)
153 return AVERROR_INVALIDDATA;
155 /* Block align needs to be computed in all cases, as the definition
156 * is specific to applications -> here we use the WAVE format definition */
157 codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
159 codec->bit_rate = codec->sample_rate * codec->block_align;
163 url_fseek(pb, size, SEEK_CUR);
175 static int aiff_write_header(AVFormatContext *s)
177 AIFFOutputContext *aiff = s->priv_data;
178 ByteIOContext *pb = &s->pb;
179 AVCodecContext *enc = s->streams[0]->codec;
180 AVExtFloat sample_rate;
183 /* First verify if format is ok */
184 enc->codec_tag = codec_get_tag(codec_aiff_tags, enc->codec_id);
185 if (!enc->codec_tag) {
190 coder_len = strlen(enc->codec->name);
192 /* FORM AIFF header */
194 aiff->form = url_ftell(pb);
195 put_be32(pb, 0); /* file length */
201 put_be32(pb, 0xA2805140);
205 if (coder_len & 1) /* Common chunk is of var size */
206 put_be32(pb, 23+coder_len);
208 put_be32(pb, 24+coder_len);
209 put_be16(pb, enc->channels); /* Number of channels */
211 aiff->frames = url_ftell(pb);
212 put_be32(pb, 0); /* Number of frames */
214 if (!enc->bits_per_sample)
215 enc->bits_per_sample = (enc->block_align<<3) / enc->channels;
216 put_be16(pb, enc->bits_per_sample); /* Sample size */
218 sample_rate = av_dbl2ext((double)enc->sample_rate);
219 put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
221 put_le32(pb, enc->codec_tag);
223 put_byte(pb, coder_len);
224 put_buffer(pb, (const uint8_t*)enc->codec->name, coder_len);
226 put_byte(pb, coder_len+1);
227 put_buffer(pb, (const uint8_t*)enc->codec->name, coder_len);
231 /* Sound data chunk */
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) */
238 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
240 /* Data is starting here */
241 put_flush_packet(pb);
246 static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
248 ByteIOContext *pb = &s->pb;
249 put_buffer(pb, pkt->data, pkt->size);
253 static int aiff_write_trailer(AVFormatContext *s)
255 ByteIOContext *pb = &s->pb;
256 AIFFOutputContext *aiff = s->priv_data;
257 AVCodecContext *enc = s->streams[0]->codec;
259 /* Chunks sizes must be even */
260 offset_t file_size, end_size;
261 end_size = file_size = url_ftell(pb);
267 if (!url_is_streamed(&s->pb)) {
269 url_fseek(pb, aiff->form, SEEK_SET);
270 put_be32(pb, (uint32_t)(file_size - aiff->form - 4));
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);
276 /* Sound Data chunk size */
277 url_fseek(pb, aiff->ssnd, SEEK_SET);
278 put_be32(pb, (uint32_t)(file_size - aiff->ssnd - 4));
280 /* return to the end */
281 url_fseek(pb, end_size, SEEK_SET);
283 put_flush_packet(pb);
288 #endif //CONFIG_MUXERS
290 static int aiff_probe(AVProbeData *p)
292 /* check file header */
293 if (p->buf_size < 16)
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;
305 static int aiff_read_header(AVFormatContext *s,
306 AVFormatParameters *ap)
308 int size, filesize, offset;
310 unsigned version = AIFF_C_VERSION1;
311 ByteIOContext *pb = &s->pb;
312 AVStream * st = s->streams[0];
314 /* check FORM header */
315 filesize = get_tag(pb, &tag);
316 if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
317 return AVERROR_INVALIDDATA;
321 if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
323 else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
324 return AVERROR_INVALIDDATA;
328 st = av_new_stream(s, 0);
330 return AVERROR_NOMEM;
332 while (filesize > 0) {
333 /* parse different chunks */
334 size = get_tag(pb, &tag);
338 filesize -= size + 8;
341 case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
342 /* Then for the complete header info */
343 st->nb_frames = get_aiff_header (pb, st->codec, size, version);
344 if (st->nb_frames < 0)
345 return st->nb_frames;
348 case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
349 version = get_be32(pb);
352 case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
353 get_meta (pb, s->title, sizeof(s->title), size);
356 case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
357 get_meta (pb, s->author, sizeof(s->author), size);
360 case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
361 get_meta (pb, s->copyright, sizeof(s->copyright), size);
364 case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
365 get_meta (pb, s->comment, sizeof(s->comment), size);
368 case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
369 get_be32(pb); /* Block align... don't care */
370 offset = get_be32(pb); /* Offset of sound data */
374 if (size & 1) /* Always even aligned */
376 url_fskip (pb, size);
380 /* End of loop and didn't get sound */
381 return AVERROR_INVALIDDATA;
384 /* Now positioned, get the sound data start and end */
386 s->file_size = st->nb_frames * st->codec->block_align;
388 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
390 st->duration = st->nb_frames;
392 /* Position the stream at the first block */
393 url_fskip(pb, offset);
398 #define MAX_SIZE 4096
400 static int aiff_read_packet(AVFormatContext *s,
403 AVStream *st = s->streams[0];
406 /* End of stream may be reached */
407 if (url_feof(&s->pb))
410 /* Now for that packet */
411 res = av_get_packet(&s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
415 /* Only one stream in an AIFF file */
416 pkt->stream_index = 0;
420 static int aiff_read_close(AVFormatContext *s)
425 static int aiff_read_seek(AVFormatContext *s,
426 int stream_index, int64_t timestamp, int flags)
428 return pcm_read_seek(s, stream_index, timestamp, flags);
431 #ifdef CONFIG_AIFF_DEMUXER
432 AVInputFormat aiff_demuxer = {
444 #ifdef CONFIG_AIFF_MUXER
445 AVOutputFormat aiff_muxer = {
450 sizeof(AIFFOutputContext),