3 * Copyright (c) 2006 Alex Beregszaszi
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/dict.h"
23 #include "libavutil/intreadwrite.h"
29 typedef struct TTAContext {
30 int totalframes, currentframe;
35 static int tta_probe(AVProbeData *p)
37 const uint8_t *d = p->buf;
39 if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
40 return AVPROBE_SCORE_EXTENSION + 30;
44 static int tta_read_header(AVFormatContext *s)
46 TTAContext *c = s->priv_data;
48 int i, channels, bps, samplerate, datalen;
49 int64_t framepos, start_offset;
51 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
54 start_offset = avio_tell(s->pb);
57 if (avio_rl32(s->pb) != AV_RL32("TTA1"))
58 return -1; // not tta file
60 avio_skip(s->pb, 2); // FIXME: flags
61 channels = avio_rl16(s->pb);
62 bps = avio_rl16(s->pb);
63 samplerate = avio_rl32(s->pb);
64 if(samplerate <= 0 || samplerate > 1000000){
65 av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
69 datalen = avio_rl32(s->pb);
71 av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
75 avio_skip(s->pb, 4); // header crc
77 c->frame_size = samplerate * 256 / 245;
78 c->last_frame_size = datalen % c->frame_size;
79 if (!c->last_frame_size)
80 c->last_frame_size = c->frame_size;
81 c->totalframes = datalen / c->frame_size + (c->last_frame_size < c->frame_size);
84 if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
85 av_log(s, AV_LOG_ERROR, "totalframes too large\n");
89 st = avformat_new_stream(s, NULL);
91 return AVERROR(ENOMEM);
93 avpriv_set_pts_info(st, 64, 1, samplerate);
95 st->duration = datalen;
97 framepos = avio_tell(s->pb);
100 framepos += 4 * c->totalframes + 4;
102 for (i = 0; i < c->totalframes; i++) {
103 uint32_t size = avio_rl32(s->pb);
104 av_add_index_entry(st, framepos, i * c->frame_size, size, 0,
108 avio_skip(s->pb, 4); // seektable crc
110 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
111 st->codecpar->codec_id = AV_CODEC_ID_TTA;
112 st->codecpar->channels = channels;
113 st->codecpar->sample_rate = samplerate;
114 st->codecpar->bits_per_coded_sample = bps;
116 st->codecpar->extradata_size = avio_tell(s->pb) - start_offset;
117 if (st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codecpar->extradata_size) {
118 //this check is redundant as avio_read should fail
119 av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
122 st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
123 if (!st->codecpar->extradata) {
124 st->codecpar->extradata_size = 0;
125 return AVERROR(ENOMEM);
127 avio_seek(s->pb, start_offset, SEEK_SET);
128 avio_read(s->pb, st->codecpar->extradata, st->codecpar->extradata_size);
133 static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
135 TTAContext *c = s->priv_data;
136 AVStream *st = s->streams[0];
140 if (c->currentframe >= c->totalframes)
143 size = st->index_entries[c->currentframe].size;
145 ret = av_get_packet(s->pb, pkt, size);
146 pkt->dts = st->index_entries[c->currentframe++].timestamp;
147 pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
152 static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
154 TTAContext *c = s->priv_data;
155 AVStream *st = s->streams[stream_index];
156 int index = av_index_search_timestamp(st, timestamp, flags);
159 if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
162 c->currentframe = index;
167 AVInputFormat ff_tta_demuxer = {
169 .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
170 .priv_data_size = sizeof(TTAContext),
171 .read_probe = tta_probe,
172 .read_header = tta_read_header,
173 .read_packet = tta_read_packet,
174 .read_seek = tta_read_seek,