Do not read data past the end of the SSND chunk in the AIFF demuxer.
[FFMpeg-mirror/lagarith.git] / libavformat / aiff.c
blobb9ee595805b2524f62425c5fe832b4b2c8739c9f
1 /*
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"
23 #include "avformat.h"
24 #include "raw.h"
25 #include "riff.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') },
43 { 0, 0 },
46 #define AIFF 0
47 #define AIFF_C_VERSION1 0xA2805140
49 typedef struct {
50 int64_t data_end;
51 } AIFFInputContext;
53 static enum CodecID aiff_codec_get_id(int bps)
55 if (bps <= 8)
56 return CODEC_ID_PCM_S8;
57 if (bps <= 16)
58 return CODEC_ID_PCM_S16BE;
59 if (bps <= 24)
60 return CODEC_ID_PCM_S24BE;
61 if (bps <= 32)
62 return CODEC_ID_PCM_S32BE;
64 /* bigger than 32 isn't allowed */
65 return CODEC_ID_NONE;
68 /* returns the size of the found tag */
69 static int get_tag(ByteIOContext *pb, uint32_t * tag)
71 int size;
73 if (url_feof(pb))
74 return AVERROR(EIO);
76 *tag = get_le32(pb);
77 size = get_be32(pb);
79 if (size < 0)
80 size = 0x7fffffff;
82 return size;
85 /* Metadata string read */
86 static void get_meta(AVFormatContext *s, const char *key, int size)
88 uint8_t str[1024];
89 int res = get_buffer(s->pb, str, FFMIN(sizeof(str)-1, size));
90 if (res < 0)
91 return;
93 str[res] = 0;
94 if (size & 1)
95 size++;
96 size -= res;
97 if (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)
107 AVExtFloat ext;
108 double sample_rate;
109 unsigned int num_frames;
111 if (size & 1)
112 size++;
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;
121 size -= 18;
123 /* Got an AIFF-C? */
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);
132 break;
133 case CODEC_ID_ADPCM_IMA_QT:
134 codec->block_align = 34*codec->channels;
135 codec->frame_size = 64;
136 break;
137 case CODEC_ID_MACE3:
138 codec->block_align = 2*codec->channels;
139 codec->frame_size = 6;
140 break;
141 case CODEC_ID_MACE6:
142 codec->block_align = 1*codec->channels;
143 codec->frame_size = 6;
144 break;
145 case CODEC_ID_GSM:
146 codec->block_align = 33;
147 codec->frame_size = 160;
148 break;
149 default:
150 break;
152 size -= 4;
153 } else {
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);
167 /* Chunk is over */
168 if (size)
169 url_fseek(pb, size, SEEK_CUR);
171 return num_frames;
174 #if CONFIG_AIFF_MUXER
175 typedef struct {
176 int64_t form;
177 int64_t frames;
178 int64_t ssnd;
179 } AIFFOutputContext;
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;
187 int aifc = 0;
189 /* First verify if format is ok */
190 if (!enc->codec_tag)
191 return -1;
192 if (enc->codec_tag != MKTAG('N','O','N','E'))
193 aifc = 1;
195 /* FORM AIFF header */
196 put_tag(pb, "FORM");
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");
205 return -1;
207 /* Version chunk */
208 put_tag(pb, "FVER");
209 put_be32(pb, 4);
210 put_be32(pb, 0xA2805140);
213 /* Common chunk */
214 put_tag(pb, "COMM");
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");
225 return -1;
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));
235 if (aifc) {
236 put_le32(pb, enc->codec_tag);
237 put_be16(pb, 0);
240 /* Sound data chunk */
241 put_tag(pb, "SSND");
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);
252 return 0;
255 static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
257 ByteIOContext *pb = s->pb;
258 put_buffer(pb, pkt->data, pkt->size);
259 return 0;
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);
271 if (file_size & 1) {
272 put_byte(pb, 0);
273 end_size++;
276 if (!url_is_streamed(s->pb)) {
277 /* File length */
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);
295 return 0;
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;
307 else
308 return 0;
311 /* aiff input */
312 static int aiff_read_header(AVFormatContext *s,
313 AVFormatParameters *ap)
315 int size, filesize;
316 int64_t offset = 0;
317 uint32_t tag;
318 unsigned version = AIFF_C_VERSION1;
319 ByteIOContext *pb = s->pb;
320 AVStream * st;
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;
328 /* AIFF data type */
329 tag = get_le32(pb);
330 if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
331 version = AIFF;
332 else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
333 return AVERROR_INVALIDDATA;
335 filesize -= 4;
337 st = av_new_stream(s, 0);
338 if (!st)
339 return AVERROR(ENOMEM);
341 while (filesize > 0) {
342 /* parse different chunks */
343 size = get_tag(pb, &tag);
344 if (size < 0)
345 return size;
347 filesize -= size + 8;
349 switch (tag) {
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
356 goto got_sound;
357 break;
358 case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
359 version = get_be32(pb);
360 break;
361 case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
362 get_meta(s, "title" , size);
363 break;
364 case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
365 get_meta(s, "author" , size);
366 break;
367 case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
368 get_meta(s, "copyright", size);
369 break;
370 case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
371 get_meta(s, "comment" , size);
372 break;
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 */
379 goto got_sound;
380 if (url_is_streamed(pb)) {
381 av_log(s, AV_LOG_ERROR, "file is not seekable\n");
382 return -1;
384 url_fskip(pb, size - 8);
385 break;
386 case MKTAG('w', 'a', 'v', 'e'):
387 if ((uint64_t)size > (1<<30))
388 return -1;
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);
394 break;
395 default: /* Jump */
396 if (size & 1) /* Always even aligned */
397 size++;
398 url_fskip (pb, size);
402 if (!st->codec->block_align) {
403 av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
404 return -1;
407 got_sound:
408 /* Now positioned, get the sound data start and end */
409 if (st->nb_frames)
410 s->file_size = st->nb_frames * st->codec->block_align;
412 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
413 st->start_time = 0;
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);
420 return 0;
423 #define MAX_SIZE 4096
425 static int aiff_read_packet(AVFormatContext *s,
426 AVPacket *pkt)
428 AVStream *st = s->streams[0];
429 AIFFInputContext *aiff = s->priv_data;
430 int64_t max_size;
431 int res;
433 /* calculate size of remaining data */
434 max_size = aiff->data_end - url_ftell(s->pb);
435 if (max_size <= 0)
436 return AVERROR_EOF;
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);
441 if (res < 0)
442 return res;
444 /* Only one stream in an AIFF file */
445 pkt->stream_index = 0;
446 return 0;
449 #if CONFIG_AIFF_DEMUXER
450 AVInputFormat aiff_demuxer = {
451 "aiff",
452 NULL_IF_CONFIG_SMALL("Audio IFF"),
453 sizeof(AIFFInputContext),
454 aiff_probe,
455 aiff_read_header,
456 aiff_read_packet,
457 NULL,
458 pcm_read_seek,
459 .codec_tag= (const AVCodecTag* const []){codec_aiff_tags, 0},
461 #endif
463 #if CONFIG_AIFF_MUXER
464 AVOutputFormat aiff_muxer = {
465 "aiff",
466 NULL_IF_CONFIG_SMALL("Audio IFF"),
467 "audio/aiff",
468 "aif,aiff,afc,aifc",
469 sizeof(AIFFOutputContext),
470 CODEC_ID_PCM_S16BE,
471 CODEC_ID_NONE,
472 aiff_write_header,
473 aiff_write_packet,
474 aiff_write_trailer,
475 .codec_tag= (const AVCodecTag* const []){codec_aiff_tags, 0},
477 #endif