tta: fix off be 1 error in the end detection.
[libav.git] / libavformat / tta.c
CommitLineData
aafaeabb
AB
1/*
2 * TTA demuxer
3 * Copyright (c) 2006 Alex Beregszaszi
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
aafaeabb
AB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
aafaeabb 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
aafaeabb
AB
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
e5a389a1 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
aafaeabb 20 */
245976da 21
9106a698 22#include "libavcodec/get_bits.h"
aafaeabb 23#include "avformat.h"
c3f9ebf7 24#include "internal.h"
22ff3361 25#include "id3v1.h"
d2d67e42 26#include "libavutil/dict.h"
aafaeabb
AB
27
28typedef struct {
29 int totalframes, currentframe;
aafaeabb
AB
30} TTAContext;
31
32static int tta_probe(AVProbeData *p)
33{
34 const uint8_t *d = p->buf;
22ff3361 35
aafaeabb
AB
36 if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
37 return 80;
38 return 0;
39}
40
41static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
42{
43 TTAContext *c = s->priv_data;
44 AVStream *st;
09a62866 45 int i, channels, bps, samplerate, datalen, framelen;
22ff3361
JM
46 uint64_t framepos, start_offset;
47
d2d67e42 48 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
22ff3361 49 ff_id3v1_read(s);
aafaeabb 50
a2704c97 51 start_offset = avio_tell(s->pb);
b7effd4e 52 if (avio_rl32(s->pb) != AV_RL32("TTA1"))
aafaeabb
AB
53 return -1; // not tta file
54
45a8a02a 55 avio_skip(s->pb, 2); // FIXME: flags
b7effd4e
AK
56 channels = avio_rl16(s->pb);
57 bps = avio_rl16(s->pb);
58 samplerate = avio_rl32(s->pb);
a443a253
MN
59 if(samplerate <= 0 || samplerate > 1000000){
60 av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
61 return -1;
62 }
63
b7effd4e 64 datalen = avio_rl32(s->pb);
a443a253
MN
65 if(datalen < 0){
66 av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
67 return -1;
68 }
69
45a8a02a 70 avio_skip(s->pb, 4); // header crc
aafaeabb 71
dc417a6e 72 framelen = samplerate*256/245;
aafaeabb
AB
73 c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
74 c->currentframe = 0;
75
a443a253
MN
76 if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
77 av_log(s, AV_LOG_ERROR, "totalframes too large\n");
78 return -1;
79 }
aafaeabb 80
3b3bbdd3 81 st = avformat_new_stream(s, NULL);
aafaeabb 82 if (!st)
769e10f0 83 return AVERROR(ENOMEM);
756fb7fe 84
c3f9ebf7 85 avpriv_set_pts_info(st, 64, 1, samplerate);
756fb7fe
DC
86 st->start_time = 0;
87 st->duration = datalen;
88
a2704c97 89 framepos = avio_tell(s->pb) + 4*c->totalframes + 4;
756fb7fe
DC
90
91 for (i = 0; i < c->totalframes; i++) {
b7effd4e 92 uint32_t size = avio_rl32(s->pb);
756fb7fe
DC
93 av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
94 framepos += size;
95 }
45a8a02a 96 avio_skip(s->pb, 4); // seektable crc
756fb7fe 97
72415b2a 98 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
aafaeabb
AB
99 st->codec->codec_id = CODEC_ID_TTA;
100 st->codec->channels = channels;
101 st->codec->sample_rate = samplerate;
dd1c8f3e 102 st->codec->bits_per_coded_sample = bps;
aafaeabb 103
a2704c97 104 st->codec->extradata_size = avio_tell(s->pb) - start_offset;
a443a253 105 if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
b7effd4e 106 //this check is redundant as avio_read should fail
a443a253
MN
107 av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
108 return -1;
109 }
aa11db2f 110 st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
f540ca22
JR
111 if (!st->codec->extradata) {
112 st->codec->extradata_size = 0;
113 return AVERROR(ENOMEM);
114 }
6b4aa5da 115 avio_seek(s->pb, start_offset, SEEK_SET);
b7effd4e 116 avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
aafaeabb
AB
117
118 return 0;
119}
120
121static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
122{
123 TTAContext *c = s->priv_data;
756fb7fe 124 AVStream *st = s->streams[0];
160ab30f 125 int size, ret;
aafaeabb
AB
126
127 // FIXME!
d8b33a99 128 if (c->currentframe >= c->totalframes)
756fb7fe 129 return -1;
aafaeabb 130
160ab30f 131 size = st->index_entries[c->currentframe].size;
aafaeabb 132
899681cd 133 ret = av_get_packet(s->pb, pkt, size);
160ab30f
DC
134 pkt->dts = st->index_entries[c->currentframe++].timestamp;
135 return ret;
aafaeabb
AB
136}
137
70fa2486
DC
138static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
139{
140 TTAContext *c = s->priv_data;
141 AVStream *st = s->streams[stream_index];
142 int index = av_index_search_timestamp(st, timestamp, flags);
143 if (index < 0)
144 return -1;
145
146 c->currentframe = index;
6b4aa5da 147 avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
70fa2486
DC
148
149 return 0;
150}
151
c6610a21 152AVInputFormat ff_tta_demuxer = {
dfc2c4d9
AK
153 .name = "tta",
154 .long_name = NULL_IF_CONFIG_SMALL("True Audio"),
155 .priv_data_size = sizeof(TTAContext),
156 .read_probe = tta_probe,
157 .read_header = tta_read_header,
158 .read_packet = tta_read_packet,
159 .read_seek = tta_read_seek,
aafaeabb
AB
160 .extensions = "tta",
161};