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 |
|
9204a849 |
#include "libavutil/crc.h" |
5c31eaa9 |
#include "libavutil/dict.h"
#include "libavutil/intreadwrite.h"
|
55121f3f |
#include "apetag.h" |
aafaeabb |
#include "avformat.h" |
6510686c |
#include "avio_internal.h" |
c3f9ebf7 |
#include "internal.h" |
22ff3361 |
#include "id3v1.h" |
aafaeabb |
|
daf8cf35 |
typedef struct TTAContext { |
aafaeabb |
int totalframes, currentframe; |
101c369b |
int frame_size;
int last_frame_size; |
aafaeabb |
} TTAContext;
|
4d8875ec |
static int tta_probe(const AVProbeData *p) |
aafaeabb |
{ |
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; |
ec524ed1 |
int64_t framepos, start_offset; |
5bafe0ce |
uint32_t nb_samples, crc; |
22ff3361 |
|
369684f1 |
ff_id3v1_read(s); |
aafaeabb |
|
a2704c97 |
start_offset = avio_tell(s->pb); |
ec524ed1 |
if (start_offset < 0)
return start_offset; |
ce944e84 |
ffio_init_checksum(s->pb, ff_crcEDB88320_update, 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; |
15a88468 |
if (crc != avio_rl32(s->pb) && s->error_recognition & AV_EF_CRCCHECK) { |
6510686c |
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 |
|
ec524ed1 |
framepos = avio_tell(s->pb);
if (framepos < 0)
return framepos;
framepos += 4 * c->totalframes + 4; |
756fb7fe |
|
6f69f7a8 |
if (ff_alloc_extradata(st->codecpar, avio_tell(s->pb) - start_offset)) |
c4e0e314 |
return AVERROR(ENOMEM);
avio_seek(s->pb, start_offset, SEEK_SET); |
6f69f7a8 |
avio_read(s->pb, st->codecpar->extradata, st->codecpar->extradata_size); |
c4e0e314 |
|
ce944e84 |
ffio_init_checksum(s->pb, ff_crcEDB88320_update, UINT32_MAX); |
756fb7fe |
for (i = 0; i < c->totalframes; i++) { |
b7effd4e |
uint32_t size = avio_rl32(s->pb); |
6a0cd529 |
int r;
if ((r = av_add_index_entry(st, framepos, i * c->frame_size, size, 0,
AVINDEX_KEYFRAME)) < 0)
return r; |
756fb7fe |
framepos += size;
} |
6510686c |
crc = ffio_get_checksum(s->pb) ^ UINT32_MAX; |
15a88468 |
if (crc != avio_rl32(s->pb) && s->error_recognition & AV_EF_CRCCHECK) { |
6510686c |
av_log(s, AV_LOG_ERROR, "Seek table CRC error\n");
return AVERROR_INVALIDDATA;
} |
756fb7fe |
|
9200514a |
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->codec_id = AV_CODEC_ID_TTA;
st->codecpar->channels = channels;
st->codecpar->sample_rate = samplerate;
st->codecpar->bits_per_coded_sample = bps; |
aafaeabb |
|
4de591e6 |
if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) { |
9ea7ff79 |
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 |
|
6a0cd529 |
if (st->nb_index_entries < c->totalframes) {
av_log(s, AV_LOG_ERROR, "Index entry disappeared\n");
return AVERROR_INVALIDDATA;
}
|
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 |
}; |