2006-02-02 17:21:19 +02:00
|
|
|
/*
|
2009-10-15 12:45:16 +03:00
|
|
|
* AIFF/AIFF-C demuxer
|
2006-02-02 17:21:19 +02:00
|
|
|
* Copyright (c) 2006 Patrick Guimond
|
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2006-02-02 17:21:19 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-02-02 17:21:19 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2006-02-02 17:21:19 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-02-02 17:21:19 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2008-05-09 14:56:36 +03:00
|
|
|
|
2011-11-27 17:04:16 +03:00
|
|
|
#include "libavutil/mathematics.h"
|
2011-05-22 13:46:29 +03:00
|
|
|
#include "libavutil/dict.h"
|
2006-02-02 17:21:19 +02:00
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2010-08-31 00:17:34 +03:00
|
|
|
#include "pcm.h"
|
2009-10-15 12:45:16 +03:00
|
|
|
#include "aiff.h"
|
2011-04-28 11:45:32 +03:00
|
|
|
#include "isom.h"
|
2006-02-02 17:21:19 +02:00
|
|
|
|
|
|
|
#define AIFF 0
|
|
|
|
#define AIFF_C_VERSION1 0xA2805140
|
|
|
|
|
2009-10-13 03:19:34 +03:00
|
|
|
typedef struct {
|
|
|
|
int64_t data_end;
|
|
|
|
} AIFFInputContext;
|
|
|
|
|
2008-10-02 19:03:00 +03:00
|
|
|
static enum CodecID aiff_codec_get_id(int bps)
|
2006-02-02 17:21:19 +02:00
|
|
|
{
|
|
|
|
if (bps <= 8)
|
|
|
|
return CODEC_ID_PCM_S8;
|
|
|
|
if (bps <= 16)
|
|
|
|
return CODEC_ID_PCM_S16BE;
|
|
|
|
if (bps <= 24)
|
|
|
|
return CODEC_ID_PCM_S24BE;
|
|
|
|
if (bps <= 32)
|
|
|
|
return CODEC_ID_PCM_S32BE;
|
|
|
|
|
|
|
|
/* bigger than 32 isn't allowed */
|
2008-10-02 19:03:00 +03:00
|
|
|
return CODEC_ID_NONE;
|
2006-02-02 17:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns the size of the found tag */
|
2011-02-20 12:04:12 +02:00
|
|
|
static int get_tag(AVIOContext *pb, uint32_t * tag)
|
2006-02-02 17:21:19 +02:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (url_feof(pb))
|
2007-07-19 18:23:32 +03:00
|
|
|
return AVERROR(EIO);
|
2006-02-02 17:21:19 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
*tag = avio_rl32(pb);
|
|
|
|
size = avio_rb32(pb);
|
2006-02-02 17:21:19 +02:00
|
|
|
|
|
|
|
if (size < 0)
|
|
|
|
size = 0x7fffffff;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Metadata string read */
|
2009-02-03 01:43:12 +02:00
|
|
|
static void get_meta(AVFormatContext *s, const char *key, int size)
|
2006-02-02 17:21:19 +02:00
|
|
|
{
|
2009-12-13 22:27:29 +02:00
|
|
|
uint8_t *str = av_malloc(size+1);
|
|
|
|
|
2011-05-06 18:39:31 +03:00
|
|
|
if (str) {
|
|
|
|
int res = avio_read(s->pb, str, size);
|
|
|
|
if (res < 0){
|
|
|
|
av_free(str);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size += (size&1)-res;
|
|
|
|
str[res] = 0;
|
2011-06-13 20:02:50 +03:00
|
|
|
av_dict_set(&s->metadata, key, str, AV_METADATA_DONT_STRDUP_VAL);
|
2011-05-06 18:39:31 +03:00
|
|
|
}else
|
|
|
|
size+= size&1;
|
|
|
|
|
|
|
|
avio_skip(s->pb, size);
|
2006-02-02 17:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the number of sound data frames or negative on error */
|
2011-02-20 12:04:12 +02:00
|
|
|
static unsigned int get_aiff_header(AVIOContext *pb, AVCodecContext *codec,
|
2006-02-02 17:21:19 +02:00
|
|
|
int size, unsigned version)
|
|
|
|
{
|
2011-11-27 17:04:16 +03:00
|
|
|
int exp;
|
|
|
|
uint64_t val;
|
2006-02-02 17:21:19 +02:00
|
|
|
double sample_rate;
|
|
|
|
unsigned int num_frames;
|
|
|
|
|
|
|
|
if (size & 1)
|
|
|
|
size++;
|
2010-03-31 02:30:55 +03:00
|
|
|
codec->codec_type = AVMEDIA_TYPE_AUDIO;
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->channels = avio_rb16(pb);
|
|
|
|
num_frames = avio_rb32(pb);
|
|
|
|
codec->bits_per_coded_sample = avio_rb16(pb);
|
2006-02-02 17:21:19 +02:00
|
|
|
|
2011-11-27 17:04:16 +03:00
|
|
|
exp = avio_rb16(pb);
|
|
|
|
val = avio_rb64(pb);
|
|
|
|
sample_rate = ldexp(val, exp - 16383 - 63);
|
2006-02-02 17:21:19 +02:00
|
|
|
codec->sample_rate = sample_rate;
|
|
|
|
size -= 18;
|
|
|
|
|
|
|
|
/* Got an AIFF-C? */
|
|
|
|
if (version == AIFF_C_VERSION1) {
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->codec_tag = avio_rl32(pb);
|
2009-10-15 12:45:16 +03:00
|
|
|
codec->codec_id = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
|
2006-02-02 17:21:19 +02:00
|
|
|
|
2008-02-25 12:57:22 +02:00
|
|
|
switch (codec->codec_id) {
|
|
|
|
case CODEC_ID_PCM_S16BE:
|
2008-09-08 17:24:59 +03:00
|
|
|
codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
|
|
|
|
codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
|
2008-02-25 12:57:22 +02:00
|
|
|
break;
|
|
|
|
case CODEC_ID_ADPCM_IMA_QT:
|
|
|
|
codec->block_align = 34*codec->channels;
|
2008-02-25 13:34:26 +02:00
|
|
|
codec->frame_size = 64;
|
2008-02-25 12:57:22 +02:00
|
|
|
break;
|
2008-02-25 14:59:42 +02:00
|
|
|
case CODEC_ID_MACE3:
|
2008-03-05 14:06:57 +02:00
|
|
|
codec->block_align = 2*codec->channels;
|
|
|
|
codec->frame_size = 6;
|
|
|
|
break;
|
2008-02-25 14:59:42 +02:00
|
|
|
case CODEC_ID_MACE6:
|
2008-03-05 14:06:57 +02:00
|
|
|
codec->block_align = 1*codec->channels;
|
2008-02-25 14:59:42 +02:00
|
|
|
codec->frame_size = 6;
|
|
|
|
break;
|
2008-09-05 01:35:05 +03:00
|
|
|
case CODEC_ID_GSM:
|
|
|
|
codec->block_align = 33;
|
|
|
|
codec->frame_size = 160;
|
|
|
|
break;
|
2009-12-01 00:01:21 +02:00
|
|
|
case CODEC_ID_QCELP:
|
|
|
|
codec->block_align = 35;
|
|
|
|
codec->frame_size= 160;
|
|
|
|
break;
|
2008-02-25 12:57:22 +02:00
|
|
|
default:
|
|
|
|
break;
|
2006-02-02 17:21:19 +02:00
|
|
|
}
|
|
|
|
size -= 4;
|
|
|
|
} else {
|
|
|
|
/* Need the codec type */
|
2008-09-08 17:24:59 +03:00
|
|
|
codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
|
|
|
|
codec->bits_per_coded_sample = av_get_bits_per_sample(codec->codec_id);
|
2006-02-02 17:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Block align needs to be computed in all cases, as the definition
|
|
|
|
* is specific to applications -> here we use the WAVE format definition */
|
2008-02-25 12:57:22 +02:00
|
|
|
if (!codec->block_align)
|
2008-09-08 17:24:59 +03:00
|
|
|
codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3;
|
2006-02-02 17:21:19 +02:00
|
|
|
|
2008-03-05 14:02:32 +02:00
|
|
|
codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
|
|
|
|
codec->sample_rate) * (codec->block_align << 3);
|
2006-02-02 17:21:19 +02:00
|
|
|
|
|
|
|
/* Chunk is over */
|
|
|
|
if (size)
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, size);
|
2006-02-02 17:21:19 +02:00
|
|
|
|
|
|
|
return num_frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aiff_probe(AVProbeData *p)
|
|
|
|
{
|
|
|
|
/* check file header */
|
|
|
|
if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
|
|
|
|
p->buf[2] == 'R' && p->buf[3] == 'M' &&
|
|
|
|
p->buf[8] == 'A' && p->buf[9] == 'I' &&
|
|
|
|
p->buf[10] == 'F' && (p->buf[11] == 'F' || p->buf[11] == 'C'))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* aiff input */
|
|
|
|
static int aiff_read_header(AVFormatContext *s,
|
|
|
|
AVFormatParameters *ap)
|
|
|
|
{
|
2007-06-26 00:31:33 +03:00
|
|
|
int size, filesize;
|
2008-10-03 13:16:29 +03:00
|
|
|
int64_t offset = 0;
|
2006-02-02 17:21:19 +02:00
|
|
|
uint32_t tag;
|
|
|
|
unsigned version = AIFF_C_VERSION1;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2009-04-17 21:18:29 +03:00
|
|
|
AVStream * st;
|
2009-10-13 03:19:34 +03:00
|
|
|
AIFFInputContext *aiff = s->priv_data;
|
2006-02-02 17:21:19 +02:00
|
|
|
|
|
|
|
/* check FORM header */
|
|
|
|
filesize = get_tag(pb, &tag);
|
|
|
|
if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
/* AIFF data type */
|
2011-02-21 17:43:01 +02:00
|
|
|
tag = avio_rl32(pb);
|
2006-02-02 17:21:19 +02:00
|
|
|
if (tag == MKTAG('A', 'I', 'F', 'F')) /* Got an AIFF file */
|
|
|
|
version = AIFF;
|
|
|
|
else if (tag != MKTAG('A', 'I', 'F', 'C')) /* An AIFF-C file then */
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
filesize -= 4;
|
|
|
|
|
2011-06-18 12:43:24 +03:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2006-02-02 17:21:19 +02:00
|
|
|
if (!st)
|
2007-07-19 18:21:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2006-02-02 17:21:19 +02:00
|
|
|
|
|
|
|
while (filesize > 0) {
|
|
|
|
/* parse different chunks */
|
|
|
|
size = get_tag(pb, &tag);
|
|
|
|
if (size < 0)
|
|
|
|
return size;
|
|
|
|
|
|
|
|
filesize -= size + 8;
|
|
|
|
|
|
|
|
switch (tag) {
|
2008-02-25 14:23:27 +02:00
|
|
|
case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
|
|
|
|
/* Then for the complete header info */
|
2008-05-16 01:14:29 +03:00
|
|
|
st->nb_frames = get_aiff_header(pb, st->codec, size, version);
|
2008-02-25 14:23:27 +02:00
|
|
|
if (st->nb_frames < 0)
|
|
|
|
return st->nb_frames;
|
|
|
|
if (offset > 0) // COMM is after SSND
|
|
|
|
goto got_sound;
|
|
|
|
break;
|
|
|
|
case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
|
2011-02-21 17:43:01 +02:00
|
|
|
version = avio_rb32(pb);
|
2008-02-25 14:23:27 +02:00
|
|
|
break;
|
|
|
|
case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
|
2009-02-03 01:43:12 +02:00
|
|
|
get_meta(s, "title" , size);
|
2008-02-25 14:23:27 +02:00
|
|
|
break;
|
|
|
|
case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */
|
2009-02-03 01:43:12 +02:00
|
|
|
get_meta(s, "author" , size);
|
2008-02-25 14:23:27 +02:00
|
|
|
break;
|
|
|
|
case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */
|
2009-02-03 01:43:12 +02:00
|
|
|
get_meta(s, "copyright", size);
|
2008-02-25 14:23:27 +02:00
|
|
|
break;
|
|
|
|
case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */
|
2009-02-03 01:43:12 +02:00
|
|
|
get_meta(s, "comment" , size);
|
2008-02-25 14:23:27 +02:00
|
|
|
break;
|
|
|
|
case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
|
2011-03-03 21:11:45 +02:00
|
|
|
aiff->data_end = avio_tell(pb) + size;
|
2011-02-21 17:43:01 +02:00
|
|
|
offset = avio_rb32(pb); /* Offset of sound data */
|
|
|
|
avio_rb32(pb); /* BlockSize... don't care */
|
2011-03-03 21:11:45 +02:00
|
|
|
offset += avio_tell(pb); /* Compute absolute data offset */
|
2008-05-16 01:47:40 +03:00
|
|
|
if (st->codec->block_align) /* Assume COMM already parsed */
|
2008-02-25 14:23:27 +02:00
|
|
|
goto got_sound;
|
2011-03-05 22:06:46 +02:00
|
|
|
if (!pb->seekable) {
|
2008-02-25 14:23:27 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "file is not seekable\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, size - 8);
|
2008-02-25 14:23:27 +02:00
|
|
|
break;
|
2008-02-25 14:29:34 +02:00
|
|
|
case MKTAG('w', 'a', 'v', 'e'):
|
2008-02-25 14:38:37 +02:00
|
|
|
if ((uint64_t)size > (1<<30))
|
|
|
|
return -1;
|
2008-02-25 14:29:34 +02:00
|
|
|
st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!st->codec->extradata)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
st->codec->extradata_size = size;
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_read(pb, st->codec->extradata, size);
|
2008-02-25 14:29:34 +02:00
|
|
|
break;
|
2011-04-23 11:31:03 +03:00
|
|
|
case MKTAG('C','H','A','N'):
|
|
|
|
if (size < 12)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2011-04-28 11:45:32 +03:00
|
|
|
ff_mov_read_chan(s, size, st->codec);
|
2011-04-23 11:31:03 +03:00
|
|
|
break;
|
2008-02-25 14:23:27 +02:00
|
|
|
default: /* Jump */
|
|
|
|
if (size & 1) /* Always even aligned */
|
|
|
|
size++;
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, size);
|
2006-02-02 17:21:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-16 01:47:40 +03:00
|
|
|
if (!st->codec->block_align) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-02-02 17:21:19 +02:00
|
|
|
|
|
|
|
got_sound:
|
|
|
|
/* Now positioned, get the sound data start and end */
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
|
2006-02-02 17:21:19 +02:00
|
|
|
st->start_time = 0;
|
2008-02-25 13:34:26 +02:00
|
|
|
st->duration = st->codec->frame_size ?
|
|
|
|
st->nb_frames * st->codec->frame_size : st->nb_frames;
|
2006-02-02 17:21:19 +02:00
|
|
|
|
|
|
|
/* Position the stream at the first block */
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(pb, offset, SEEK_SET);
|
2006-02-02 17:21:19 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_SIZE 4096
|
|
|
|
|
|
|
|
static int aiff_read_packet(AVFormatContext *s,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
2006-05-15 15:38:33 +03:00
|
|
|
AVStream *st = s->streams[0];
|
2009-10-13 03:19:34 +03:00
|
|
|
AIFFInputContext *aiff = s->priv_data;
|
|
|
|
int64_t max_size;
|
2009-12-01 00:01:21 +02:00
|
|
|
int res, size;
|
2006-02-02 17:21:19 +02:00
|
|
|
|
2009-10-13 03:19:34 +03:00
|
|
|
/* calculate size of remaining data */
|
2011-03-03 21:11:45 +02:00
|
|
|
max_size = aiff->data_end - avio_tell(s->pb);
|
2009-10-13 03:19:34 +03:00
|
|
|
if (max_size <= 0)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2006-02-02 17:21:19 +02:00
|
|
|
/* Now for that packet */
|
2009-12-01 00:01:21 +02:00
|
|
|
if (st->codec->block_align >= 33) // GSM, QCLP, IMA4
|
|
|
|
size = st->codec->block_align;
|
|
|
|
else
|
|
|
|
size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
|
|
|
|
size = FFMIN(max_size, size);
|
|
|
|
res = av_get_packet(s->pb, pkt, size);
|
2006-02-02 17:21:19 +02:00
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
/* Only one stream in an AIFF file */
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVInputFormat ff_aiff_demuxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "aiff",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Audio IFF"),
|
|
|
|
.priv_data_size = sizeof(AIFFInputContext),
|
|
|
|
.read_probe = aiff_probe,
|
|
|
|
.read_header = aiff_read_header,
|
|
|
|
.read_packet = aiff_read_packet,
|
|
|
|
.read_seek = pcm_read_seek,
|
2009-10-15 12:45:16 +03:00
|
|
|
.codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
|
2006-02-02 17:21:19 +02:00
|
|
|
};
|