f11288da |
/* |
47b47bbd |
* AIFF/AIFF-C demuxer |
f11288da |
* Copyright (c) 2006 Patrick Guimond
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
f11288da |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
f11288da |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
f11288da |
* 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 |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
f11288da |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ |
245976da |
#include "libavutil/intfloat_readwrite.h" |
d2d67e42 |
#include "libavutil/dict.h" |
f11288da |
#include "avformat.h" |
e94204df |
#include "pcm.h" |
47b47bbd |
#include "aiff.h" |
91b78272 |
#include "isom.h" |
f11288da |
#define AIFF 0
#define AIFF_C_VERSION1 0xA2805140
|
24c6f152 |
typedef struct {
int64_t data_end;
} AIFFInputContext;
|
fb65d2ca |
static enum CodecID aiff_codec_get_id(int bps) |
f11288da |
{
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 */ |
fb65d2ca |
return CODEC_ID_NONE; |
f11288da |
}
/* returns the size of the found tag */ |
471fe57e |
static int get_tag(AVIOContext *pb, uint32_t * tag) |
f11288da |
{
int size;
if (url_feof(pb)) |
6f3e0b21 |
return AVERROR(EIO); |
f11288da |
|
e63a3628 |
*tag = avio_rl32(pb);
size = avio_rb32(pb); |
f11288da |
if (size < 0)
size = 0x7fffffff;
return size;
}
/* Metadata string read */ |
c0f14458 |
static void get_meta(AVFormatContext *s, const char *key, int size) |
f11288da |
{ |
12ad6671 |
uint8_t *str = av_malloc(size+1);
|
a565c7b7 |
if (str) {
int res = avio_read(s->pb, str, size);
if (res < 0){
av_free(str);
return;
}
size += (size&1)-res;
str[res] = 0; |
e6ba3d42 |
av_dict_set(&s->metadata, key, str, AV_METADATA_DONT_STRDUP_VAL); |
a565c7b7 |
}else
size+= size&1;
avio_skip(s->pb, size); |
f11288da |
}
/* Returns the number of sound data frames or negative on error */ |
471fe57e |
static unsigned int get_aiff_header(AVIOContext *pb, AVCodecContext *codec, |
f11288da |
int size, unsigned version)
{
AVExtFloat ext;
double sample_rate;
unsigned int num_frames;
if (size & 1)
size++; |
72415b2a |
codec->codec_type = AVMEDIA_TYPE_AUDIO; |
e63a3628 |
codec->channels = avio_rb16(pb);
num_frames = avio_rb32(pb);
codec->bits_per_coded_sample = avio_rb16(pb); |
f11288da |
|
e63a3628 |
avio_read(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */ |
f11288da |
sample_rate = av_ext2dbl(ext); /* 80 bits BE IEEE extended float */
codec->sample_rate = sample_rate;
size -= 18;
/* Got an AIFF-C? */
if (version == AIFF_C_VERSION1) { |
e63a3628 |
codec->codec_tag = avio_rl32(pb); |
47b47bbd |
codec->codec_id = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag); |
f11288da |
|
3a957df4 |
switch (codec->codec_id) {
case CODEC_ID_PCM_S16BE: |
dd1c8f3e |
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); |
3a957df4 |
break;
case CODEC_ID_ADPCM_IMA_QT:
codec->block_align = 34*codec->channels; |
e0130159 |
codec->frame_size = 64; |
3a957df4 |
break; |
4649fa90 |
case CODEC_ID_MACE3: |
112249de |
codec->block_align = 2*codec->channels;
codec->frame_size = 6;
break; |
4649fa90 |
case CODEC_ID_MACE6: |
112249de |
codec->block_align = 1*codec->channels; |
4649fa90 |
codec->frame_size = 6;
break; |
5d79f30d |
case CODEC_ID_GSM:
codec->block_align = 33;
codec->frame_size = 160;
break; |
cea65433 |
case CODEC_ID_QCELP:
codec->block_align = 35;
codec->frame_size= 160;
break; |
3a957df4 |
default:
break; |
f11288da |
}
size -= 4;
} else {
/* Need the codec type */ |
dd1c8f3e |
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); |
f11288da |
}
/* Block align needs to be computed in all cases, as the definition
* is specific to applications -> here we use the WAVE format definition */ |
3a957df4 |
if (!codec->block_align) |
dd1c8f3e |
codec->block_align = (codec->bits_per_coded_sample * codec->channels) >> 3; |
f11288da |
|
28da7981 |
codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
codec->sample_rate) * (codec->block_align << 3); |
f11288da |
/* Chunk is over */
if (size) |
45a8a02a |
avio_skip(pb, size); |
f11288da |
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)
{ |
620d1d78 |
int size, filesize; |
bc5c918e |
int64_t offset = 0; |
f11288da |
uint32_t tag;
unsigned version = AIFF_C_VERSION1; |
471fe57e |
AVIOContext *pb = s->pb; |
551b41d4 |
AVStream * st; |
24c6f152 |
AIFFInputContext *aiff = s->priv_data; |
f11288da |
/* check FORM header */
filesize = get_tag(pb, &tag);
if (filesize < 0 || tag != MKTAG('F', 'O', 'R', 'M'))
return AVERROR_INVALIDDATA;
/* AIFF data type */ |
e63a3628 |
tag = avio_rl32(pb); |
f11288da |
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;
st = av_new_stream(s, 0);
if (!st) |
769e10f0 |
return AVERROR(ENOMEM); |
f11288da |
while (filesize > 0) {
/* parse different chunks */
size = get_tag(pb, &tag);
if (size < 0)
return size;
filesize -= size + 8;
switch (tag) { |
54d8fd20 |
case MKTAG('C', 'O', 'M', 'M'): /* Common chunk */
/* Then for the complete header info */ |
24c1d3b5 |
st->nb_frames = get_aiff_header(pb, st->codec, size, version); |
54d8fd20 |
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 */ |
e63a3628 |
version = avio_rb32(pb); |
54d8fd20 |
break;
case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */ |
c0f14458 |
get_meta(s, "title" , size); |
54d8fd20 |
break;
case MKTAG('A', 'U', 'T', 'H'): /* Author chunk */ |
c0f14458 |
get_meta(s, "author" , size); |
54d8fd20 |
break;
case MKTAG('(', 'c', ')', ' '): /* Copyright chunk */ |
c0f14458 |
get_meta(s, "copyright", size); |
54d8fd20 |
break;
case MKTAG('A', 'N', 'N', 'O'): /* Annotation chunk */ |
c0f14458 |
get_meta(s, "comment" , size); |
54d8fd20 |
break;
case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */ |
384c9c2f |
aiff->data_end = avio_tell(pb) + size; |
e63a3628 |
offset = avio_rb32(pb); /* Offset of sound data */
avio_rb32(pb); /* BlockSize... don't care */ |
384c9c2f |
offset += avio_tell(pb); /* Compute absolute data offset */ |
3f60afad |
if (st->codec->block_align) /* Assume COMM already parsed */ |
54d8fd20 |
goto got_sound; |
8978feda |
if (!pb->seekable) { |
54d8fd20 |
av_log(s, AV_LOG_ERROR, "file is not seekable\n");
return -1;
} |
45a8a02a |
avio_skip(pb, size - 8); |
54d8fd20 |
break; |
b8d3daca |
case MKTAG('w', 'a', 'v', 'e'): |
9c7fd997 |
if ((uint64_t)size > (1<<30))
return -1; |
b8d3daca |
st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
if (!st->codec->extradata)
return AVERROR(ENOMEM);
st->codec->extradata_size = size; |
e63a3628 |
avio_read(pb, st->codec->extradata, size); |
b8d3daca |
break; |
8f2e438e |
case MKTAG('C','H','A','N'):
if (size < 12)
return AVERROR_INVALIDDATA; |
91b78272 |
ff_mov_read_chan(s, size, st->codec); |
8f2e438e |
break; |
54d8fd20 |
default: /* Jump */
if (size & 1) /* Always even aligned */
size++; |
45a8a02a |
avio_skip(pb, size); |
f11288da |
}
}
|
3f60afad |
if (!st->codec->block_align) {
av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
return -1;
} |
f11288da |
got_sound:
/* Now positioned, get the sound data start and end */
if (st->nb_frames)
s->file_size = st->nb_frames * st->codec->block_align;
av_set_pts_info(st, 64, 1, st->codec->sample_rate);
st->start_time = 0; |
e0130159 |
st->duration = st->codec->frame_size ?
st->nb_frames * st->codec->frame_size : st->nb_frames; |
f11288da |
/* Position the stream at the first block */ |
f59d8ff8 |
avio_seek(pb, offset, SEEK_SET); |
f11288da |
return 0;
}
#define MAX_SIZE 4096
static int aiff_read_packet(AVFormatContext *s,
AVPacket *pkt)
{ |
a959f0ea |
AVStream *st = s->streams[0]; |
24c6f152 |
AIFFInputContext *aiff = s->priv_data;
int64_t max_size; |
cea65433 |
int res, size; |
f11288da |
|
24c6f152 |
/* calculate size of remaining data */ |
384c9c2f |
max_size = aiff->data_end - avio_tell(s->pb); |
24c6f152 |
if (max_size <= 0)
return AVERROR_EOF;
|
f11288da |
/* Now for that packet */ |
cea65433 |
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); |
f11288da |
if (res < 0)
return res;
/* Only one stream in an AIFF file */
pkt->stream_index = 0;
return 0;
}
|
66355be3 |
AVInputFormat ff_aiff_demuxer = { |
f11288da |
"aiff", |
bde15e74 |
NULL_IF_CONFIG_SMALL("Audio IFF"), |
24c6f152 |
sizeof(AIFFInputContext), |
f11288da |
aiff_probe,
aiff_read_header,
aiff_read_packet, |
4c638f0c |
NULL, |
5256e42c |
pcm_read_seek, |
47b47bbd |
.codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0}, |
f11288da |
}; |