2 * AIFF/AIFF-C muxer and demuxer
3 * Copyright (c) 2006 Patrick Guimond
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "intfloat_readwrite.h"
26 static const AVCodecTag 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_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') },
37 { CODEC_ID_PCM_S16LE, MKTAG('s','o','w','t') },
38 { CODEC_ID_ADPCM_IMA_QT, MKTAG('i','m','a','4') },
39 { CODEC_ID_QDM2, MKTAG('Q','D','M','2') },
44 #define AIFF_C_VERSION1 0xA2805140
46 static int aiff_codec_get_id (int bps)
49 return CODEC_ID_PCM_S8;
51 return CODEC_ID_PCM_S16BE;
53 return CODEC_ID_PCM_S24BE;
55 return CODEC_ID_PCM_S32BE;
57 /* bigger than 32 isn't allowed */
61 /* returns the size of the found tag */
62 static int get_tag(ByteIOContext *pb, uint32_t * tag)
78 /* Metadata string read */
79 static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
84 res = get_buffer(pb, (uint8_t*)str, strsize-1);
86 res = get_buffer(pb, (uint8_t*)str, size);
99 /* Returns the number of sound data frames or negative on error */
100 static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
101 int size, unsigned version)
105 unsigned int num_frames;
109 codec->codec_type = CODEC_TYPE_AUDIO;
110 codec->channels = get_be16(pb);
111 num_frames = get_be32(pb);
112 codec->bits_per_sample = get_be16(pb);
114 get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
115 sample_rate = av_ext2dbl(ext); /* 80 bits BE IEEE extended float */
116 codec->sample_rate = sample_rate;
120 if (version == AIFF_C_VERSION1) {
121 codec->codec_tag = get_le32(pb);
122 codec->codec_id = codec_get_id(codec_aiff_tags, codec->codec_tag);
124 switch (codec->codec_id) {
125 case CODEC_ID_PCM_S16BE:
126 codec->codec_id = aiff_codec_get_id(codec->bits_per_sample);
127 codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
129 case CODEC_ID_ADPCM_IMA_QT:
130 codec->block_align = 34*codec->channels;
131 codec->frame_size = 64;
135 codec->frame_size = 6;
142 /* Need the codec type */
143 codec->codec_id = aiff_codec_get_id(codec->bits_per_sample);
144 codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
147 if (!codec->codec_id)
148 return AVERROR_INVALIDDATA;
150 /* Block align needs to be computed in all cases, as the definition
151 * is specific to applications -> here we use the WAVE format definition */
152 if (!codec->block_align)
153 codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
155 codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
156 codec->sample_rate) * (codec->block_align << 3);
160 url_fseek(pb, size, SEEK_CUR);
172 static int aiff_write_header(AVFormatContext *s)
174 AIFFOutputContext *aiff = s->priv_data;
175 ByteIOContext *pb = s->pb;
176 AVCodecContext *enc = s->streams[0]->codec;
177 AVExtFloat sample_rate;
180 /* First verify if format is ok */
183 if (enc->codec_tag != MKTAG('N','O','N','E'))
186 /* FORM AIFF header */
188 aiff->form = url_ftell(pb);
189 put_be32(pb, 0); /* file length */
190 put_tag(pb, aifc ? "AIFC" : "AIFF");
192 if (aifc) { // compressed audio
193 enc->bits_per_sample = 16;
194 if (!enc->block_align) {
195 av_log(s, AV_LOG_ERROR, "block align not set\n");
201 put_be32(pb, 0xA2805140);
206 put_be32(pb, aifc ? 24 : 18); /* size */
207 put_be16(pb, enc->channels); /* Number of channels */
209 aiff->frames = url_ftell(pb);
210 put_be32(pb, 0); /* Number of frames */
212 if (!enc->bits_per_sample)
213 enc->bits_per_sample = av_get_bits_per_sample(enc->codec_id);
214 if (!enc->bits_per_sample) {
215 av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
218 if (!enc->block_align)
219 enc->block_align = (enc->bits_per_sample * enc->channels) >> 3;
221 put_be16(pb, enc->bits_per_sample); /* Sample size */
223 sample_rate = av_dbl2ext((double)enc->sample_rate);
224 put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
227 put_le32(pb, enc->codec_tag);
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, file_size - aiff->form - 4);
272 /* Number of sample frames */
273 url_fseek(pb, aiff->frames, SEEK_SET);
274 put_be32(pb, (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, 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[0] == 'F' && p->buf[1] == 'O' &&
294 p->buf[2] == 'R' && p->buf[3] == 'M' &&
295 p->buf[8] == 'A' && p->buf[9] == 'I' &&
296 p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
297 return AVPROBE_SCORE_MAX;
303 static int aiff_read_header(AVFormatContext *s,
304 AVFormatParameters *ap)
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(ENOMEM);
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;
345 if (offset > 0) // COMM is after SSND
348 case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
349 version = get_be32(pb);
351 case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
352 get_meta (pb, s->title, sizeof(s->title), size);
354 case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
355 get_meta (pb, s->author, sizeof(s->author), size);
357 case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
358 get_meta (pb, s->copyright, sizeof(s->copyright), size);
360 case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
361 get_meta (pb, s->comment, sizeof(s->comment), size);
363 case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
364 offset = get_be32(pb); /* Offset of sound data */
365 get_be32(pb); /* BlockSize... don't care */
366 offset += url_ftell(pb); /* Compute absolute data offset */
367 if (st->codec->codec_id) /* Assume COMM already parsed */
369 if (url_is_streamed(pb)) {
370 av_log(s, AV_LOG_ERROR, "file is not seekable\n");
373 url_fskip(pb, size - 8);
375 case MKTAG('w', 'a', 'v', 'e'):
376 if ((uint64_t)size > (1<<30))
378 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
379 if (!st->codec->extradata)
380 return AVERROR(ENOMEM);
381 st->codec->extradata_size = size;
382 get_buffer(pb, st->codec->extradata, size);
385 if (size & 1) /* Always even aligned */
387 url_fskip (pb, size);
391 /* End of loop and didn't get sound */
392 return AVERROR_INVALIDDATA;
395 /* Now positioned, get the sound data start and end */
397 s->file_size = st->nb_frames * st->codec->block_align;
399 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
401 st->duration = st->codec->frame_size ?
402 st->nb_frames * st->codec->frame_size : st->nb_frames;
404 /* Position the stream at the first block */
405 url_fseek(pb, offset, SEEK_SET);
410 #define MAX_SIZE 4096
412 static int aiff_read_packet(AVFormatContext *s,
415 AVStream *st = s->streams[0];
418 /* End of stream may be reached */
422 /* Now for that packet */
423 res = av_get_packet(s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
427 /* Only one stream in an AIFF file */
428 pkt->stream_index = 0;
432 static int aiff_read_seek(AVFormatContext *s,
433 int stream_index, int64_t timestamp, int flags)
435 return pcm_read_seek(s, stream_index, timestamp, flags);
438 #ifdef CONFIG_AIFF_DEMUXER
439 AVInputFormat aiff_demuxer = {
448 .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
452 #ifdef CONFIG_AIFF_MUXER
453 AVOutputFormat aiff_muxer = {
458 sizeof(AIFFOutputContext),
464 .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},