1 | /*
|
---|
2 | * TTA demuxer
|
---|
3 | * Copyright (c) 2006 Alex Beregszaszi
|
---|
4 | *
|
---|
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.
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
18 | */
|
---|
19 | #include "avformat.h"
|
---|
20 | #define ALT_BITSREAM_READER_LE
|
---|
21 | #include "bitstream.h"
|
---|
22 |
|
---|
23 | typedef struct {
|
---|
24 | int totalframes, currentframe;
|
---|
25 | uint32_t *seektable;
|
---|
26 | } TTAContext;
|
---|
27 |
|
---|
28 | static int tta_probe(AVProbeData *p)
|
---|
29 | {
|
---|
30 | const uint8_t *d = p->buf;
|
---|
31 | if (p->buf_size < 4)
|
---|
32 | return 0;
|
---|
33 | if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
|
---|
34 | return 80;
|
---|
35 | return 0;
|
---|
36 | }
|
---|
37 |
|
---|
38 | static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
|
---|
39 | {
|
---|
40 | TTAContext *c = s->priv_data;
|
---|
41 | AVStream *st;
|
---|
42 | int i, channels, bps, samplerate, datalen, framelen, start;
|
---|
43 |
|
---|
44 | start = url_ftell(&s->pb);
|
---|
45 |
|
---|
46 | if (get_le32(&s->pb) != ff_get_fourcc("TTA1"))
|
---|
47 | return -1; // not tta file
|
---|
48 |
|
---|
49 | url_fskip(&s->pb, 2); // FIXME: flags
|
---|
50 | channels = get_le16(&s->pb);
|
---|
51 | bps = get_le16(&s->pb);
|
---|
52 | samplerate = get_le32(&s->pb);
|
---|
53 | if(samplerate <= 0 || samplerate > 1000000){
|
---|
54 | av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
|
---|
55 | return -1;
|
---|
56 | }
|
---|
57 |
|
---|
58 | datalen = get_le32(&s->pb);
|
---|
59 | if(datalen < 0){
|
---|
60 | av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
|
---|
61 | return -1;
|
---|
62 | }
|
---|
63 |
|
---|
64 | url_fskip(&s->pb, 4); // header crc
|
---|
65 |
|
---|
66 | framelen = 1.04489795918367346939 * samplerate;
|
---|
67 | c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
|
---|
68 | c->currentframe = 0;
|
---|
69 |
|
---|
70 | if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
|
---|
71 | av_log(s, AV_LOG_ERROR, "totalframes too large\n");
|
---|
72 | return -1;
|
---|
73 | }
|
---|
74 | c->seektable = av_mallocz(sizeof(uint32_t)*c->totalframes);
|
---|
75 | if (!c->seektable)
|
---|
76 | return AVERROR_NOMEM;
|
---|
77 |
|
---|
78 | for (i = 0; i < c->totalframes; i++)
|
---|
79 | c->seektable[i] = get_le32(&s->pb);
|
---|
80 | url_fskip(&s->pb, 4); // seektable crc
|
---|
81 |
|
---|
82 | st = av_new_stream(s, 0);
|
---|
83 | // av_set_pts_info(st, 32, 1, 1000);
|
---|
84 | if (!st)
|
---|
85 | return AVERROR_NOMEM;
|
---|
86 | st->codec->codec_type = CODEC_TYPE_AUDIO;
|
---|
87 | st->codec->codec_id = CODEC_ID_TTA;
|
---|
88 | st->codec->channels = channels;
|
---|
89 | st->codec->sample_rate = samplerate;
|
---|
90 | st->codec->bits_per_sample = bps;
|
---|
91 |
|
---|
92 | st->codec->extradata_size = url_ftell(&s->pb) - start;
|
---|
93 | if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
|
---|
94 | //this check is redundant as get_buffer should fail
|
---|
95 | av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
|
---|
96 | return -1;
|
---|
97 | }
|
---|
98 | st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
|
---|
99 | url_fseek(&s->pb, start, SEEK_SET); // or SEEK_CUR and -size ? :)
|
---|
100 | get_buffer(&s->pb, st->codec->extradata, st->codec->extradata_size);
|
---|
101 |
|
---|
102 | return 0;
|
---|
103 | }
|
---|
104 |
|
---|
105 | static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
|
---|
106 | {
|
---|
107 | TTAContext *c = s->priv_data;
|
---|
108 | int ret, size;
|
---|
109 |
|
---|
110 | // FIXME!
|
---|
111 | if (c->currentframe > c->totalframes)
|
---|
112 | size = 0;
|
---|
113 | else
|
---|
114 | size = c->seektable[c->currentframe];
|
---|
115 |
|
---|
116 | c->currentframe++;
|
---|
117 |
|
---|
118 | if (av_new_packet(pkt, size) < 0)
|
---|
119 | return AVERROR_IO;
|
---|
120 |
|
---|
121 | pkt->pos = url_ftell(&s->pb);
|
---|
122 | pkt->stream_index = 0;
|
---|
123 | ret = get_buffer(&s->pb, pkt->data, size);
|
---|
124 | if (ret <= 0) {
|
---|
125 | av_free_packet(pkt);
|
---|
126 | return AVERROR_IO;
|
---|
127 | }
|
---|
128 | pkt->size = ret;
|
---|
129 | // av_log(s, AV_LOG_INFO, "TTA packet #%d desired size: %d read size: %d at pos %d\n",
|
---|
130 | // c->currentframe, size, ret, pkt->pos);
|
---|
131 | return 0; //ret;
|
---|
132 | }
|
---|
133 |
|
---|
134 | static int tta_read_close(AVFormatContext *s)
|
---|
135 | {
|
---|
136 | TTAContext *c = s->priv_data;
|
---|
137 | if (c->seektable)
|
---|
138 | av_free(c->seektable);
|
---|
139 | return 0;
|
---|
140 | }
|
---|
141 |
|
---|
142 | AVInputFormat tta_demuxer = {
|
---|
143 | "tta",
|
---|
144 | "true-audio",
|
---|
145 | sizeof(TTAContext),
|
---|
146 | tta_probe,
|
---|
147 | tta_read_header,
|
---|
148 | tta_read_packet,
|
---|
149 | tta_read_close,
|
---|
150 | .extensions = "tta",
|
---|
151 | };
|
---|
152 |
|
---|
153 | int tta_init(void)
|
---|
154 | {
|
---|
155 | av_register_input_format(&tta_demuxer);
|
---|
156 | return 0;
|
---|
157 | }
|
---|