2006-02-12 02:18:37 +00:00
|
|
|
/*
|
|
|
|
* TTA demuxer
|
|
|
|
* Copyright (c) 2006 Alex Beregszaszi
|
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2006-02-12 02:18:37 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 15:30:46 +00:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-02-12 02:18:37 +00:00
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2006-02-12 02:18:37 +00: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 15:30:46 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2007-07-05 10:40:25 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-02-12 02:18:37 +00:00
|
|
|
*/
|
2008-05-09 11:56:36 +00:00
|
|
|
|
|
|
|
#include "libavcodec/bitstream.h"
|
2006-02-12 02:18:37 +00:00
|
|
|
#include "avformat.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int totalframes, currentframe;
|
|
|
|
} TTAContext;
|
|
|
|
|
|
|
|
static int tta_probe(AVProbeData *p)
|
|
|
|
{
|
|
|
|
const uint8_t *d = p->buf;
|
|
|
|
if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
|
|
|
|
return 80;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
|
|
|
|
{
|
|
|
|
TTAContext *c = s->priv_data;
|
|
|
|
AVStream *st;
|
2007-03-30 22:10:25 +00:00
|
|
|
int i, channels, bps, samplerate, datalen, framelen;
|
2007-04-22 00:50:36 +00:00
|
|
|
uint64_t framepos;
|
2006-02-12 02:18:37 +00:00
|
|
|
|
2007-11-21 07:41:00 +00:00
|
|
|
if (get_le32(s->pb) != ff_get_fourcc("TTA1"))
|
2006-02-12 02:18:37 +00:00
|
|
|
return -1; // not tta file
|
|
|
|
|
2007-11-21 07:41:00 +00:00
|
|
|
url_fskip(s->pb, 2); // FIXME: flags
|
|
|
|
channels = get_le16(s->pb);
|
|
|
|
bps = get_le16(s->pb);
|
|
|
|
samplerate = get_le32(s->pb);
|
2006-05-13 11:37:56 +00:00
|
|
|
if(samplerate <= 0 || samplerate > 1000000){
|
|
|
|
av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-11-21 07:41:00 +00:00
|
|
|
datalen = get_le32(s->pb);
|
2006-05-13 11:37:56 +00:00
|
|
|
if(datalen < 0){
|
|
|
|
av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-11-21 07:41:00 +00:00
|
|
|
url_fskip(s->pb, 4); // header crc
|
2006-02-12 02:18:37 +00:00
|
|
|
|
2007-03-30 21:59:52 +00:00
|
|
|
framelen = samplerate*256/245;
|
2006-02-12 02:18:37 +00:00
|
|
|
c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
|
|
|
|
c->currentframe = 0;
|
|
|
|
|
2006-05-13 11:37:56 +00:00
|
|
|
if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
|
|
|
|
av_log(s, AV_LOG_ERROR, "totalframes too large\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-02-12 02:18:37 +00:00
|
|
|
|
|
|
|
st = av_new_stream(s, 0);
|
|
|
|
if (!st)
|
2007-07-19 15:21:30 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2007-04-22 00:50:36 +00:00
|
|
|
|
|
|
|
av_set_pts_info(st, 64, 1, samplerate);
|
|
|
|
st->start_time = 0;
|
|
|
|
st->duration = datalen;
|
|
|
|
|
2007-11-21 07:41:00 +00:00
|
|
|
framepos = url_ftell(s->pb) + 4*c->totalframes + 4;
|
2007-04-22 00:50:36 +00:00
|
|
|
|
|
|
|
for (i = 0; i < c->totalframes; i++) {
|
2007-11-21 07:41:00 +00:00
|
|
|
uint32_t size = get_le32(s->pb);
|
2007-04-22 00:50:36 +00:00
|
|
|
av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
|
|
|
|
framepos += size;
|
|
|
|
}
|
2007-11-21 07:41:00 +00:00
|
|
|
url_fskip(s->pb, 4); // seektable crc
|
2007-04-22 00:50:36 +00:00
|
|
|
|
2006-02-12 02:18:37 +00:00
|
|
|
st->codec->codec_type = CODEC_TYPE_AUDIO;
|
|
|
|
st->codec->codec_id = CODEC_ID_TTA;
|
|
|
|
st->codec->channels = channels;
|
|
|
|
st->codec->sample_rate = samplerate;
|
|
|
|
st->codec->bits_per_sample = bps;
|
|
|
|
|
2007-11-21 07:41:00 +00:00
|
|
|
st->codec->extradata_size = url_ftell(s->pb);
|
2006-05-13 11:37:56 +00:00
|
|
|
if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
|
|
|
|
//this check is redundant as get_buffer should fail
|
|
|
|
av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-02-13 12:05:06 +00:00
|
|
|
st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
|
2007-11-21 07:41:00 +00:00
|
|
|
url_fseek(s->pb, 0, SEEK_SET);
|
|
|
|
get_buffer(s->pb, st->codec->extradata, st->codec->extradata_size);
|
2006-02-12 02:18:37 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
TTAContext *c = s->priv_data;
|
2007-04-22 00:50:36 +00:00
|
|
|
AVStream *st = s->streams[0];
|
2007-08-11 22:45:40 +00:00
|
|
|
int size, ret;
|
2006-02-12 02:18:37 +00:00
|
|
|
|
|
|
|
// FIXME!
|
|
|
|
if (c->currentframe > c->totalframes)
|
2007-04-22 00:50:36 +00:00
|
|
|
return -1;
|
2006-02-12 02:18:37 +00:00
|
|
|
|
2007-08-11 22:45:40 +00:00
|
|
|
size = st->index_entries[c->currentframe].size;
|
2006-02-12 02:18:37 +00:00
|
|
|
|
2007-11-21 07:41:00 +00:00
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
2007-08-11 22:45:40 +00:00
|
|
|
pkt->dts = st->index_entries[c->currentframe++].timestamp;
|
|
|
|
return ret;
|
2006-02-12 02:18:37 +00:00
|
|
|
}
|
|
|
|
|
2007-04-25 23:22:05 +00:00
|
|
|
static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
|
|
|
|
{
|
|
|
|
TTAContext *c = s->priv_data;
|
|
|
|
AVStream *st = s->streams[stream_index];
|
|
|
|
int index = av_index_search_timestamp(st, timestamp, flags);
|
|
|
|
if (index < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
c->currentframe = index;
|
2007-11-21 07:41:00 +00:00
|
|
|
url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
|
2007-04-25 23:22:05 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-09 23:40:53 +00:00
|
|
|
AVInputFormat tta_demuxer = {
|
2006-02-12 02:18:37 +00:00
|
|
|
"tta",
|
|
|
|
"true-audio",
|
|
|
|
sizeof(TTAContext),
|
|
|
|
tta_probe,
|
|
|
|
tta_read_header,
|
|
|
|
tta_read_packet,
|
2007-04-22 00:50:36 +00:00
|
|
|
NULL,
|
2007-04-25 23:22:05 +00:00
|
|
|
tta_read_seek,
|
2006-02-12 02:18:37 +00:00
|
|
|
.extensions = "tta",
|
|
|
|
};
|