FFmpeg  1.2.12
tta.c
Go to the documentation of this file.
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 "internal.h"
25 #include "id3v1.h"
26 #include "libavutil/dict.h"
27 
28 typedef struct {
29  int totalframes, currentframe;
32 } TTAContext;
33 
34 static int tta_probe(AVProbeData *p)
35 {
36  const uint8_t *d = p->buf;
37 
38  if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
39  return 80;
40  return 0;
41 }
42 
44 {
45  TTAContext *c = s->priv_data;
46  AVStream *st;
47  int i, channels, bps, samplerate;
48  uint64_t framepos, start_offset;
49  uint32_t datalen;
50 
52  ff_id3v1_read(s);
53 
54  start_offset = avio_tell(s->pb);
55  if (avio_rl32(s->pb) != AV_RL32("TTA1"))
56  return -1; // not tta file
57 
58  avio_skip(s->pb, 2); // FIXME: flags
59  channels = avio_rl16(s->pb);
60  bps = avio_rl16(s->pb);
61  samplerate = avio_rl32(s->pb);
62  if(samplerate <= 0 || samplerate > 1000000){
63  av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
64  return -1;
65  }
66 
67  datalen = avio_rl32(s->pb);
68  if (!datalen) {
69  av_log(s, AV_LOG_ERROR, "invalid datalen\n");
70  return AVERROR_INVALIDDATA;
71  }
72 
73  avio_skip(s->pb, 4); // header crc
74 
75  c->frame_size = samplerate * 256 / 245;
76  c->last_frame_size = datalen % c->frame_size;
77  if (!c->last_frame_size)
79  c->totalframes = datalen / c->frame_size + (c->last_frame_size < c->frame_size);
80  c->currentframe = 0;
81 
82  if(c->totalframes >= UINT_MAX/sizeof(uint32_t) || c->totalframes <= 0){
83  av_log(s, AV_LOG_ERROR, "totalframes %d invalid\n", c->totalframes);
84  return -1;
85  }
86 
87  st = avformat_new_stream(s, NULL);
88  if (!st)
89  return AVERROR(ENOMEM);
90 
91  avpriv_set_pts_info(st, 64, 1, samplerate);
92  st->start_time = 0;
93  st->duration = datalen;
94 
95  framepos = avio_tell(s->pb) + 4*c->totalframes + 4;
96 
97  for (i = 0; i < c->totalframes; i++) {
98  uint32_t size = avio_rl32(s->pb);
99  int r;
100  if ((r = av_add_index_entry(st, framepos, i * c->frame_size, size, 0,
101  AVINDEX_KEYFRAME)) < 0)
102  return r;
103  framepos += size;
104  }
105  avio_skip(s->pb, 4); // seektable crc
106 
109  st->codec->channels = channels;
110  st->codec->sample_rate = samplerate;
112 
113  st->codec->extradata_size = avio_tell(s->pb) - start_offset;
115  //this check is redundant as avio_read should fail
116  av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
117  return -1;
118  }
120  if (!st->codec->extradata) {
121  st->codec->extradata_size = 0;
122  return AVERROR(ENOMEM);
123  }
124  avio_seek(s->pb, start_offset, SEEK_SET);
126 
127  return 0;
128 }
129 
131 {
132  TTAContext *c = s->priv_data;
133  AVStream *st = s->streams[0];
134  int size, ret;
135 
136  // FIXME!
137  if (c->currentframe >= c->totalframes)
138  return AVERROR_EOF;
139 
140  if (st->nb_index_entries < c->totalframes) {
141  av_log(s, AV_LOG_ERROR, "Index entry disappeared\n");
142  return AVERROR_INVALIDDATA;
143  }
144 
145  size = st->index_entries[c->currentframe].size;
146 
147  ret = av_get_packet(s->pb, pkt, size);
148  pkt->dts = st->index_entries[c->currentframe++].timestamp;
149  pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
150  c->frame_size;
151  return ret;
152 }
153 
154 static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
155 {
156  TTAContext *c = s->priv_data;
157  AVStream *st = s->streams[stream_index];
158  int index = av_index_search_timestamp(st, timestamp, flags);
159  if (index < 0)
160  return -1;
161  if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
162  return -1;
163 
164  c->currentframe = index;
165 
166  return 0;
167 }
168 
170  .name = "tta",
171  .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
172  .priv_data_size = sizeof(TTAContext),
177  .extensions = "tta",
178 };