3 * Copyright (c) 2006 Alex Beregszaszi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "bitstream.h"
23 int totalframes, currentframe;
27 static int tta_probe(AVProbeData *p)
29 const uint8_t *d = p->buf;
32 if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
37 static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
39 TTAContext *c = s->priv_data;
41 int i, channels, bps, samplerate, datalen, framelen, start;
43 start = url_ftell(&s->pb);
45 if (get_le32(&s->pb) != ff_get_fourcc("TTA1"))
46 return -1; // not tta file
48 url_fskip(&s->pb, 2); // FIXME: flags
49 channels = get_le16(&s->pb);
50 bps = get_le16(&s->pb);
51 samplerate = get_le32(&s->pb);
52 if(samplerate <= 0 || samplerate > 1000000){
53 av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
57 datalen = get_le32(&s->pb);
59 av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
63 url_fskip(&s->pb, 4); // header crc
65 framelen = 1.04489795918367346939 * samplerate;
66 c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
69 if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
70 av_log(s, AV_LOG_ERROR, "totalframes too large\n");
73 c->seektable = av_mallocz(sizeof(uint32_t)*c->totalframes);
77 for (i = 0; i < c->totalframes; i++)
78 c->seektable[i] = get_le32(&s->pb);
79 url_fskip(&s->pb, 4); // seektable crc
81 st = av_new_stream(s, 0);
82 // av_set_pts_info(st, 32, 1, 1000);
85 st->codec->codec_type = CODEC_TYPE_AUDIO;
86 st->codec->codec_id = CODEC_ID_TTA;
87 st->codec->channels = channels;
88 st->codec->sample_rate = samplerate;
89 st->codec->bits_per_sample = bps;
91 st->codec->extradata_size = url_ftell(&s->pb) - start;
92 if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
93 //this check is redundant as get_buffer should fail
94 av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
97 st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
98 url_fseek(&s->pb, start, SEEK_SET); // or SEEK_CUR and -size ? :)
99 get_buffer(&s->pb, st->codec->extradata, st->codec->extradata_size);
104 static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
106 TTAContext *c = s->priv_data;
110 if (c->currentframe > c->totalframes)
113 size = c->seektable[c->currentframe];
117 if (av_new_packet(pkt, size) < 0)
120 pkt->pos = url_ftell(&s->pb);
121 pkt->stream_index = 0;
122 ret = get_buffer(&s->pb, pkt->data, size);
128 // av_log(s, AV_LOG_INFO, "TTA packet #%d desired size: %d read size: %d at pos %d\n",
129 // c->currentframe, size, ret, pkt->pos);
133 static int tta_read_close(AVFormatContext *s)
135 TTAContext *c = s->priv_data;
137 av_free(c->seektable);
141 AVInputFormat tta_demuxer = {