OSDN Git Service

Merge remote-tracking branch 'qatar/master'
[coroid/ffmpeg_saccubus.git] / libavformat / tta.c
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 "id3v1.h"
25 #include "libavutil/dict.h"
26
27 typedef struct {
28     int totalframes, currentframe;
29 } TTAContext;
30
31 static int tta_probe(AVProbeData *p)
32 {
33     const uint8_t *d = p->buf;
34
35     if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
36         return 80;
37     return 0;
38 }
39
40 static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
41 {
42     TTAContext *c = s->priv_data;
43     AVStream *st;
44     int i, channels, bps, samplerate, datalen, framelen;
45     uint64_t framepos, start_offset;
46
47     if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
48         ff_id3v1_read(s);
49
50     start_offset = avio_tell(s->pb);
51     if (avio_rl32(s->pb) != AV_RL32("TTA1"))
52         return -1; // not tta file
53
54     avio_skip(s->pb, 2); // FIXME: flags
55     channels = avio_rl16(s->pb);
56     bps = avio_rl16(s->pb);
57     samplerate = avio_rl32(s->pb);
58     if(samplerate <= 0 || samplerate > 1000000){
59         av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
60         return -1;
61     }
62
63     datalen = avio_rl32(s->pb);
64     if(datalen < 0){
65         av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
66         return -1;
67     }
68
69     avio_skip(s->pb, 4); // header crc
70
71     framelen = samplerate*256/245;
72     c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
73     c->currentframe = 0;
74
75     if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
76         av_log(s, AV_LOG_ERROR, "totalframes too large\n");
77         return -1;
78     }
79
80     st = av_new_stream(s, 0);
81     if (!st)
82         return AVERROR(ENOMEM);
83
84     av_set_pts_info(st, 64, 1, samplerate);
85     st->start_time = 0;
86     st->duration = datalen;
87
88     framepos = avio_tell(s->pb) + 4*c->totalframes + 4;
89
90     for (i = 0; i < c->totalframes; i++) {
91         uint32_t size = avio_rl32(s->pb);
92         av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
93         framepos += size;
94     }
95     avio_skip(s->pb, 4); // seektable crc
96
97     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
98     st->codec->codec_id = CODEC_ID_TTA;
99     st->codec->channels = channels;
100     st->codec->sample_rate = samplerate;
101     st->codec->bits_per_coded_sample = bps;
102
103     st->codec->extradata_size = avio_tell(s->pb) - start_offset;
104     if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
105         //this check is redundant as avio_read should fail
106         av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
107         return -1;
108     }
109     st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
110     avio_seek(s->pb, start_offset, SEEK_SET);
111     avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
112
113     return 0;
114 }
115
116 static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
117 {
118     TTAContext *c = s->priv_data;
119     AVStream *st = s->streams[0];
120     int size, ret;
121
122     // FIXME!
123     if (c->currentframe > c->totalframes)
124         return -1;
125
126     size = st->index_entries[c->currentframe].size;
127
128     ret = av_get_packet(s->pb, pkt, size);
129     pkt->dts = st->index_entries[c->currentframe++].timestamp;
130     return ret;
131 }
132
133 static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
134 {
135     TTAContext *c = s->priv_data;
136     AVStream *st = s->streams[stream_index];
137     int index = av_index_search_timestamp(st, timestamp, flags);
138     if (index < 0)
139         return -1;
140
141     c->currentframe = index;
142     avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
143
144     return 0;
145 }
146
147 AVInputFormat ff_tta_demuxer = {
148     .name           = "tta",
149     .long_name      = NULL_IF_CONFIG_SMALL("True Audio"),
150     .priv_data_size = sizeof(TTAContext),
151     .read_probe     = tta_probe,
152     .read_header    = tta_read_header,
153     .read_packet    = tta_read_packet,
154     .read_seek      = tta_read_seek,
155     .extensions = "tta",
156 };