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

Generated on Fri Sep 16 2011 17:17:44 for FFmpeg by  doxygen 1.7.1