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 St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "bitstream.h"
25 int totalframes, currentframe;
29 static int tta_probe(AVProbeData *p)
31 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;
43 if (get_le32(&s->pb) != ff_get_fourcc("TTA1"))
44 return -1; // not tta file
46 url_fskip(&s->pb, 2); // FIXME: flags
47 channels = get_le16(&s->pb);
48 bps = get_le16(&s->pb);
49 samplerate = get_le32(&s->pb);
50 if(samplerate <= 0 || samplerate > 1000000){
51 av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
55 datalen = get_le32(&s->pb);
57 av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
61 url_fskip(&s->pb, 4); // header crc
63 framelen = samplerate*256/245;
64 c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
67 if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
68 av_log(s, AV_LOG_ERROR, "totalframes too large\n");
71 c->seektable = av_mallocz(sizeof(uint32_t)*c->totalframes);
75 for (i = 0; i < c->totalframes; i++)
76 c->seektable[i] = get_le32(&s->pb);
77 url_fskip(&s->pb, 4); // seektable crc
79 st = av_new_stream(s, 0);
80 // av_set_pts_info(st, 32, 1, 1000);
83 st->codec->codec_type = CODEC_TYPE_AUDIO;
84 st->codec->codec_id = CODEC_ID_TTA;
85 st->codec->channels = channels;
86 st->codec->sample_rate = samplerate;
87 st->codec->bits_per_sample = bps;
89 st->codec->extradata_size = url_ftell(&s->pb);
90 if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
91 //this check is redundant as get_buffer should fail
92 av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
95 st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
96 url_fseek(&s->pb, 0, SEEK_SET);
97 get_buffer(&s->pb, st->codec->extradata, st->codec->extradata_size);
102 static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
104 TTAContext *c = s->priv_data;
108 if (c->currentframe > c->totalframes)
111 size = c->seektable[c->currentframe];
115 if (av_new_packet(pkt, size) < 0)
118 pkt->pos = url_ftell(&s->pb);
119 pkt->stream_index = 0;
120 ret = get_buffer(&s->pb, pkt->data, size);
126 // av_log(s, AV_LOG_INFO, "TTA packet #%d desired size: %d read size: %d at pos %d\n",
127 // c->currentframe, size, ret, pkt->pos);
131 static int tta_read_close(AVFormatContext *s)
133 TTAContext *c = s->priv_data;
135 av_free(c->seektable);
139 AVInputFormat tta_demuxer = {