tta.c 4.43 KB
Newer Older
1 2 3 4
/*
 * TTA demuxer
 * Copyright (c) 2006 Alex Beregszaszi
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

22
#include "libavcodec/get_bits.h"
23
#include "avformat.h"
24
#include "id3v1.h"
25 26 27 28 29 30 31 32

typedef struct {
    int totalframes, currentframe;
} TTAContext;

static int tta_probe(AVProbeData *p)
{
    const uint8_t *d = p->buf;
33

34 35 36 37 38 39 40 41 42
    if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
        return 80;
    return 0;
}

static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
{
    TTAContext *c = s->priv_data;
    AVStream *st;
43
    int i, channels, bps, samplerate, datalen, framelen;
44 45 46 47
    uint64_t framepos, start_offset;

    if (!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
        ff_id3v1_read(s);
48

49
    start_offset = avio_tell(s->pb);
50
    if (avio_rl32(s->pb) != AV_RL32("TTA1"))
51 52
        return -1; // not tta file

53
    avio_skip(s->pb, 2); // FIXME: flags
54 55 56
    channels = avio_rl16(s->pb);
    bps = avio_rl16(s->pb);
    samplerate = avio_rl32(s->pb);
57 58 59 60 61
    if(samplerate <= 0 || samplerate > 1000000){
        av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
        return -1;
    }

62
    datalen = avio_rl32(s->pb);
63 64 65 66 67
    if(datalen < 0){
        av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
        return -1;
    }

68
    avio_skip(s->pb, 4); // header crc
69

70
    framelen = samplerate*256/245;
71 72 73
    c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
    c->currentframe = 0;

74 75 76 77
    if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
        av_log(s, AV_LOG_ERROR, "totalframes too large\n");
        return -1;
    }
78 79 80

    st = av_new_stream(s, 0);
    if (!st)
81
        return AVERROR(ENOMEM);
82 83 84 85 86

    av_set_pts_info(st, 64, 1, samplerate);
    st->start_time = 0;
    st->duration = datalen;

87
    framepos = avio_tell(s->pb) + 4*c->totalframes + 4;
88 89

    for (i = 0; i < c->totalframes; i++) {
90
        uint32_t size = avio_rl32(s->pb);
91 92 93
        av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
        framepos += size;
    }
94
    avio_skip(s->pb, 4); // seektable crc
95

96
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
97 98 99
    st->codec->codec_id = CODEC_ID_TTA;
    st->codec->channels = channels;
    st->codec->sample_rate = samplerate;
100
    st->codec->bits_per_coded_sample = bps;
101

102
    st->codec->extradata_size = avio_tell(s->pb) - start_offset;
103
    if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
104
        //this check is redundant as avio_read should fail
105 106 107
        av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
        return -1;
    }
108
    st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
109
    avio_seek(s->pb, start_offset, SEEK_SET);
110
    avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
111 112 113 114 115 116 117

    return 0;
}

static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    TTAContext *c = s->priv_data;
118
    AVStream *st = s->streams[0];
David Conrad's avatar
David Conrad committed
119
    int size, ret;
120 121 122

    // FIXME!
    if (c->currentframe > c->totalframes)
123
        return -1;
124

David Conrad's avatar
David Conrad committed
125
    size = st->index_entries[c->currentframe].size;
126

127
    ret = av_get_packet(s->pb, pkt, size);
David Conrad's avatar
David Conrad committed
128 129
    pkt->dts = st->index_entries[c->currentframe++].timestamp;
    return ret;
130 131
}

132 133 134 135 136 137 138 139 140
static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
{
    TTAContext *c = s->priv_data;
    AVStream *st = s->streams[stream_index];
    int index = av_index_search_timestamp(st, timestamp, flags);
    if (index < 0)
        return -1;

    c->currentframe = index;
141
    avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
142 143 144 145

    return 0;
}

146
AVInputFormat ff_tta_demuxer = {
147
    "tta",
148
    NULL_IF_CONFIG_SMALL("True Audio"),
149 150 151 152
    sizeof(TTAContext),
    tta_probe,
    tta_read_header,
    tta_read_packet,
153
    NULL,
154
    tta_read_seek,
155 156
    .extensions = "tta",
};