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" |
55121f3f |
#include "apetag.h" |
aafaeabb |
#include "avformat.h" |
6510686c |
#include "avio_internal.h" |
c3f9ebf7 |
#include "internal.h" |
22ff3361 |
#include "id3v1.h" |
6510686c |
#include "libavutil/crc.h" |
d2d67e42 |
#include "libavutil/dict.h" |
aafaeabb |
typedef struct {
int totalframes, currentframe; |
101c369b |
int frame_size;
int last_frame_size; |
aafaeabb |
} TTAContext;
|
6510686c |
static unsigned long tta_check_crc(unsigned long checksum, const uint8_t *buf,
unsigned int len)
{
return av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), checksum, buf, len);
}
|
aafaeabb |
static int tta_probe(AVProbeData *p)
{ |
ced0307e |
if (AV_RL32(&p->buf[0]) == MKTAG('T', 'T', 'A', '1') &&
(AV_RL16(&p->buf[4]) == 1 || AV_RL16(&p->buf[4]) == 2) &&
AV_RL16(&p->buf[6]) > 0 &&
AV_RL16(&p->buf[8]) > 0 &&
AV_RL32(&p->buf[10]) > 0) |
e0f8be64 |
return AVPROBE_SCORE_EXTENSION + 30; |
aafaeabb |
return 0;
}
|
6e9651d1 |
static int tta_read_header(AVFormatContext *s) |
aafaeabb |
{
TTAContext *c = s->priv_data;
AVStream *st; |
d6ea59b8 |
int i, channels, bps, samplerate; |
22ff3361 |
uint64_t framepos, start_offset; |
5bafe0ce |
uint32_t nb_samples, crc; |
22ff3361 |
|
369684f1 |
ff_id3v1_read(s); |
aafaeabb |
|
a2704c97 |
start_offset = avio_tell(s->pb); |
6510686c |
ffio_init_checksum(s->pb, tta_check_crc, UINT32_MAX); |
b7effd4e |
if (avio_rl32(s->pb) != AV_RL32("TTA1")) |
0e84ba74 |
return AVERROR_INVALIDDATA; |
aafaeabb |
|
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"); |
0e84ba74 |
return AVERROR_INVALIDDATA; |
a443a253 |
}
|
5bafe0ce |
nb_samples = avio_rl32(s->pb);
if (!nb_samples) {
av_log(s, AV_LOG_ERROR, "invalid number of samples\n"); |
d6ea59b8 |
return AVERROR_INVALIDDATA; |
a443a253 |
}
|
6510686c |
crc = ffio_get_checksum(s->pb) ^ UINT32_MAX;
if (crc != avio_rl32(s->pb)) {
av_log(s, AV_LOG_ERROR, "Header CRC error\n");
return AVERROR_INVALIDDATA;
} |
aafaeabb |
|
101c369b |
c->frame_size = samplerate * 256 / 245; |
5bafe0ce |
c->last_frame_size = nb_samples % c->frame_size; |
101c369b |
if (!c->last_frame_size)
c->last_frame_size = c->frame_size; |
5bafe0ce |
c->totalframes = nb_samples / c->frame_size + (c->last_frame_size < c->frame_size); |
aafaeabb |
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); |
0e84ba74 |
return AVERROR_INVALIDDATA; |
a443a253 |
} |
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; |
5bafe0ce |
st->duration = nb_samples; |
756fb7fe |
|
a2704c97 |
framepos = avio_tell(s->pb) + 4*c->totalframes + 4; |
756fb7fe |
|
a807c682 |
if (ff_alloc_extradata(st->codec, avio_tell(s->pb) - start_offset)) |
c4e0e314 |
return AVERROR(ENOMEM);
avio_seek(s->pb, start_offset, SEEK_SET);
avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
|
6510686c |
ffio_init_checksum(s->pb, tta_check_crc, UINT32_MAX); |
756fb7fe |
for (i = 0; i < c->totalframes; i++) { |
b7effd4e |
uint32_t size = avio_rl32(s->pb); |
101c369b |
av_add_index_entry(st, framepos, i * c->frame_size, size, 0,
AVINDEX_KEYFRAME); |
756fb7fe |
framepos += size;
} |
6510686c |
crc = ffio_get_checksum(s->pb) ^ UINT32_MAX;
if (crc != avio_rl32(s->pb)) {
av_log(s, AV_LOG_ERROR, "Seek table CRC error\n");
return AVERROR_INVALIDDATA;
} |
756fb7fe |
|
72415b2a |
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
36ef5369 |
st->codec->codec_id = AV_CODEC_ID_TTA; |
aafaeabb |
st->codec->channels = channels;
st->codec->sample_rate = samplerate; |
dd1c8f3e |
st->codec->bits_per_coded_sample = bps; |
aafaeabb |
|
9ea7ff79 |
if (s->pb->seekable) {
int64_t pos = avio_tell(s->pb);
ff_ape_parse_tag(s);
avio_seek(s->pb, pos, SEEK_SET);
}
|
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; |
101c369b |
pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
c->frame_size; |
160ab30f |
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", |
0177b7d2 |
.long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"), |
dfc2c4d9 |
.priv_data_size = sizeof(TTAContext),
.read_probe = tta_probe,
.read_header = tta_read_header,
.read_packet = tta_read_packet,
.read_seek = tta_read_seek, |
20234a4b |
.extensions = "tta", |
aafaeabb |
}; |