2006-02-12 04:18:37 +02:00
|
|
|
/*
|
|
|
|
* TTA demuxer
|
|
|
|
* Copyright (c) 2006 Alex Beregszaszi
|
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2006-02-12 04:18:37 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-02-12 04:18:37 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2006-02-12 04:18:37 +02: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 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2007-07-05 13:40:25 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-02-12 04:18:37 +02:00
|
|
|
*/
|
2008-05-09 14:56:36 +03:00
|
|
|
|
2016-06-21 15:42:49 +02:00
|
|
|
#include "libavutil/crc.h"
|
2016-04-23 16:44:11 +02:00
|
|
|
#include "libavutil/dict.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
|
2013-06-04 01:52:50 +03:00
|
|
|
#include "apetag.h"
|
2006-02-12 04:18:37 +02:00
|
|
|
#include "avformat.h"
|
2013-05-31 23:27:53 +03:00
|
|
|
#include "avio_internal.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2009-12-16 21:18:48 +02:00
|
|
|
#include "id3v1.h"
|
2006-02-12 04:18:37 +02:00
|
|
|
|
2014-09-22 10:19:33 +03:00
|
|
|
typedef struct TTAContext {
|
2006-02-12 04:18:37 +02:00
|
|
|
int totalframes, currentframe;
|
2012-01-13 21:35:06 +03:00
|
|
|
int frame_size;
|
|
|
|
int last_frame_size;
|
2006-02-12 04:18:37 +02:00
|
|
|
} TTAContext;
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int tta_probe(const AVProbeData *p)
|
2006-02-12 04:18:37 +02:00
|
|
|
{
|
2013-05-28 14:46:10 +03: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 18:12:51 +03:00
|
|
|
return AVPROBE_SCORE_EXTENSION + 30;
|
2006-02-12 04:18:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int tta_read_header(AVFormatContext *s)
|
2006-02-12 04:18:37 +02:00
|
|
|
{
|
|
|
|
TTAContext *c = s->priv_data;
|
|
|
|
AVStream *st;
|
2012-10-17 12:12:18 +03:00
|
|
|
int i, channels, bps, samplerate;
|
2015-02-02 08:21:30 +02:00
|
|
|
int64_t framepos, start_offset;
|
2013-06-07 12:49:04 +03:00
|
|
|
uint32_t nb_samples, crc;
|
2009-12-16 21:18:48 +02:00
|
|
|
|
2013-06-07 13:28:13 +03:00
|
|
|
ff_id3v1_read(s);
|
2006-02-12 04:18:37 +02:00
|
|
|
|
2011-03-03 21:11:45 +02:00
|
|
|
start_offset = avio_tell(s->pb);
|
2015-02-02 08:21:30 +02:00
|
|
|
if (start_offset < 0)
|
|
|
|
return start_offset;
|
2016-08-04 21:17:44 +02:00
|
|
|
ffio_init_checksum(s->pb, ff_crcEDB88320_update, UINT32_MAX);
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_rl32(s->pb) != AV_RL32("TTA1"))
|
2013-06-04 01:26:16 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-02-12 04:18:37 +02:00
|
|
|
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(s->pb, 2); // FIXME: flags
|
2011-02-21 17:43:01 +02:00
|
|
|
channels = avio_rl16(s->pb);
|
|
|
|
bps = avio_rl16(s->pb);
|
|
|
|
samplerate = avio_rl32(s->pb);
|
2006-05-13 14:37:56 +03:00
|
|
|
if(samplerate <= 0 || samplerate > 1000000){
|
|
|
|
av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
|
2013-06-04 01:26:16 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-05-13 14:37:56 +03:00
|
|
|
}
|
|
|
|
|
2013-06-07 12:49:04 +03:00
|
|
|
nb_samples = avio_rl32(s->pb);
|
|
|
|
if (!nb_samples) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "invalid number of samples\n");
|
2012-10-17 12:12:18 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-05-13 14:37:56 +03:00
|
|
|
}
|
|
|
|
|
2013-05-31 23:27:53 +03:00
|
|
|
crc = ffio_get_checksum(s->pb) ^ UINT32_MAX;
|
2015-02-06 04:18:58 +02:00
|
|
|
if (crc != avio_rl32(s->pb) && s->error_recognition & AV_EF_CRCCHECK) {
|
2013-05-31 23:27:53 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Header CRC error\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2006-02-12 04:18:37 +02:00
|
|
|
|
2012-01-13 21:35:06 +03:00
|
|
|
c->frame_size = samplerate * 256 / 245;
|
2013-06-07 12:49:04 +03:00
|
|
|
c->last_frame_size = nb_samples % c->frame_size;
|
2012-01-13 21:35:06 +03:00
|
|
|
if (!c->last_frame_size)
|
|
|
|
c->last_frame_size = c->frame_size;
|
2013-06-07 12:49:04 +03:00
|
|
|
c->totalframes = nb_samples / c->frame_size + (c->last_frame_size < c->frame_size);
|
2006-02-12 04:18:37 +02:00
|
|
|
c->currentframe = 0;
|
|
|
|
|
2011-11-27 17:37:15 +03: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-04 01:26:16 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-05-13 14:37:56 +03:00
|
|
|
}
|
2006-02-12 04:18:37 +02:00
|
|
|
|
2011-06-18 12:43:24 +03:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2006-02-12 04:18:37 +02:00
|
|
|
if (!st)
|
2007-07-19 18:21:30 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2007-04-22 03:50:36 +03:00
|
|
|
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 64, 1, samplerate);
|
2007-04-22 03:50:36 +03:00
|
|
|
st->start_time = 0;
|
2013-06-07 12:49:04 +03:00
|
|
|
st->duration = nb_samples;
|
2007-04-22 03:50:36 +03:00
|
|
|
|
2015-02-02 08:21:30 +02:00
|
|
|
framepos = avio_tell(s->pb);
|
|
|
|
if (framepos < 0)
|
|
|
|
return framepos;
|
|
|
|
framepos += 4 * c->totalframes + 4;
|
2007-04-22 03:50:36 +03:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (ff_alloc_extradata(st->codecpar, avio_tell(s->pb) - start_offset))
|
2013-05-30 22:57:47 +03:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
avio_seek(s->pb, start_offset, SEEK_SET);
|
2016-04-10 21:58:15 +02:00
|
|
|
avio_read(s->pb, st->codecpar->extradata, st->codecpar->extradata_size);
|
2013-05-30 22:57:47 +03:00
|
|
|
|
2016-08-04 21:17:44 +02:00
|
|
|
ffio_init_checksum(s->pb, ff_crcEDB88320_update, UINT32_MAX);
|
2007-04-22 03:50:36 +03:00
|
|
|
for (i = 0; i < c->totalframes; i++) {
|
2011-02-21 17:43:01 +02:00
|
|
|
uint32_t size = avio_rl32(s->pb);
|
2015-02-03 15:41:10 +02: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 03:50:36 +03:00
|
|
|
framepos += size;
|
|
|
|
}
|
2013-05-31 23:27:53 +03:00
|
|
|
crc = ffio_get_checksum(s->pb) ^ UINT32_MAX;
|
2015-02-06 04:18:58 +02:00
|
|
|
if (crc != avio_rl32(s->pb) && s->error_recognition & AV_EF_CRCCHECK) {
|
2013-05-31 23:27:53 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Seek table CRC error\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2007-04-22 03:50:36 +03: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 21:42:52 +03: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 04:18:37 +02:00
|
|
|
|
2017-03-21 22:02:30 +02:00
|
|
|
if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
|
2013-06-16 21:46:35 +03:00
|
|
|
int64_t pos = avio_tell(s->pb);
|
|
|
|
ff_ape_parse_tag(s);
|
|
|
|
avio_seek(s->pb, pos, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
2006-02-12 04:18:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
TTAContext *c = s->priv_data;
|
2007-04-22 03:50:36 +03:00
|
|
|
AVStream *st = s->streams[0];
|
2007-08-12 01:45:40 +03:00
|
|
|
int size, ret;
|
2006-02-12 04:18:37 +02:00
|
|
|
|
|
|
|
// FIXME!
|
2012-01-06 19:30:05 +03:00
|
|
|
if (c->currentframe >= c->totalframes)
|
2012-01-06 19:34:22 +03:00
|
|
|
return AVERROR_EOF;
|
2006-02-12 04:18:37 +02:00
|
|
|
|
2020-10-09 09:22:36 +02:00
|
|
|
if (st->internal->nb_index_entries < c->totalframes) {
|
2015-02-03 15:41:10 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "Index entry disappeared\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2020-10-09 09:22:36 +02:00
|
|
|
size = st->internal->index_entries[c->currentframe].size;
|
2006-02-12 04:18:37 +02:00
|
|
|
|
2007-11-21 09:41:00 +02:00
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
2020-10-09 09:22:36 +02:00
|
|
|
pkt->dts = st->internal->index_entries[c->currentframe++].timestamp;
|
2012-01-13 21:35:06 +03:00
|
|
|
pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
|
|
|
|
c->frame_size;
|
2007-08-12 01:45:40 +03:00
|
|
|
return ret;
|
2006-02-12 04:18:37 +02:00
|
|
|
}
|
|
|
|
|
2007-04-26 02:22:05 +03: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 09:22:36 +02:00
|
|
|
if (avio_seek(s->pb, st->internal->index_entries[index].pos, SEEK_SET) < 0)
|
2011-09-14 20:33:20 +03:00
|
|
|
return -1;
|
2007-04-26 02:22:05 +03:00
|
|
|
|
|
|
|
c->currentframe = index;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVInputFormat ff_tta_demuxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "tta",
|
2012-07-24 04:23:48 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
|
2011-07-16 23:18:12 +03: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 17:50:48 +03:00
|
|
|
.extensions = "tta",
|
2006-02-12 04:18:37 +02:00
|
|
|
};
|