00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavcodec/get_bits.h"
00023 #include "avformat.h"
00024 #include "internal.h"
00025 #include "id3v1.h"
00026 #include "libavutil/dict.h"
00027
00028 typedef struct {
00029 int totalframes, currentframe;
00030 } TTAContext;
00031
00032 static int tta_probe(AVProbeData *p)
00033 {
00034 const uint8_t *d = p->buf;
00035
00036 if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
00037 return 80;
00038 return 0;
00039 }
00040
00041 static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
00042 {
00043 TTAContext *c = s->priv_data;
00044 AVStream *st;
00045 int i, channels, bps, samplerate, datalen, framelen;
00046 uint64_t framepos, start_offset;
00047
00048 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
00049 ff_id3v1_read(s);
00050
00051 start_offset = avio_tell(s->pb);
00052 if (avio_rl32(s->pb) != AV_RL32("TTA1"))
00053 return -1;
00054
00055 avio_skip(s->pb, 2);
00056 channels = avio_rl16(s->pb);
00057 bps = avio_rl16(s->pb);
00058 samplerate = avio_rl32(s->pb);
00059 if(samplerate <= 0 || samplerate > 1000000){
00060 av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
00061 return -1;
00062 }
00063
00064 datalen = avio_rl32(s->pb);
00065 if(datalen < 0){
00066 av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
00067 return -1;
00068 }
00069
00070 avio_skip(s->pb, 4);
00071
00072 framelen = samplerate*256/245;
00073 c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
00074 c->currentframe = 0;
00075
00076 if(c->totalframes >= UINT_MAX/sizeof(uint32_t) || c->totalframes <= 0){
00077 av_log(s, AV_LOG_ERROR, "totalframes %d invalid\n", c->totalframes);
00078 return -1;
00079 }
00080
00081 st = avformat_new_stream(s, NULL);
00082 if (!st)
00083 return AVERROR(ENOMEM);
00084
00085 avpriv_set_pts_info(st, 64, 1, samplerate);
00086 st->start_time = 0;
00087 st->duration = datalen;
00088
00089 framepos = avio_tell(s->pb) + 4*c->totalframes + 4;
00090
00091 for (i = 0; i < c->totalframes; i++) {
00092 uint32_t size = avio_rl32(s->pb);
00093 av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
00094 framepos += size;
00095 }
00096 avio_skip(s->pb, 4);
00097
00098 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00099 st->codec->codec_id = CODEC_ID_TTA;
00100 st->codec->channels = channels;
00101 st->codec->sample_rate = samplerate;
00102 st->codec->bits_per_coded_sample = bps;
00103
00104 st->codec->extradata_size = avio_tell(s->pb) - start_offset;
00105 if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
00106
00107 av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
00108 return -1;
00109 }
00110 st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
00111 if (!st->codec->extradata) {
00112 st->codec->extradata_size = 0;
00113 return AVERROR(ENOMEM);
00114 }
00115 avio_seek(s->pb, start_offset, SEEK_SET);
00116 avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
00117
00118 return 0;
00119 }
00120
00121 static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
00122 {
00123 TTAContext *c = s->priv_data;
00124 AVStream *st = s->streams[0];
00125 int size, ret;
00126
00127
00128 if (c->currentframe >= c->totalframes)
00129 return AVERROR_EOF;
00130
00131 size = st->index_entries[c->currentframe].size;
00132
00133 ret = av_get_packet(s->pb, pkt, size);
00134 pkt->dts = st->index_entries[c->currentframe++].timestamp;
00135 return ret;
00136 }
00137
00138 static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00139 {
00140 TTAContext *c = s->priv_data;
00141 AVStream *st = s->streams[stream_index];
00142 int index = av_index_search_timestamp(st, timestamp, flags);
00143 if (index < 0)
00144 return -1;
00145 if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
00146 return -1;
00147
00148 c->currentframe = index;
00149
00150 return 0;
00151 }
00152
00153 AVInputFormat ff_tta_demuxer = {
00154 .name = "tta",
00155 .long_name = NULL_IF_CONFIG_SMALL("True Audio"),
00156 .priv_data_size = sizeof(TTAContext),
00157 .read_probe = tta_probe,
00158 .read_header = tta_read_header,
00159 .read_packet = tta_read_packet,
00160 .read_seek = tta_read_seek,
00161 .extensions = "tta",
00162 };