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
22 #include "libavutil/intfloat_readwrite.h"
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') },
47 #define AIFF_C_VERSION1 0xA2805140
53 static enum CodecID
aiff_codec_get_id(int bps
)
56 return CODEC_ID_PCM_S8
;
58 return CODEC_ID_PCM_S16BE
;
60 return CODEC_ID_PCM_S24BE
;
62 return CODEC_ID_PCM_S32BE
;
64 /* bigger than 32 isn't allowed */
68 /* returns the size of the found tag */
69 static int get_tag(ByteIOContext
*pb
, uint32_t * tag
)
85 /* Metadata string read */
86 static void get_meta(AVFormatContext
*s
, const char *key
, int size
)
89 int res
= get_buffer(s
->pb
, str
, FFMIN(sizeof(str
)-1, size
));
98 url_fskip(s
->pb
, size
);
100 av_metadata_set(&s
->metadata
, key
, str
);
103 /* Returns the number of sound data frames or negative on error */
104 static unsigned int get_aiff_header(ByteIOContext
*pb
, AVCodecContext
*codec
,
105 int size
, unsigned version
)
109 unsigned int num_frames
;
113 codec
->codec_type
= CODEC_TYPE_AUDIO
;
114 codec
->channels
= get_be16(pb
);
115 num_frames
= get_be32(pb
);
116 codec
->bits_per_coded_sample
= get_be16(pb
);
118 get_buffer(pb
, (uint8_t*)&ext
, sizeof(ext
));/* Sample rate is in */
119 sample_rate
= av_ext2dbl(ext
); /* 80 bits BE IEEE extended float */
120 codec
->sample_rate
= sample_rate
;
124 if (version
== AIFF_C_VERSION1
) {
125 codec
->codec_tag
= get_le32(pb
);
126 codec
->codec_id
= ff_codec_get_id(codec_aiff_tags
, codec
->codec_tag
);
128 switch (codec
->codec_id
) {
129 case CODEC_ID_PCM_S16BE
:
130 codec
->codec_id
= aiff_codec_get_id(codec
->bits_per_coded_sample
);
131 codec
->bits_per_coded_sample
= av_get_bits_per_sample(codec
->codec_id
);
133 case CODEC_ID_ADPCM_IMA_QT
:
134 codec
->block_align
= 34*codec
->channels
;
135 codec
->frame_size
= 64;
138 codec
->block_align
= 2*codec
->channels
;
139 codec
->frame_size
= 6;
142 codec
->block_align
= 1*codec
->channels
;
143 codec
->frame_size
= 6;
146 codec
->block_align
= 33;
147 codec
->frame_size
= 160;
154 /* Need the codec type */
155 codec
->codec_id
= aiff_codec_get_id(codec
->bits_per_coded_sample
);
156 codec
->bits_per_coded_sample
= av_get_bits_per_sample(codec
->codec_id
);
159 /* Block align needs to be computed in all cases, as the definition
160 * is specific to applications -> here we use the WAVE format definition */
161 if (!codec
->block_align
)
162 codec
->block_align
= (codec
->bits_per_coded_sample
* codec
->channels
) >> 3;
164 codec
->bit_rate
= (codec
->frame_size
? codec
->sample_rate
/codec
->frame_size
:
165 codec
->sample_rate
) * (codec
->block_align
<< 3);
169 url_fseek(pb
, size
, SEEK_CUR
);
174 #if CONFIG_AIFF_MUXER
181 static int aiff_write_header(AVFormatContext
*s
)
183 AIFFOutputContext
*aiff
= s
->priv_data
;
184 ByteIOContext
*pb
= s
->pb
;
185 AVCodecContext
*enc
= s
->streams
[0]->codec
;
186 AVExtFloat sample_rate
;
189 /* First verify if format is ok */
192 if (enc
->codec_tag
!= MKTAG('N','O','N','E'))
195 /* FORM AIFF header */
197 aiff
->form
= url_ftell(pb
);
198 put_be32(pb
, 0); /* file length */
199 put_tag(pb
, aifc
? "AIFC" : "AIFF");
201 if (aifc
) { // compressed audio
202 enc
->bits_per_coded_sample
= 16;
203 if (!enc
->block_align
) {
204 av_log(s
, AV_LOG_ERROR
, "block align not set\n");
210 put_be32(pb
, 0xA2805140);
215 put_be32(pb
, aifc
? 24 : 18); /* size */
216 put_be16(pb
, enc
->channels
); /* Number of channels */
218 aiff
->frames
= url_ftell(pb
);
219 put_be32(pb
, 0); /* Number of frames */
221 if (!enc
->bits_per_coded_sample
)
222 enc
->bits_per_coded_sample
= av_get_bits_per_sample(enc
->codec_id
);
223 if (!enc
->bits_per_coded_sample
) {
224 av_log(s
, AV_LOG_ERROR
, "could not compute bits per sample\n");
227 if (!enc
->block_align
)
228 enc
->block_align
= (enc
->bits_per_coded_sample
* enc
->channels
) >> 3;
230 put_be16(pb
, enc
->bits_per_coded_sample
); /* Sample size */
232 sample_rate
= av_dbl2ext((double)enc
->sample_rate
);
233 put_buffer(pb
, (uint8_t*)&sample_rate
, sizeof(sample_rate
));
236 put_le32(pb
, enc
->codec_tag
);
240 /* Sound data chunk */
242 aiff
->ssnd
= url_ftell(pb
); /* Sound chunk size */
243 put_be32(pb
, 0); /* Sound samples data size */
244 put_be32(pb
, 0); /* Data offset */
245 put_be32(pb
, 0); /* Block-size (block align) */
247 av_set_pts_info(s
->streams
[0], 64, 1, s
->streams
[0]->codec
->sample_rate
);
249 /* Data is starting here */
250 put_flush_packet(pb
);
255 static int aiff_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
257 ByteIOContext
*pb
= s
->pb
;
258 put_buffer(pb
, pkt
->data
, pkt
->size
);
262 static int aiff_write_trailer(AVFormatContext
*s
)
264 ByteIOContext
*pb
= s
->pb
;
265 AIFFOutputContext
*aiff
= s
->priv_data
;
266 AVCodecContext
*enc
= s
->streams
[0]->codec
;
268 /* Chunks sizes must be even */
269 int64_t file_size
, end_size
;
270 end_size
= file_size
= url_ftell(pb
);
276 if (!url_is_streamed(s
->pb
)) {
278 url_fseek(pb
, aiff
->form
, SEEK_SET
);
279 put_be32(pb
, file_size
- aiff
->form
- 4);
281 /* Number of sample frames */
282 url_fseek(pb
, aiff
->frames
, SEEK_SET
);
283 put_be32(pb
, (file_size
-aiff
->ssnd
-12)/enc
->block_align
);
285 /* Sound Data chunk size */
286 url_fseek(pb
, aiff
->ssnd
, SEEK_SET
);
287 put_be32(pb
, file_size
- aiff
->ssnd
- 4);
289 /* return to the end */
290 url_fseek(pb
, end_size
, SEEK_SET
);
292 put_flush_packet(pb
);
297 #endif /* CONFIG_AIFF_MUXER */
299 static int aiff_probe(AVProbeData
*p
)
301 /* check file header */
302 if (p
->buf
[0] == 'F' && p
->buf
[1] == 'O' &&
303 p
->buf
[2] == 'R' && p
->buf
[3] == 'M' &&
304 p
->buf
[8] == 'A' && p
->buf
[9] == 'I' &&
305 p
->buf
[10] == 'F' && (p
->buf
[11] == 'F' || p
->buf
[11] == 'C'))
306 return AVPROBE_SCORE_MAX
;
312 static int aiff_read_header(AVFormatContext
*s
,
313 AVFormatParameters
*ap
)
318 unsigned version
= AIFF_C_VERSION1
;
319 ByteIOContext
*pb
= s
->pb
;
321 AIFFInputContext
*aiff
= s
->priv_data
;
323 /* check FORM header */
324 filesize
= get_tag(pb
, &tag
);
325 if (filesize
< 0 || tag
!= MKTAG('F', 'O', 'R', 'M'))
326 return AVERROR_INVALIDDATA
;
330 if (tag
== MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
332 else if (tag
!= MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
333 return AVERROR_INVALIDDATA
;
337 st
= av_new_stream(s
, 0);
339 return AVERROR(ENOMEM
);
341 while (filesize
> 0) {
342 /* parse different chunks */
343 size
= get_tag(pb
, &tag
);
347 filesize
-= size
+ 8;
350 case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
351 /* Then for the complete header info */
352 st
->nb_frames
= get_aiff_header(pb
, st
->codec
, size
, version
);
353 if (st
->nb_frames
< 0)
354 return st
->nb_frames
;
355 if (offset
> 0) // COMM is after SSND
358 case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
359 version
= get_be32(pb
);
361 case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
362 get_meta(s
, "title" , size
);
364 case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
365 get_meta(s
, "author" , size
);
367 case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
368 get_meta(s
, "copyright", size
);
370 case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
371 get_meta(s
, "comment" , size
);
373 case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
374 aiff
->data_end
= url_ftell(pb
) + size
;
375 offset
= get_be32(pb
); /* Offset of sound data */
376 get_be32(pb
); /* BlockSize... don't care */
377 offset
+= url_ftell(pb
); /* Compute absolute data offset */
378 if (st
->codec
->block_align
) /* Assume COMM already parsed */
380 if (url_is_streamed(pb
)) {
381 av_log(s
, AV_LOG_ERROR
, "file is not seekable\n");
384 url_fskip(pb
, size
- 8);
386 case MKTAG('w', 'a', 'v', 'e'):
387 if ((uint64_t)size
> (1<<30))
389 st
->codec
->extradata
= av_mallocz(size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
390 if (!st
->codec
->extradata
)
391 return AVERROR(ENOMEM
);
392 st
->codec
->extradata_size
= size
;
393 get_buffer(pb
, st
->codec
->extradata
, size
);
396 if (size
& 1) /* Always even aligned */
398 url_fskip (pb
, size
);
402 if (!st
->codec
->block_align
) {
403 av_log(s
, AV_LOG_ERROR
, "could not find COMM tag\n");
408 /* Now positioned, get the sound data start and end */
410 s
->file_size
= st
->nb_frames
* st
->codec
->block_align
;
412 av_set_pts_info(st
, 64, 1, st
->codec
->sample_rate
);
414 st
->duration
= st
->codec
->frame_size
?
415 st
->nb_frames
* st
->codec
->frame_size
: st
->nb_frames
;
417 /* Position the stream at the first block */
418 url_fseek(pb
, offset
, SEEK_SET
);
423 #define MAX_SIZE 4096
425 static int aiff_read_packet(AVFormatContext
*s
,
428 AVStream
*st
= s
->streams
[0];
429 AIFFInputContext
*aiff
= s
->priv_data
;
433 /* calculate size of remaining data */
434 max_size
= aiff
->data_end
- url_ftell(s
->pb
);
438 /* Now for that packet */
439 max_size
= FFMIN(max_size
, (MAX_SIZE
/ st
->codec
->block_align
) * st
->codec
->block_align
);
440 res
= av_get_packet(s
->pb
, pkt
, max_size
);
444 /* Only one stream in an AIFF file */
445 pkt
->stream_index
= 0;
449 #if CONFIG_AIFF_DEMUXER
450 AVInputFormat aiff_demuxer
= {
452 NULL_IF_CONFIG_SMALL("Audio IFF"),
453 sizeof(AIFFInputContext
),
459 .codec_tag
= (const AVCodecTag
* const []){codec_aiff_tags
, 0},
463 #if CONFIG_AIFF_MUXER
464 AVOutputFormat aiff_muxer
= {
466 NULL_IF_CONFIG_SMALL("Audio IFF"),
469 sizeof(AIFFOutputContext
),
475 .codec_tag
= (const AVCodecTag
* const []){codec_aiff_tags
, 0},