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 |
|
e367d906 |
#include "libavutil/intreadwrite.h" |
3383a53e |
#include "libavutil/mathematics.h" |
d2d67e42 |
#include "libavutil/dict.h" |
f11288da |
#include "avformat.h" |
c3f9ebf7 |
#include "internal.h" |
e94204df |
#include "pcm.h" |
47b47bbd |
#include "aiff.h" |
91b78272 |
#include "isom.h" |
8ac3868f |
#include "id3v2.h" |
c4dba58f |
#include "mov_chan.h" |
f11288da |
#define AIFF 0
#define AIFF_C_VERSION1 0xA2805140
|
24c6f152 |
typedef struct {
int64_t data_end; |
8d1a20aa |
int block_duration; |
24c6f152 |
} AIFFInputContext;
|
36ef5369 |
static enum AVCodecID aiff_codec_get_id(int bps) |
f11288da |
{
if (bps <= 8) |
36ef5369 |
return AV_CODEC_ID_PCM_S8; |
f11288da |
if (bps <= 16) |
36ef5369 |
return AV_CODEC_ID_PCM_S16BE; |
f11288da |
if (bps <= 24) |
36ef5369 |
return AV_CODEC_ID_PCM_S24BE; |
f11288da |
if (bps <= 32) |
36ef5369 |
return AV_CODEC_ID_PCM_S32BE; |
f11288da |
/* bigger than 32 isn't allowed */ |
36ef5369 |
return AV_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; |
e37f161e |
av_dict_set(&s->metadata, key, str, AV_DICT_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 */ |
8d1a20aa |
static unsigned int get_aiff_header(AVFormatContext *s, int size,
unsigned version) |
f11288da |
{ |
8d1a20aa |
AVIOContext *pb = s->pb;
AVCodecContext *codec = s->streams[0]->codec;
AIFFInputContext *aiff = s->priv_data; |
3383a53e |
int exp;
uint64_t val; |
f11288da |
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); |
99f50907 |
codec->bits_per_coded_sample = avio_rb16(pb); |
f11288da |
|
3383a53e |
exp = avio_rb16(pb);
val = avio_rb64(pb);
sample_rate = ldexp(val, exp - 16383 - 63); |
f11288da |
codec->sample_rate = sample_rate;
size -= 18;
|
b38b7cc3 |
/* get codec id for AIFF-C */ |
f11288da |
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); |
b38b7cc3 |
size -= 4;
} |
f11288da |
|
36ef5369 |
if (version != AIFF_C_VERSION1 || codec->codec_id == AV_CODEC_ID_PCM_S16BE) { |
b38b7cc3 |
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);
aiff->block_duration = 1;
} else { |
3a957df4 |
switch (codec->codec_id) { |
36ef5369 |
case AV_CODEC_ID_PCM_F32BE:
case AV_CODEC_ID_PCM_F64BE:
case AV_CODEC_ID_PCM_S16LE:
case AV_CODEC_ID_PCM_ALAW:
case AV_CODEC_ID_PCM_MULAW: |
8d1a20aa |
aiff->block_duration = 1; |
3a957df4 |
break; |
36ef5369 |
case AV_CODEC_ID_ADPCM_IMA_QT: |
3a957df4 |
codec->block_align = 34*codec->channels;
break; |
36ef5369 |
case AV_CODEC_ID_MACE3: |
112249de |
codec->block_align = 2*codec->channels;
break; |
9860fb44 |
case AV_CODEC_ID_ADPCM_G726LE:
codec->bits_per_coded_sample = 5; |
c153ea47 |
case AV_CODEC_ID_ADPCM_G722: |
36ef5369 |
case AV_CODEC_ID_MACE6: |
112249de |
codec->block_align = 1*codec->channels; |
4649fa90 |
break; |
36ef5369 |
case AV_CODEC_ID_GSM: |
5d79f30d |
codec->block_align = 33;
break; |
3a957df4 |
default: |
8d393ce3 |
aiff->block_duration = 1; |
3a957df4 |
break; |
f11288da |
} |
2c07c180 |
if (codec->block_align > 0)
aiff->block_duration = av_get_audio_frame_duration(codec,
codec->block_align); |
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) |
99f50907 |
codec->block_align = (av_get_bits_per_sample(codec->codec_id) * codec->channels) >> 3; |
f11288da |
|
02f88eec |
if (aiff->block_duration) {
codec->bit_rate = codec->sample_rate * (codec->block_align << 3) /
aiff->block_duration;
} |
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 */ |
6e9651d1 |
static int aiff_read_header(AVFormatContext *s) |
f11288da |
{ |
cfb1c3c9 |
int ret, size, filesize; |
db2d3a90 |
int64_t offset = 0, position; |
f11288da |
uint32_t tag;
unsigned version = AIFF_C_VERSION1; |
471fe57e |
AVIOContext *pb = s->pb; |
551b41d4 |
AVStream * st; |
24c6f152 |
AIFFInputContext *aiff = s->priv_data; |
8ac3868f |
ID3v2ExtraMeta *id3v2_extra_meta = NULL; |
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;
|
3b3bbdd3 |
st = avformat_new_stream(s, NULL); |
f11288da |
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 */ |
8d1a20aa |
st->nb_frames = get_aiff_header(s, size, version); |
54d8fd20 |
if (st->nb_frames < 0)
return st->nb_frames;
if (offset > 0) // COMM is after SSND
goto got_sound;
break; |
8ac3868f |
case MKTAG('I', 'D', '3', ' '): |
db2d3a90 |
position = avio_tell(pb); |
30cf47c6 |
ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, size); |
cfb1c3c9 |
if (id3v2_extra_meta)
if ((ret = ff_id3v2_parse_apic(s, &id3v2_extra_meta)) < 0) {
ff_id3v2_free_extra_meta(&id3v2_extra_meta);
return ret;
} |
8ac3868f |
ff_id3v2_free_extra_meta(&id3v2_extra_meta); |
db2d3a90 |
if (position + size > avio_tell(pb))
avio_skip(pb, position + size - avio_tell(pb)); |
8ac3868f |
break; |
54d8fd20 |
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 */ |
c68a8a13 |
if (st->codec->block_align && !pb->seekable) /* 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; |
b59740ea |
if (ff_get_extradata(st->codec, pb, size) < 0) |
b8d3daca |
return AVERROR(ENOMEM); |
fde13052 |
if (st->codec->codec_id == AV_CODEC_ID_QDM2 && size>=12*4 && !st->codec->block_align) { |
e367d906 |
st->codec->block_align = AV_RB32(st->codec->extradata+11*4); |
fde13052 |
aiff->block_duration = AV_RB32(st->codec->extradata+9*4); |
9526c9cc |
} else if (st->codec->codec_id == AV_CODEC_ID_QCELP) {
char rate = 0;
if (size >= 25)
rate = st->codec->extradata[24];
switch (rate) {
case 'H': // RATE_HALF
st->codec->block_align = 17;
break;
case 'F': // RATE_FULL
default:
st->codec->block_align = 35;
}
aiff->block_duration = 160;
st->codec->bit_rate = st->codec->sample_rate * (st->codec->block_align << 3) /
aiff->block_duration; |
fde13052 |
} |
b8d3daca |
break; |
8f2e438e |
case MKTAG('C','H','A','N'): |
fa851185 |
if(ff_mov_read_chan(s, pb, st, size) < 0) |
8f2e438e |
return AVERROR_INVALIDDATA;
break; |
54d8fd20 |
default: /* Jump */
if (size & 1) /* Always even aligned */
size++; |
45a8a02a |
avio_skip(pb, size); |
f11288da |
}
}
|
32a659c7 |
got_sound: |
3f60afad |
if (!st->codec->block_align) { |
32a659c7 |
av_log(s, AV_LOG_ERROR, "could not find COMM tag or invalid block_align value\n"); |
3f60afad |
return -1;
} |
f11288da |
/* Now positioned, get the sound data start and end */ |
c3f9ebf7 |
avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); |
f11288da |
st->start_time = 0; |
8d1a20aa |
st->duration = st->nb_frames * aiff->block_duration; |
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 */ |
d1b62a9a |
switch (st->codec->codec_id) {
case AV_CODEC_ID_ADPCM_IMA_QT:
case AV_CODEC_ID_GSM:
case AV_CODEC_ID_QDM2:
case AV_CODEC_ID_QCELP: |
cea65433 |
size = st->codec->block_align; |
d1b62a9a |
break;
default: |
cea65433 |
size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align; |
d1b62a9a |
} |
cea65433 |
size = FFMIN(max_size, size);
res = av_get_packet(s->pb, pkt, size); |
f11288da |
if (res < 0)
return res;
|
7effbee6 |
if (size >= st->codec->block_align)
pkt->flags &= ~AV_PKT_FLAG_CORRUPT; |
f11288da |
/* Only one stream in an AIFF file */
pkt->stream_index = 0; |
8d1a20aa |
pkt->duration = (res / st->codec->block_align) * aiff->block_duration; |
f11288da |
return 0;
}
|
66355be3 |
AVInputFormat ff_aiff_demuxer = { |
dfc2c4d9 |
.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, |
167f3b8d |
.read_seek = ff_pcm_read_seek, |
20234a4b |
.codec_tag = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 }, |
f11288da |
}; |