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
|
|
|
|
2016-06-21 13:42:49 +00:00
|
|
|
#include "libavutil/crc.h"
|
2016-04-23 14:44:11 +00:00
|
|
|
#include "libavutil/dict.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
|
2013-06-03 22:52:50 +00:00
|
|
|
#include "apetag.h"
|
2006-02-12 02:18:37 +00:00
|
|
|
#include "avformat.h"
|
2013-05-31 20:27:53 +00:00
|
|
|
#include "avio_internal.h"
|
2011-11-29 18:28:15 +00:00
|
|
|
#include "internal.h"
|
2009-12-16 19:18:48 +00:00
|
|
|
#include "id3v1.h"
|
2006-02-12 02:18:37 +00:00
|
|
|
|
2014-09-22 07:19:33 +00:00
|
|
|
typedef struct TTAContext {
|
2006-02-12 02:18:37 +00:00
|
|
|
int totalframes, currentframe;
|
2012-01-13 18:35:06 +00:00
|
|
|
int frame_size;
|
|
|
|
int last_frame_size;
|
2006-02-12 02:18:37 +00:00
|
|
|
} TTAContext;
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int tta_probe(const AVProbeData *p)
|
2006-02-12 02:18:37 +00:00
|
|
|
{
|
2013-05-28 11:46:10 +00:00
|
|
|
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)
|
2013-03-25 15:12:51 +00:00
|
|
|
return AVPROBE_SCORE_EXTENSION + 30;
|
2006-02-12 02:18:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 12:20:36 +00:00
|
|
|
static int tta_read_header(AVFormatContext *s)
|
2006-02-12 02:18:37 +00:00
|
|
|
{
|
|
|
|
TTAContext *c = s->priv_data;
|
|
|
|
AVStream *st;
|
2012-10-17 09:12:18 +00:00
|
|
|
int i, channels, bps, samplerate;
|
2015-02-02 06:21:30 +00:00
|
|
|
int64_t framepos, start_offset;
|
2013-06-07 09:49:04 +00:00
|
|
|
uint32_t nb_samples, crc;
|
2009-12-16 19:18:48 +00:00
|
|
|
|
2013-06-07 10:28:13 +00:00
|
|
|
ff_id3v1_read(s);
|
2006-02-12 02:18:37 +00:00
|
|
|
|
2011-03-03 19:11:45 +00:00
|
|
|
start_offset = avio_tell(s->pb);
|
2015-02-02 06:21:30 +00:00
|
|
|
if (start_offset < 0)
|
|
|
|
return start_offset;
|
2016-08-04 19:17:44 +00:00
|
|
|
ffio_init_checksum(s->pb, ff_crcEDB88320_update, UINT32_MAX);
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_rl32(s->pb) != AV_RL32("TTA1"))
|
2013-06-03 22:26:16 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-02-12 02:18:37 +00:00
|
|
|
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(s->pb, 2); // FIXME: flags
|
2011-02-21 15:43:01 +00:00
|
|
|
channels = avio_rl16(s->pb);
|
|
|
|
bps = avio_rl16(s->pb);
|
|
|
|
samplerate = avio_rl32(s->pb);
|
2006-05-13 11:37:56 +00:00
|
|
|
if(samplerate <= 0 || samplerate > 1000000){
|
|
|
|
av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
|
2013-06-03 22:26:16 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-05-13 11:37:56 +00:00
|
|
|
}
|
|
|
|
|
2013-06-07 09:49:04 +00:00
|
|
|
nb_samples = avio_rl32(s->pb);
|
|
|
|
if (!nb_samples) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "invalid number of samples\n");
|
2012-10-17 09:12:18 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-05-13 11:37:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 20:27:53 +00:00
|
|
|
crc = ffio_get_checksum(s->pb) ^ UINT32_MAX;
|
2015-02-06 02:18:58 +00:00
|
|
|
if (crc != avio_rl32(s->pb) && s->error_recognition & AV_EF_CRCCHECK) {
|
2013-05-31 20:27:53 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Header CRC error\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2006-02-12 02:18:37 +00:00
|
|
|
|
2012-01-13 18:35:06 +00:00
|
|
|
c->frame_size = samplerate * 256 / 245;
|
2013-06-07 09:49:04 +00:00
|
|
|
c->last_frame_size = nb_samples % c->frame_size;
|
2012-01-13 18:35:06 +00:00
|
|
|
if (!c->last_frame_size)
|
|
|
|
c->last_frame_size = c->frame_size;
|
2013-06-07 09:49:04 +00:00
|
|
|
c->totalframes = nb_samples / c->frame_size + (c->last_frame_size < c->frame_size);
|
2006-02-12 02:18:37 +00:00
|
|
|
c->currentframe = 0;
|
|
|
|
|
2011-11-27 14:37:15 +00:00
|
|
|
if(c->totalframes >= UINT_MAX/sizeof(uint32_t) || c->totalframes <= 0){
|
|
|
|
av_log(s, AV_LOG_ERROR, "totalframes %d invalid\n", c->totalframes);
|
2013-06-03 22:26:16 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-05-13 11:37:56 +00:00
|
|
|
}
|
2006-02-12 02:18:37 +00:00
|
|
|
|
2011-06-18 09:43:24 +00:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2006-02-12 02:18:37 +00:00
|
|
|
if (!st)
|
2007-07-19 15:21:30 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2007-04-22 00:50:36 +00:00
|
|
|
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(st, 64, 1, samplerate);
|
2007-04-22 00:50:36 +00:00
|
|
|
st->start_time = 0;
|
2013-06-07 09:49:04 +00:00
|
|
|
st->duration = nb_samples;
|
2007-04-22 00:50:36 +00:00
|
|
|
|
2015-02-02 06:21:30 +00:00
|
|
|
framepos = avio_tell(s->pb);
|
|
|
|
if (framepos < 0)
|
|
|
|
return framepos;
|
|
|
|
framepos += 4 * c->totalframes + 4;
|
2007-04-22 00:50:36 +00:00
|
|
|
|
2016-04-10 19:58:15 +00:00
|
|
|
if (ff_alloc_extradata(st->codecpar, avio_tell(s->pb) - start_offset))
|
2013-05-30 19:57:47 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
avio_seek(s->pb, start_offset, SEEK_SET);
|
2016-04-10 19:58:15 +00:00
|
|
|
avio_read(s->pb, st->codecpar->extradata, st->codecpar->extradata_size);
|
2013-05-30 19:57:47 +00:00
|
|
|
|
2016-08-04 19:17:44 +00:00
|
|
|
ffio_init_checksum(s->pb, ff_crcEDB88320_update, UINT32_MAX);
|
2007-04-22 00:50:36 +00:00
|
|
|
for (i = 0; i < c->totalframes; i++) {
|
2011-02-21 15:43:01 +00:00
|
|
|
uint32_t size = avio_rl32(s->pb);
|
2015-02-03 13:41:10 +00:00
|
|
|
int r;
|
|
|
|
if ((r = av_add_index_entry(st, framepos, i * c->frame_size, size, 0,
|
|
|
|
AVINDEX_KEYFRAME)) < 0)
|
|
|
|
return r;
|
2007-04-22 00:50:36 +00:00
|
|
|
framepos += size;
|
|
|
|
}
|
2013-05-31 20:27:53 +00:00
|
|
|
crc = ffio_get_checksum(s->pb) ^ UINT32_MAX;
|
2015-02-06 02:18:58 +00:00
|
|
|
if (crc != avio_rl32(s->pb) && s->error_recognition & AV_EF_CRCCHECK) {
|
2013-05-31 20:27:53 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Seek table CRC error\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2007-04-22 00:50:36 +00:00
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
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;
|
2006-02-12 02:18:37 +00:00
|
|
|
|
2017-03-21 20:02:30 +00:00
|
|
|
if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
|
2013-06-16 18:46:35 +00:00
|
|
|
int64_t pos = avio_tell(s->pb);
|
|
|
|
ff_ape_parse_tag(s);
|
|
|
|
avio_seek(s->pb, pos, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
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!
|
2012-01-06 16:30:05 +00:00
|
|
|
if (c->currentframe >= c->totalframes)
|
2012-01-06 16:34:22 +00:00
|
|
|
return AVERROR_EOF;
|
2006-02-12 02:18:37 +00:00
|
|
|
|
2020-10-09 07:22:36 +00:00
|
|
|
if (st->internal->nb_index_entries < c->totalframes) {
|
2015-02-03 13:41:10 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Index entry disappeared\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2020-10-09 07:22:36 +00:00
|
|
|
size = st->internal->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);
|
2020-10-09 07:22:36 +00:00
|
|
|
pkt->dts = st->internal->index_entries[c->currentframe++].timestamp;
|
2012-01-13 18:35:06 +00:00
|
|
|
pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
|
|
|
|
c->frame_size;
|
2007-08-11 22:45:40 +00:00
|
|
|
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;
|
2020-10-09 07:22:36 +00:00
|
|
|
if (avio_seek(s->pb, st->internal->index_entries[index].pos, SEEK_SET) < 0)
|
2011-09-14 17:33:20 +00:00
|
|
|
return -1;
|
2007-04-25 23:22:05 +00:00
|
|
|
|
|
|
|
c->currentframe = index;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:03:28 +00:00
|
|
|
AVInputFormat ff_tta_demuxer = {
|
2011-07-16 20:18:12 +00:00
|
|
|
.name = "tta",
|
2012-07-24 01:23:48 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
|
2011-07-16 20:18:12 +00: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 14:50:48 +00:00
|
|
|
.extensions = "tta",
|
2006-02-12 02:18:37 +00:00
|
|
|
};
|