* AIFF/AIFF-C demuxer
* Copyright (c) 2006 Patrick Guimond
*
- * This file is part of FFmpeg.
+ * This file is part of Libav.
*
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
* 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
- * License along with FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include "libavutil/intfloat_readwrite.h"
+#include "libavutil/mathematics.h"
+#include "libavutil/dict.h"
#include "avformat.h"
-#include "raw.h"
+#include "internal.h"
+#include "pcm.h"
#include "aiff.h"
#define AIFF 0
}
/* returns the size of the found tag */
-static int get_tag(ByteIOContext *pb, uint32_t * tag)
+static int get_tag(AVIOContext *pb, uint32_t * tag)
{
int size;
- if (url_feof(pb))
+ if (pb->eof_reached)
return AVERROR(EIO);
- *tag = get_le32(pb);
- size = get_be32(pb);
+ *tag = avio_rl32(pb);
+ size = avio_rb32(pb);
if (size < 0)
size = 0x7fffffff;
/* Metadata string read */
static void get_meta(AVFormatContext *s, const char *key, int size)
{
- uint8_t str[1024];
- int res = get_buffer(s->pb, str, FFMIN(sizeof(str)-1, size));
+ uint8_t *str = av_malloc(size+1);
+ int res;
+
+ if (!str) {
+ avio_skip(s->pb, size);
+ return;
+ }
+
+ res = avio_read(s->pb, str, size);
if (res < 0)
return;
str[res] = 0;
- if (size & 1)
- size++;
- size -= res;
- if (size)
- url_fskip(s->pb, size);
-
- av_metadata_set(&s->metadata, key, str);
+ av_dict_set(&s->metadata, key, str, AV_DICT_DONT_STRDUP_VAL);
}
/* Returns the number of sound data frames or negative on error */
-static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
+static unsigned int get_aiff_header(AVIOContext *pb, AVCodecContext *codec,
int size, unsigned version)
{
- AVExtFloat ext;
+ int exp;
+ uint64_t val;
double sample_rate;
unsigned int num_frames;
if (size & 1)
size++;
- codec->codec_type = CODEC_TYPE_AUDIO;
- codec->channels = get_be16(pb);
- num_frames = get_be32(pb);
- codec->bits_per_coded_sample = get_be16(pb);
-
- get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
- sample_rate = av_ext2dbl(ext); /* 80 bits BE IEEE extended float */
+ codec->codec_type = AVMEDIA_TYPE_AUDIO;
+ codec->channels = avio_rb16(pb);
+ num_frames = avio_rb32(pb);
+ codec->bits_per_coded_sample = avio_rb16(pb);
+
+ exp = avio_rb16(pb);
+ val = avio_rb64(pb);
+ sample_rate = ldexp(val, exp - 16383 - 63);
codec->sample_rate = sample_rate;
size -= 18;
/* Got an AIFF-C? */
if (version == AIFF_C_VERSION1) {
- codec->codec_tag = get_le32(pb);
+ codec->codec_tag = avio_rl32(pb);
codec->codec_id = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
switch (codec->codec_id) {
codec->block_align = 33;
codec->frame_size = 160;
break;
+ case CODEC_ID_QCELP:
+ codec->block_align = 35;
+ codec->frame_size= 160;
+ break;
default:
break;
}
/* Chunk is over */
if (size)
- url_fseek(pb, size, SEEK_CUR);
+ avio_skip(pb, size);
return num_frames;
}
}
/* aiff input */
-static int aiff_read_header(AVFormatContext *s,
- AVFormatParameters *ap)
+static int aiff_read_header(AVFormatContext *s)
{
int size, filesize;
int64_t offset = 0;
uint32_t tag;
unsigned version = AIFF_C_VERSION1;
- ByteIOContext *pb = s->pb;
+ AVIOContext *pb = s->pb;
AVStream * st;
AIFFInputContext *aiff = s->priv_data;
return AVERROR_INVALIDDATA;
/* AIFF data type */
- tag = get_le32(pb);
+ tag = avio_rl32(pb);
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 */
filesize -= 4;
- st = av_new_stream(s, 0);
+ st = avformat_new_stream(s, NULL);
if (!st)
return AVERROR(ENOMEM);
goto got_sound;
break;
case MKTAG('F', 'V', 'E', 'R'): /* Version chunk */
- version = get_be32(pb);
+ version = avio_rb32(pb);
break;
case MKTAG('N', 'A', 'M', 'E'): /* Sample name chunk */
get_meta(s, "title" , size);
get_meta(s, "comment" , size);
break;
case MKTAG('S', 'S', 'N', 'D'): /* Sampled sound chunk */
- aiff->data_end = url_ftell(pb) + size;
- offset = get_be32(pb); /* Offset of sound data */
- get_be32(pb); /* BlockSize... don't care */
- offset += url_ftell(pb); /* Compute absolute data offset */
+ aiff->data_end = avio_tell(pb) + size;
+ offset = avio_rb32(pb); /* Offset of sound data */
+ avio_rb32(pb); /* BlockSize... don't care */
+ offset += avio_tell(pb); /* Compute absolute data offset */
if (st->codec->block_align) /* Assume COMM already parsed */
goto got_sound;
- if (url_is_streamed(pb)) {
+ if (!pb->seekable) {
av_log(s, AV_LOG_ERROR, "file is not seekable\n");
return -1;
}
- url_fskip(pb, size - 8);
+ avio_skip(pb, size - 8);
break;
case MKTAG('w', 'a', 'v', 'e'):
if ((uint64_t)size > (1<<30))
if (!st->codec->extradata)
return AVERROR(ENOMEM);
st->codec->extradata_size = size;
- get_buffer(pb, st->codec->extradata, size);
+ avio_read(pb, st->codec->extradata, size);
break;
default: /* Jump */
if (size & 1) /* Always even aligned */
size++;
- url_fskip (pb, size);
+ avio_skip(pb, size);
}
}
+got_sound:
if (!st->codec->block_align) {
- av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
+ av_log(s, AV_LOG_ERROR, "could not find COMM tag or invalid block_align value\n");
return -1;
}
-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);
+ avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
st->start_time = 0;
st->duration = st->codec->frame_size ?
st->nb_frames * st->codec->frame_size : st->nb_frames;
/* Position the stream at the first block */
- url_fseek(pb, offset, SEEK_SET);
+ avio_seek(pb, offset, SEEK_SET);
return 0;
}
AVStream *st = s->streams[0];
AIFFInputContext *aiff = s->priv_data;
int64_t max_size;
- int res;
+ int res, size;
/* calculate size of remaining data */
- max_size = aiff->data_end - url_ftell(s->pb);
+ max_size = aiff->data_end - avio_tell(s->pb);
if (max_size <= 0)
return AVERROR_EOF;
/* Now for that packet */
- max_size = FFMIN(max_size, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
- res = av_get_packet(s->pb, pkt, max_size);
+ 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);
if (res < 0)
return res;
return 0;
}
-AVInputFormat aiff_demuxer = {
- "aiff",
- NULL_IF_CONFIG_SMALL("Audio IFF"),
- sizeof(AIFFInputContext),
- aiff_probe,
- aiff_read_header,
- aiff_read_packet,
- NULL,
- pcm_read_seek,
+AVInputFormat ff_aiff_demuxer = {
+ .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 = ff_pcm_read_seek,
.codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
};