1/*
2 * TTA demuxer
3 * Copyright (c) 2006 Alex Beregszaszi
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include "libavcodec/get_bits.h"
23#include "avformat.h"
24#include "id3v2.h"
25#include "id3v1.h"
26
27typedef struct {
28    int totalframes, currentframe;
29} TTAContext;
30
31static int tta_probe(AVProbeData *p)
32{
33    const uint8_t *d = p->buf;
34
35    if (ff_id3v2_match(d))
36        d += ff_id3v2_tag_len(d);
37
38    if (d - p->buf >= p->buf_size)
39        return 0;
40
41    if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
42        return 80;
43    return 0;
44}
45
46static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
47{
48    TTAContext *c = s->priv_data;
49    AVStream *st;
50    int i, channels, bps, samplerate, datalen, framelen;
51    uint64_t framepos, start_offset;
52
53    ff_id3v2_read(s);
54    if (!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
55        ff_id3v1_read(s);
56
57    start_offset = url_ftell(s->pb);
58    if (get_le32(s->pb) != AV_RL32("TTA1"))
59        return -1; // not tta file
60
61    url_fskip(s->pb, 2); // FIXME: flags
62    channels = get_le16(s->pb);
63    bps = get_le16(s->pb);
64    samplerate = get_le32(s->pb);
65    if(samplerate <= 0 || samplerate > 1000000){
66        av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
67        return -1;
68    }
69
70    datalen = get_le32(s->pb);
71    if(datalen < 0){
72        av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
73        return -1;
74    }
75
76    url_fskip(s->pb, 4); // header crc
77
78    framelen = samplerate*256/245;
79    c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
80    c->currentframe = 0;
81
82    if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
83        av_log(s, AV_LOG_ERROR, "totalframes too large\n");
84        return -1;
85    }
86
87    st = av_new_stream(s, 0);
88    if (!st)
89        return AVERROR(ENOMEM);
90
91    av_set_pts_info(st, 64, 1, samplerate);
92    st->start_time = 0;
93    st->duration = datalen;
94
95    framepos = url_ftell(s->pb) + 4*c->totalframes + 4;
96
97    for (i = 0; i < c->totalframes; i++) {
98        uint32_t size = get_le32(s->pb);
99        av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
100        framepos += size;
101    }
102    url_fskip(s->pb, 4); // seektable crc
103
104    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
105    st->codec->codec_id = CODEC_ID_TTA;
106    st->codec->channels = channels;
107    st->codec->sample_rate = samplerate;
108    st->codec->bits_per_coded_sample = bps;
109
110    st->codec->extradata_size = url_ftell(s->pb) - start_offset;
111    if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
112        //this check is redundant as get_buffer should fail
113        av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
114        return -1;
115    }
116    st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
117    url_fseek(s->pb, start_offset, SEEK_SET);
118    get_buffer(s->pb, st->codec->extradata, st->codec->extradata_size);
119
120    return 0;
121}
122
123static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
124{
125    TTAContext *c = s->priv_data;
126    AVStream *st = s->streams[0];
127    int size, ret;
128
129    // FIXME!
130    if (c->currentframe > c->totalframes)
131        return -1;
132
133    size = st->index_entries[c->currentframe].size;
134
135    ret = av_get_packet(s->pb, pkt, size);
136    pkt->dts = st->index_entries[c->currentframe++].timestamp;
137    return ret;
138}
139
140static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
141{
142    TTAContext *c = s->priv_data;
143    AVStream *st = s->streams[stream_index];
144    int index = av_index_search_timestamp(st, timestamp, flags);
145    if (index < 0)
146        return -1;
147
148    c->currentframe = index;
149    url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
150
151    return 0;
152}
153
154AVInputFormat tta_demuxer = {
155    "tta",
156    NULL_IF_CONFIG_SMALL("True Audio"),
157    sizeof(TTAContext),
158    tta_probe,
159    tta_read_header,
160    tta_read_packet,
161    NULL,
162    tta_read_seek,
163    .extensions = "tta",
164};
165