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
22 #include "libavutil/intfloat_readwrite.h"
28 #define AIFF_C_VERSION1 0xA2805140
34 static enum CodecID aiff_codec_get_id(int bps)
37 return CODEC_ID_PCM_S8;
39 return CODEC_ID_PCM_S16BE;
41 return CODEC_ID_PCM_S24BE;
43 return CODEC_ID_PCM_S32BE;
45 /* bigger than 32 isn't allowed */
49 /* returns the size of the found tag */
50 static int get_tag(AVIOContext *pb, uint32_t * tag)
66 /* Metadata string read */
67 static void get_meta(AVFormatContext *s, const char *key, int size)
69 uint8_t *str = av_malloc(size+1);
73 url_fskip(s->pb, size);
77 res = avio_read(s->pb, str, size);
82 av_metadata_set2(&s->metadata, key, str, AV_METADATA_DONT_STRDUP_VAL);
85 /* Returns the number of sound data frames or negative on error */
86 static unsigned int get_aiff_header(AVIOContext *pb, AVCodecContext *codec,
87 int size, unsigned version)
91 unsigned int num_frames;
95 codec->codec_type = AVMEDIA_TYPE_AUDIO;
96 codec->channels = avio_rb16(pb);
97 num_frames = avio_rb32(pb);
98 codec->bits_per_coded_sample = avio_rb16(pb);
100 avio_read(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
101 sample_rate = av_ext2dbl(ext); /* 80 bits BE IEEE extended float */
102 codec->sample_rate = sample_rate;
106 if (version == AIFF_C_VERSION1) {
107 codec->codec_tag = avio_rl32(pb);
108 codec->codec_id = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
110 switch (codec->codec_id) {
111 case CODEC_ID_PCM_S16BE:
112 codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
113 codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
115 case CODEC_ID_ADPCM_IMA_QT:
116 codec->block_align = 34*codec->channels;
117 codec->frame_size = 64;
120 codec->block_align = 2*codec->channels;
121 codec->frame_size = 6;
124 codec->block_align = 1*codec->channels;
125 codec->frame_size = 6;
128 codec->block_align = 33;
129 codec->frame_size = 160;
132 codec->block_align = 35;
133 codec->frame_size= 160;
140 /* Need the codec type */
141 codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
142 codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
145 /* Block align needs to be computed in all cases, as the definition
146 * is specific to applications -> here we use the WAVE format definition */
147 if (!codec->block_align)
148 codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
150 codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
151 codec->sample_rate) * (codec->block_align << 3);
155 url_fseek(pb, size, SEEK_CUR);
160 static int aiff_probe(AVProbeData *p)
162 /* check file header */
163 if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
164 p->buf[2] == 'R' && p->buf[3] == 'M' &&
165 p->buf[8] == 'A' && p->buf[9] == 'I' &&
166 p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
167 return AVPROBE_SCORE_MAX;
173 static int aiff_read_header(AVFormatContext *s,
174 AVFormatParameters *ap)
179 unsigned version = AIFF_C_VERSION1;
180 AVIOContext *pb = s->pb;
182 AIFFInputContext *aiff = s->priv_data;
184 /* check FORM header */
185 filesize = get_tag(pb, &tag);
186 if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
187 return AVERROR_INVALIDDATA;
191 if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
193 else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
194 return AVERROR_INVALIDDATA;
198 st = av_new_stream(s, 0);
200 return AVERROR(ENOMEM);
202 while (filesize > 0) {
203 /* parse different chunks */
204 size = get_tag(pb, &tag);
208 filesize -= size + 8;
211 case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
212 /* Then for the complete header info */
213 st->nb_frames = get_aiff_header(pb, st->codec, size, version);
214 if (st->nb_frames < 0)
215 return st->nb_frames;
216 if (offset > 0) // COMM is after SSND
219 case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
220 version = avio_rb32(pb);
222 case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
223 get_meta(s, "title" , size);
225 case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
226 get_meta(s, "author" , size);
228 case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
229 get_meta(s, "copyright", size);
231 case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
232 get_meta(s, "comment" , size);
234 case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
235 aiff->data_end = url_ftell(pb) + size;
236 offset = avio_rb32(pb); /* Offset of sound data */
237 avio_rb32(pb); /* BlockSize... don't care */
238 offset += url_ftell(pb); /* Compute absolute data offset */
239 if (st->codec->block_align) /* Assume COMM already parsed */
241 if (url_is_streamed(pb)) {
242 av_log(s, AV_LOG_ERROR, "file is not seekable\n");
245 url_fskip(pb, size - 8);
247 case MKTAG('w', 'a', 'v', 'e'):
248 if ((uint64_t)size > (1<<30))
250 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
251 if (!st->codec->extradata)
252 return AVERROR(ENOMEM);
253 st->codec->extradata_size = size;
254 avio_read(pb, st->codec->extradata, size);
257 if (size & 1) /* Always even aligned */
259 url_fskip (pb, size);
263 if (!st->codec->block_align) {
264 av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
269 /* Now positioned, get the sound data start and end */
271 s->file_size = st->nb_frames * st->codec->block_align;
273 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
275 st->duration = st->codec->frame_size ?
276 st->nb_frames * st->codec->frame_size : st->nb_frames;
278 /* Position the stream at the first block */
279 url_fseek(pb, offset, SEEK_SET);
284 #define MAX_SIZE 4096
286 static int aiff_read_packet(AVFormatContext *s,
289 AVStream *st = s->streams[0];
290 AIFFInputContext *aiff = s->priv_data;
294 /* calculate size of remaining data */
295 max_size = aiff->data_end - url_ftell(s->pb);
299 /* Now for that packet */
300 if (st->codec->block_align >= 33) // GSM, QCLP, IMA4
301 size = st->codec->block_align;
303 size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
304 size = FFMIN(max_size, size);
305 res = av_get_packet(s->pb, pkt, size);
309 /* Only one stream in an AIFF file */
310 pkt->stream_index = 0;
314 AVInputFormat ff_aiff_demuxer = {
316 NULL_IF_CONFIG_SMALL("Audio IFF"),
317 sizeof(AIFFInputContext),
323 .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},