• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/tta.c

Go to the documentation of this file.
00001 /*
00002  * TTA demuxer
00003  * Copyright (c) 2006 Alex Beregszaszi
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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     if (!st->codec->extradata) {
00111         st->codec->extradata_size = 0;
00112         return AVERROR(ENOMEM);
00113     }
00114     avio_seek(s->pb, start_offset, SEEK_SET);
00115     avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
00116 
00117     return 0;
00118 }
00119 
00120 static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
00121 {
00122     TTAContext *c = s->priv_data;
00123     AVStream *st = s->streams[0];
00124     int size, ret;
00125 
00126     // FIXME!
00127     if (c->currentframe > c->totalframes)
00128         return -1;
00129 
00130     size = st->index_entries[c->currentframe].size;
00131 
00132     ret = av_get_packet(s->pb, pkt, size);
00133     pkt->dts = st->index_entries[c->currentframe++].timestamp;
00134     return ret;
00135 }
00136 
00137 static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00138 {
00139     TTAContext *c = s->priv_data;
00140     AVStream *st = s->streams[stream_index];
00141     int index = av_index_search_timestamp(st, timestamp, flags);
00142     if (index < 0)
00143         return -1;
00144 
00145     c->currentframe = index;
00146     avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
00147 
00148     return 0;
00149 }
00150 
00151 AVInputFormat ff_tta_demuxer = {
00152     "tta",
00153     NULL_IF_CONFIG_SMALL("True Audio"),
00154     sizeof(TTAContext),
00155     tta_probe,
00156     tta_read_header,
00157     tta_read_packet,
00158     NULL,
00159     tta_read_seek,
00160     .extensions = "tta",
00161 };

Generated on Wed Apr 11 2012 07:31:35 for FFmpeg by  doxygen 1.7.1