Libav 0.7.1
|
00001 /* 00002 * TTA demuxer 00003 * Copyright (c) 2006 Alex Beregszaszi 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "libavcodec/get_bits.h" 00023 #include "avformat.h" 00024 #include "id3v1.h" 00025 #include "libavutil/dict.h" 00026 00027 typedef struct { 00028 int totalframes, currentframe; 00029 } TTAContext; 00030 00031 static int tta_probe(AVProbeData *p) 00032 { 00033 const uint8_t *d = p->buf; 00034 00035 if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1') 00036 return 80; 00037 return 0; 00038 } 00039 00040 static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap) 00041 { 00042 TTAContext *c = s->priv_data; 00043 AVStream *st; 00044 int i, channels, bps, samplerate, datalen, framelen; 00045 uint64_t framepos, start_offset; 00046 00047 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX)) 00048 ff_id3v1_read(s); 00049 00050 start_offset = avio_tell(s->pb); 00051 if (avio_rl32(s->pb) != AV_RL32("TTA1")) 00052 return -1; // not tta file 00053 00054 avio_skip(s->pb, 2); // FIXME: flags 00055 channels = avio_rl16(s->pb); 00056 bps = avio_rl16(s->pb); 00057 samplerate = avio_rl32(s->pb); 00058 if(samplerate <= 0 || samplerate > 1000000){ 00059 av_log(s, AV_LOG_ERROR, "nonsense samplerate\n"); 00060 return -1; 00061 } 00062 00063 datalen = avio_rl32(s->pb); 00064 if(datalen < 0){ 00065 av_log(s, AV_LOG_ERROR, "nonsense datalen\n"); 00066 return -1; 00067 } 00068 00069 avio_skip(s->pb, 4); // header crc 00070 00071 framelen = samplerate*256/245; 00072 c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0); 00073 c->currentframe = 0; 00074 00075 if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){ 00076 av_log(s, AV_LOG_ERROR, "totalframes too large\n"); 00077 return -1; 00078 } 00079 00080 st = av_new_stream(s, 0); 00081 if (!st) 00082 return AVERROR(ENOMEM); 00083 00084 av_set_pts_info(st, 64, 1, samplerate); 00085 st->start_time = 0; 00086 st->duration = datalen; 00087 00088 framepos = avio_tell(s->pb) + 4*c->totalframes + 4; 00089 00090 for (i = 0; i < c->totalframes; i++) { 00091 uint32_t size = avio_rl32(s->pb); 00092 av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME); 00093 framepos += size; 00094 } 00095 avio_skip(s->pb, 4); // seektable crc 00096 00097 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00098 st->codec->codec_id = CODEC_ID_TTA; 00099 st->codec->channels = channels; 00100 st->codec->sample_rate = samplerate; 00101 st->codec->bits_per_coded_sample = bps; 00102 00103 st->codec->extradata_size = avio_tell(s->pb) - start_offset; 00104 if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){ 00105 //this check is redundant as avio_read should fail 00106 av_log(s, AV_LOG_ERROR, "extradata_size too large\n"); 00107 return -1; 00108 } 00109 st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE); 00110 avio_seek(s->pb, start_offset, SEEK_SET); 00111 avio_read(s->pb, st->codec->extradata, st->codec->extradata_size); 00112 00113 return 0; 00114 } 00115 00116 static int tta_read_packet(AVFormatContext *s, AVPacket *pkt) 00117 { 00118 TTAContext *c = s->priv_data; 00119 AVStream *st = s->streams[0]; 00120 int size, ret; 00121 00122 // FIXME! 00123 if (c->currentframe > c->totalframes) 00124 return -1; 00125 00126 size = st->index_entries[c->currentframe].size; 00127 00128 ret = av_get_packet(s->pb, pkt, size); 00129 pkt->dts = st->index_entries[c->currentframe++].timestamp; 00130 return ret; 00131 } 00132 00133 static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) 00134 { 00135 TTAContext *c = s->priv_data; 00136 AVStream *st = s->streams[stream_index]; 00137 int index = av_index_search_timestamp(st, timestamp, flags); 00138 if (index < 0) 00139 return -1; 00140 00141 c->currentframe = index; 00142 avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET); 00143 00144 return 0; 00145 } 00146 00147 AVInputFormat ff_tta_demuxer = { 00148 "tta", 00149 NULL_IF_CONFIG_SMALL("True Audio"), 00150 sizeof(TTAContext), 00151 tta_probe, 00152 tta_read_header, 00153 tta_read_packet, 00154 NULL, 00155 tta_read_seek, 00156 .extensions = "tta", 00157 };