Statistics
| Branch: | Revision:

ffmpeg / libavformat / tta.c @ 80a289b9

History | View | Annotate | Download (4.3 KB)

1
/*
2
 * TTA demuxer
3
 * Copyright (c) 2006 Alex Beregszaszi
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * 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
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
20
 */
21
#include "avformat.h"
22
#include "bitstream.h"
23

    
24
typedef struct {
25
    int totalframes, currentframe;
26
    uint32_t *seektable;
27
} TTAContext;
28

    
29
static int tta_probe(AVProbeData *p)
30
{
31
    const uint8_t *d = p->buf;
32
    if (p->buf_size < 4)
33
        return 0;
34
    if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
35
        return 80;
36
    return 0;
37
}
38

    
39
static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
40
{
41
    TTAContext *c = s->priv_data;
42
    AVStream *st;
43
    int i, channels, bps, samplerate, datalen, framelen, start;
44

    
45
    start = url_ftell(&s->pb);
46

    
47
    if (get_le32(&s->pb) != ff_get_fourcc("TTA1"))
48
        return -1; // not tta file
49

    
50
    url_fskip(&s->pb, 2); // FIXME: flags
51
    channels = get_le16(&s->pb);
52
    bps = get_le16(&s->pb);
53
    samplerate = get_le32(&s->pb);
54
    if(samplerate <= 0 || samplerate > 1000000){
55
        av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
56
        return -1;
57
    }
58

    
59
    datalen = get_le32(&s->pb);
60
    if(datalen < 0){
61
        av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
62
        return -1;
63
    }
64

    
65
    url_fskip(&s->pb, 4); // header crc
66

    
67
    framelen = 1.04489795918367346939 * samplerate;
68
    c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
69
    c->currentframe = 0;
70

    
71
    if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
72
        av_log(s, AV_LOG_ERROR, "totalframes too large\n");
73
        return -1;
74
    }
75
    c->seektable = av_mallocz(sizeof(uint32_t)*c->totalframes);
76
    if (!c->seektable)
77
        return AVERROR_NOMEM;
78

    
79
    for (i = 0; i < c->totalframes; i++)
80
            c->seektable[i] = get_le32(&s->pb);
81
    url_fskip(&s->pb, 4); // seektable crc
82

    
83
    st = av_new_stream(s, 0);
84
//    av_set_pts_info(st, 32, 1, 1000);
85
    if (!st)
86
        return AVERROR_NOMEM;
87
    st->codec->codec_type = CODEC_TYPE_AUDIO;
88
    st->codec->codec_id = CODEC_ID_TTA;
89
    st->codec->channels = channels;
90
    st->codec->sample_rate = samplerate;
91
    st->codec->bits_per_sample = bps;
92

    
93
    st->codec->extradata_size = url_ftell(&s->pb) - start;
94
    if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
95
        //this check is redundant as get_buffer should fail
96
        av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
97
        return -1;
98
    }
99
    st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
100
    url_fseek(&s->pb, start, SEEK_SET); // or SEEK_CUR and -size ? :)
101
    get_buffer(&s->pb, st->codec->extradata, st->codec->extradata_size);
102

    
103
    return 0;
104
}
105

    
106
static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
107
{
108
    TTAContext *c = s->priv_data;
109
    int ret, size;
110

    
111
    // FIXME!
112
    if (c->currentframe > c->totalframes)
113
        size = 0;
114
    else
115
        size = c->seektable[c->currentframe];
116

    
117
    c->currentframe++;
118

    
119
    if (av_new_packet(pkt, size) < 0)
120
        return AVERROR_IO;
121

    
122
    pkt->pos = url_ftell(&s->pb);
123
    pkt->stream_index = 0;
124
    ret = get_buffer(&s->pb, pkt->data, size);
125
    if (ret <= 0) {
126
        av_free_packet(pkt);
127
        return AVERROR_IO;
128
    }
129
    pkt->size = ret;
130
//    av_log(s, AV_LOG_INFO, "TTA packet #%d desired size: %d read size: %d at pos %d\n",
131
//        c->currentframe, size, ret, pkt->pos);
132
    return 0; //ret;
133
}
134

    
135
static int tta_read_close(AVFormatContext *s)
136
{
137
    TTAContext *c = s->priv_data;
138
    if (c->seektable)
139
        av_free(c->seektable);
140
    return 0;
141
}
142

    
143
AVInputFormat tta_demuxer = {
144
    "tta",
145
    "true-audio",
146
    sizeof(TTAContext),
147
    tta_probe,
148
    tta_read_header,
149
    tta_read_packet,
150
    tta_read_close,
151
    .extensions = "tta",
152
};