• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • 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/bitstream.h"
00023 #include "avformat.h"
00024 
00025 typedef struct {
00026     int totalframes, currentframe;
00027 } TTAContext;
00028 
00029 static int tta_probe(AVProbeData *p)
00030 {
00031     const uint8_t *d = p->buf;
00032     if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
00033         return 80;
00034     return 0;
00035 }
00036 
00037 static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
00038 {
00039     TTAContext *c = s->priv_data;
00040     AVStream *st;
00041     int i, channels, bps, samplerate, datalen, framelen;
00042     uint64_t framepos;
00043 
00044     if (get_le32(s->pb) != AV_RL32("TTA1"))
00045         return -1; // not tta file
00046 
00047     url_fskip(s->pb, 2); // FIXME: flags
00048     channels = get_le16(s->pb);
00049     bps = get_le16(s->pb);
00050     samplerate = get_le32(s->pb);
00051     if(samplerate <= 0 || samplerate > 1000000){
00052         av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
00053         return -1;
00054     }
00055 
00056     datalen = get_le32(s->pb);
00057     if(datalen < 0){
00058         av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
00059         return -1;
00060     }
00061 
00062     url_fskip(s->pb, 4); // header crc
00063 
00064     framelen = samplerate*256/245;
00065     c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
00066     c->currentframe = 0;
00067 
00068     if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
00069         av_log(s, AV_LOG_ERROR, "totalframes too large\n");
00070         return -1;
00071     }
00072 
00073     st = av_new_stream(s, 0);
00074     if (!st)
00075         return AVERROR(ENOMEM);
00076 
00077     av_set_pts_info(st, 64, 1, samplerate);
00078     st->start_time = 0;
00079     st->duration = datalen;
00080 
00081     framepos = url_ftell(s->pb) + 4*c->totalframes + 4;
00082 
00083     for (i = 0; i < c->totalframes; i++) {
00084         uint32_t size = get_le32(s->pb);
00085         av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
00086         framepos += size;
00087     }
00088     url_fskip(s->pb, 4); // seektable crc
00089 
00090     st->codec->codec_type = CODEC_TYPE_AUDIO;
00091     st->codec->codec_id = CODEC_ID_TTA;
00092     st->codec->channels = channels;
00093     st->codec->sample_rate = samplerate;
00094     st->codec->bits_per_coded_sample = bps;
00095 
00096     st->codec->extradata_size = url_ftell(s->pb);
00097     if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
00098         //this check is redundant as get_buffer should fail
00099         av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
00100         return -1;
00101     }
00102     st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
00103     url_fseek(s->pb, 0, SEEK_SET);
00104     get_buffer(s->pb, st->codec->extradata, st->codec->extradata_size);
00105 
00106     return 0;
00107 }
00108 
00109 static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
00110 {
00111     TTAContext *c = s->priv_data;
00112     AVStream *st = s->streams[0];
00113     int size, ret;
00114 
00115     // FIXME!
00116     if (c->currentframe > c->totalframes)
00117         return -1;
00118 
00119     size = st->index_entries[c->currentframe].size;
00120 
00121     ret = av_get_packet(s->pb, pkt, size);
00122     pkt->dts = st->index_entries[c->currentframe++].timestamp;
00123     return ret;
00124 }
00125 
00126 static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00127 {
00128     TTAContext *c = s->priv_data;
00129     AVStream *st = s->streams[stream_index];
00130     int index = av_index_search_timestamp(st, timestamp, flags);
00131     if (index < 0)
00132         return -1;
00133 
00134     c->currentframe = index;
00135     url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
00136 
00137     return 0;
00138 }
00139 
00140 AVInputFormat tta_demuxer = {
00141     "tta",
00142     NULL_IF_CONFIG_SMALL("True Audio"),
00143     sizeof(TTAContext),
00144     tta_probe,
00145     tta_read_header,
00146     tta_read_packet,
00147     NULL,
00148     tta_read_seek,
00149     .extensions = "tta",
00150 };

Generated on Sat Feb 16 2013 09:23:14 for ffmpeg by  doxygen 1.7.1