2006-02-12 03:18:37 +01:00
|
|
|
/*
|
|
|
|
* TTA demuxer
|
|
|
|
* Copyright (c) 2006 Alex Beregszaszi
|
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2006-02-12 03:18:37 +01:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 17:30:46 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-02-12 03:18:37 +01:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2006-02-12 03:18:37 +01: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 17:30:46 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2007-07-05 12:40:25 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-02-12 03:18:37 +01:00
|
|
|
*/
|
2008-05-09 13:56:36 +02:00
|
|
|
|
2009-04-13 18:20:26 +02:00
|
|
|
#include "libavcodec/get_bits.h"
|
2006-02-12 03:18:37 +01:00
|
|
|
#include "avformat.h"
|
2011-11-29 19:28:15 +01:00
|
|
|
#include "internal.h"
|
2009-12-16 20:18:48 +01:00
|
|
|
#include "id3v1.h"
|
2011-05-22 12:46:29 +02:00
|
|
|
#include "libavutil/dict.h"
|
2006-02-12 03:18:37 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int totalframes, currentframe;
|
2012-01-13 19:35:06 +01:00
|
|
|
int frame_size;
|
|
|
|
int last_frame_size;
|
2006-02-12 03:18:37 +01:00
|
|
|
} TTAContext;
|
|
|
|
|
|
|
|
static int tta_probe(AVProbeData *p)
|
|
|
|
{
|
|
|
|
const uint8_t *d = p->buf;
|
2009-12-16 20:18:48 +01:00
|
|
|
|
2006-02-12 03:18:37 +01:00
|
|
|
if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
|
|
|
|
return 80;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 13:20:36 +01:00
|
|
|
static int tta_read_header(AVFormatContext *s)
|
2006-02-12 03:18:37 +01:00
|
|
|
{
|
|
|
|
TTAContext *c = s->priv_data;
|
|
|
|
AVStream *st;
|
2012-01-13 19:35:06 +01:00
|
|
|
int i, channels, bps, samplerate, datalen;
|
2009-12-16 20:18:48 +01:00
|
|
|
uint64_t framepos, start_offset;
|
|
|
|
|
2011-05-22 12:46:29 +02:00
|
|
|
if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
|
2009-12-16 20:18:48 +01:00
|
|
|
ff_id3v1_read(s);
|
2006-02-12 03:18:37 +01:00
|
|
|
|
2011-03-03 20:11:45 +01:00
|
|
|
start_offset = avio_tell(s->pb);
|
2011-02-21 16:43:01 +01:00
|
|
|
if (avio_rl32(s->pb) != AV_RL32("TTA1"))
|
2006-02-12 03:18:37 +01:00
|
|
|
return -1; // not tta file
|
|
|
|
|
2011-03-15 09:14:38 +01:00
|
|
|
avio_skip(s->pb, 2); // FIXME: flags
|
2011-02-21 16:43:01 +01:00
|
|
|
channels = avio_rl16(s->pb);
|
|
|
|
bps = avio_rl16(s->pb);
|
|
|
|
samplerate = avio_rl32(s->pb);
|
2006-05-13 13:37:56 +02:00
|
|
|
if(samplerate <= 0 || samplerate > 1000000){
|
|
|
|
av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-02-21 16:43:01 +01:00
|
|
|
datalen = avio_rl32(s->pb);
|
2006-05-13 13:37:56 +02:00
|
|
|
if(datalen < 0){
|
|
|
|
av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-15 09:14:38 +01:00
|
|
|
avio_skip(s->pb, 4); // header crc
|
2006-02-12 03:18:37 +01:00
|
|
|
|
2012-01-13 19:35:06 +01:00
|
|
|
c->frame_size = samplerate * 256 / 245;
|
|
|
|
c->last_frame_size = datalen % c->frame_size;
|
|
|
|
if (!c->last_frame_size)
|
|
|
|
c->last_frame_size = c->frame_size;
|
|
|
|
c->totalframes = datalen / c->frame_size + (c->last_frame_size < c->frame_size);
|
2006-02-12 03:18:37 +01:00
|
|
|
c->currentframe = 0;
|
|
|
|
|
2011-11-27 15:37:15 +01:00
|
|
|
if(c->totalframes >= UINT_MAX/sizeof(uint32_t) || c->totalframes <= 0){
|
|
|
|
av_log(s, AV_LOG_ERROR, "totalframes %d invalid\n", c->totalframes);
|
2006-05-13 13:37:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-02-12 03:18:37 +01:00
|
|
|
|
2011-06-18 11:43:24 +02:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2006-02-12 03:18:37 +01:00
|
|
|
if (!st)
|
2007-07-19 17:21:30 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2007-04-22 02:50:36 +02:00
|
|
|
|
2011-11-29 19:28:15 +01:00
|
|
|
avpriv_set_pts_info(st, 64, 1, samplerate);
|
2007-04-22 02:50:36 +02:00
|
|
|
st->start_time = 0;
|
|
|
|
st->duration = datalen;
|
|
|
|
|
2011-03-03 20:11:45 +01:00
|
|
|
framepos = avio_tell(s->pb) + 4*c->totalframes + 4;
|
2007-04-22 02:50:36 +02:00
|
|
|
|
|
|
|
for (i = 0; i < c->totalframes; i++) {
|
2011-02-21 16:43:01 +01:00
|
|
|
uint32_t size = avio_rl32(s->pb);
|
2012-01-13 19:35:06 +01:00
|
|
|
av_add_index_entry(st, framepos, i * c->frame_size, size, 0,
|
|
|
|
AVINDEX_KEYFRAME);
|
2007-04-22 02:50:36 +02:00
|
|
|
framepos += size;
|
|
|
|
}
|
2011-03-15 09:14:38 +01:00
|
|
|
avio_skip(s->pb, 4); // seektable crc
|
2007-04-22 02:50:36 +02:00
|
|
|
|
2010-03-31 01:30:55 +02:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
|
2012-08-05 11:11:04 +02:00
|
|
|
st->codec->codec_id = AV_CODEC_ID_TTA;
|
2006-02-12 03:18:37 +01:00
|
|
|
st->codec->channels = channels;
|
|
|
|
st->codec->sample_rate = samplerate;
|
2008-09-08 16:24:59 +02:00
|
|
|
st->codec->bits_per_coded_sample = bps;
|
2006-02-12 03:18:37 +01:00
|
|
|
|
2011-03-03 20:11:45 +01:00
|
|
|
st->codec->extradata_size = avio_tell(s->pb) - start_offset;
|
2006-05-13 13:37:56 +02:00
|
|
|
if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
|
2011-02-21 16:43:01 +01:00
|
|
|
//this check is redundant as avio_read should fail
|
2006-05-13 13:37:56 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-02-13 13:05:06 +01:00
|
|
|
st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
|
2011-09-24 01:22:06 +02:00
|
|
|
if (!st->codec->extradata) {
|
|
|
|
st->codec->extradata_size = 0;
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2011-02-28 14:57:54 +01:00
|
|
|
avio_seek(s->pb, start_offset, SEEK_SET);
|
2011-02-21 16:43:01 +01:00
|
|
|
avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
|
2006-02-12 03:18:37 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
TTAContext *c = s->priv_data;
|
2007-04-22 02:50:36 +02:00
|
|
|
AVStream *st = s->streams[0];
|
2007-08-12 00:45:40 +02:00
|
|
|
int size, ret;
|
2006-02-12 03:18:37 +01:00
|
|
|
|
|
|
|
// FIXME!
|
2012-01-06 17:30:05 +01:00
|
|
|
if (c->currentframe >= c->totalframes)
|
2012-01-06 17:34:22 +01:00
|
|
|
return AVERROR_EOF;
|
2006-02-12 03:18:37 +01:00
|
|
|
|
2007-08-12 00:45:40 +02:00
|
|
|
size = st->index_entries[c->currentframe].size;
|
2006-02-12 03:18:37 +01:00
|
|
|
|
2007-11-21 08:41:00 +01:00
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
2007-08-12 00:45:40 +02:00
|
|
|
pkt->dts = st->index_entries[c->currentframe++].timestamp;
|
2012-01-13 19:35:06 +01:00
|
|
|
pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
|
|
|
|
c->frame_size;
|
2007-08-12 00:45:40 +02:00
|
|
|
return ret;
|
2006-02-12 03:18:37 +01:00
|
|
|
}
|
|
|
|
|
2007-04-26 01:22:05 +02: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;
|
2011-09-14 19:33:20 +02:00
|
|
|
if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
|
|
|
|
return -1;
|
2007-04-26 01:22:05 +02:00
|
|
|
|
|
|
|
c->currentframe = index;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 23:03:28 +01:00
|
|
|
AVInputFormat ff_tta_demuxer = {
|
2011-07-16 22:18:12 +02:00
|
|
|
.name = "tta",
|
2012-07-24 03:23:48 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
|
2011-07-16 22:18:12 +02:00
|
|
|
.priv_data_size = sizeof(TTAContext),
|
|
|
|
.read_probe = tta_probe,
|
|
|
|
.read_header = tta_read_header,
|
|
|
|
.read_packet = tta_read_packet,
|
|
|
|
.read_seek = tta_read_seek,
|
2012-04-06 16:50:48 +02:00
|
|
|
.extensions = "tta",
|
2006-02-12 03:18:37 +01:00
|
|
|
};
|