aafaeabb |
/*
* TTA demuxer
* Copyright (c) 2006 Alex Beregszaszi
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
aafaeabb |
* 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. |
aafaeabb |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
aafaeabb |
* 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 |
e5a389a1 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
aafaeabb |
*/ |
245976da |
|
9106a698 |
#include "libavcodec/get_bits.h" |
aafaeabb |
#include "avformat.h" |
c3f9ebf7 |
#include "internal.h" |
22ff3361 |
#include "id3v1.h" |
d2d67e42 |
#include "libavutil/dict.h" |
aafaeabb |
typedef struct {
int totalframes, currentframe;
} TTAContext;
static int tta_probe(AVProbeData *p)
{
const uint8_t *d = p->buf; |
22ff3361 |
|
aafaeabb |
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; |
09a62866 |
int i, channels, bps, samplerate, datalen, framelen; |
22ff3361 |
uint64_t framepos, start_offset;
|
d2d67e42 |
if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX)) |
22ff3361 |
ff_id3v1_read(s); |
aafaeabb |
|
a2704c97 |
start_offset = avio_tell(s->pb); |
b7effd4e |
if (avio_rl32(s->pb) != AV_RL32("TTA1")) |
aafaeabb |
return -1; // not tta file
|
45a8a02a |
avio_skip(s->pb, 2); // FIXME: flags |
b7effd4e |
channels = avio_rl16(s->pb);
bps = avio_rl16(s->pb);
samplerate = avio_rl32(s->pb); |
a443a253 |
if(samplerate <= 0 || samplerate > 1000000){
av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
return -1;
}
|
b7effd4e |
datalen = avio_rl32(s->pb); |
a443a253 |
if(datalen < 0){
av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
return -1;
}
|
45a8a02a |
avio_skip(s->pb, 4); // header crc |
aafaeabb |
|
dc417a6e |
framelen = samplerate*256/245; |
aafaeabb |
c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
c->currentframe = 0;
|
b7c7eae7 |
if(c->totalframes >= UINT_MAX/sizeof(uint32_t) || c->totalframes <= 0){
av_log(s, AV_LOG_ERROR, "totalframes %d invalid\n", c->totalframes); |
a443a253 |
return -1;
} |
aafaeabb |
|
3b3bbdd3 |
st = avformat_new_stream(s, NULL); |
aafaeabb |
if (!st) |
769e10f0 |
return AVERROR(ENOMEM); |
756fb7fe |
|
c3f9ebf7 |
avpriv_set_pts_info(st, 64, 1, samplerate); |
756fb7fe |
st->start_time = 0;
st->duration = datalen;
|
a2704c97 |
framepos = avio_tell(s->pb) + 4*c->totalframes + 4; |
756fb7fe |
for (i = 0; i < c->totalframes; i++) { |
b7effd4e |
uint32_t size = avio_rl32(s->pb); |
756fb7fe |
av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
framepos += size;
} |
45a8a02a |
avio_skip(s->pb, 4); // seektable crc |
756fb7fe |
|
72415b2a |
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
aafaeabb |
st->codec->codec_id = CODEC_ID_TTA;
st->codec->channels = channels;
st->codec->sample_rate = samplerate; |
dd1c8f3e |
st->codec->bits_per_coded_sample = bps; |
aafaeabb |
|
a2704c97 |
st->codec->extradata_size = avio_tell(s->pb) - start_offset; |
a443a253 |
if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){ |
b7effd4e |
//this check is redundant as avio_read should fail |
a443a253 |
av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
return -1;
} |
aa11db2f |
st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE); |
f540ca22 |
if (!st->codec->extradata) {
st->codec->extradata_size = 0;
return AVERROR(ENOMEM);
} |
6b4aa5da |
avio_seek(s->pb, start_offset, SEEK_SET); |
b7effd4e |
avio_read(s->pb, st->codec->extradata, st->codec->extradata_size); |
aafaeabb |
return 0;
}
static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
{
TTAContext *c = s->priv_data; |
756fb7fe |
AVStream *st = s->streams[0]; |
160ab30f |
int size, ret; |
aafaeabb |
// FIXME! |
4925b6e7 |
if (c->currentframe >= c->totalframes) |
877f6eb5 |
return AVERROR_EOF; |
aafaeabb |
|
160ab30f |
size = st->index_entries[c->currentframe].size; |
aafaeabb |
|
899681cd |
ret = av_get_packet(s->pb, pkt, size); |
160ab30f |
pkt->dts = st->index_entries[c->currentframe++].timestamp;
return ret; |
aafaeabb |
}
|
70fa2486 |
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; |
d9d7174d |
if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
return -1; |
70fa2486 |
c->currentframe = index;
return 0;
}
|
c6610a21 |
AVInputFormat ff_tta_demuxer = { |
dfc2c4d9 |
.name = "tta",
.long_name = NULL_IF_CONFIG_SMALL("True Audio"),
.priv_data_size = sizeof(TTAContext),
.read_probe = tta_probe,
.read_header = tta_read_header,
.read_packet = tta_read_packet,
.read_seek = tta_read_seek, |
aafaeabb |
.extensions = "tta",
}; |