• 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 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 "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; // not tta file
00054 
00055     avio_skip(s->pb, 2); // FIXME: flags
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); // header crc
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)){
00077         av_log(s, AV_LOG_ERROR, "totalframes too large\n");
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); // seektable crc
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         //this check is redundant as avio_read should fail
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     // FIXME!
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 
00146     c->currentframe = index;
00147     avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
00148 
00149     return 0;
00150 }
00151 
00152 AVInputFormat ff_tta_demuxer = {
00153     .name           = "tta",
00154     .long_name      = NULL_IF_CONFIG_SMALL("True Audio"),
00155     .priv_data_size = sizeof(TTAContext),
00156     .read_probe     = tta_probe,
00157     .read_header    = tta_read_header,
00158     .read_packet    = tta_read_packet,
00159     .read_seek      = tta_read_seek,
00160     .extensions = "tta",
00161 };
Generated on Thu Jul 11 2013 15:38:21 for Libav by doxygen 1.7.1