3 * Copyright (c) 2006 Alex Beregszaszi
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavcodec/get_bits.h"
26 #include "libavutil/dict.h"
29 int totalframes, currentframe;
32 static int tta_probe(AVProbeData *p)
34 const uint8_t *d = p->buf;
36 if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
41 static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
43 TTAContext *c = s->priv_data;
45 int i, channels, bps, samplerate, datalen, framelen;
46 uint64_t framepos, start_offset;
48 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
51 start_offset = avio_tell(s->pb);
52 if (avio_rl32(s->pb) != AV_RL32("TTA1"))
53 return -1; // not tta file
55 avio_skip(s->pb, 2); // FIXME: flags
56 channels = avio_rl16(s->pb);
57 bps = avio_rl16(s->pb);
58 samplerate = avio_rl32(s->pb);
59 if(samplerate <= 0 || samplerate > 1000000){
60 av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
64 datalen = avio_rl32(s->pb);
66 av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
70 avio_skip(s->pb, 4); // header crc
72 framelen = samplerate*256/245;
73 c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
76 if(c->totalframes >= UINT_MAX/sizeof(uint32_t) || c->totalframes <= 0){
77 av_log(s, AV_LOG_ERROR, "totalframes %d invalid\n", c->totalframes);
81 st = avformat_new_stream(s, NULL);
83 return AVERROR(ENOMEM);
85 avpriv_set_pts_info(st, 64, 1, samplerate);
87 st->duration = datalen;
89 framepos = avio_tell(s->pb) + 4*c->totalframes + 4;
91 for (i = 0; i < c->totalframes; i++) {
92 uint32_t size = avio_rl32(s->pb);
93 av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
96 avio_skip(s->pb, 4); // seektable crc
98 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
99 st->codec->codec_id = CODEC_ID_TTA;
100 st->codec->channels = channels;
101 st->codec->sample_rate = samplerate;
102 st->codec->bits_per_coded_sample = bps;
104 st->codec->extradata_size = avio_tell(s->pb) - start_offset;
105 if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
106 //this check is redundant as avio_read should fail
107 av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
110 st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
111 if (!st->codec->extradata) {
112 st->codec->extradata_size = 0;
113 return AVERROR(ENOMEM);
115 avio_seek(s->pb, start_offset, SEEK_SET);
116 avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
121 static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
123 TTAContext *c = s->priv_data;
124 AVStream *st = s->streams[0];
128 if (c->currentframe > c->totalframes)
131 size = st->index_entries[c->currentframe].size;
133 ret = av_get_packet(s->pb, pkt, size);
134 pkt->dts = st->index_entries[c->currentframe++].timestamp;
138 static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
140 TTAContext *c = s->priv_data;
141 AVStream *st = s->streams[stream_index];
142 int index = av_index_search_timestamp(st, timestamp, flags);
145 if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
148 c->currentframe = index;
153 AVInputFormat ff_tta_demuxer = {
155 .long_name = NULL_IF_CONFIG_SMALL("True Audio"),
156 .priv_data_size = sizeof(TTAContext),
157 .read_probe = tta_probe,
158 .read_header = tta_read_header,
159 .read_packet = tta_read_packet,
160 .read_seek = tta_read_seek,