Statistics
| Branch: | Revision:

ffmpeg / libavformat / tta.c @ e19fbbec

History | View | Annotate | Download (4.2 KB)

1 aafaeabb Alex Beregszaszi
/*
2
 * TTA demuxer
3
 * Copyright (c) 2006 Alex Beregszaszi
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 aafaeabb Alex Beregszaszi
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 aafaeabb Alex Beregszaszi
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 aafaeabb Alex Beregszaszi
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 538389c9 Diego Biurrun
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 aafaeabb Alex Beregszaszi
 */
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 09a62866 Michael Niedermayer
    int i, channels, bps, samplerate, datalen, framelen;
44 aafaeabb Alex Beregszaszi
45
    if (get_le32(&s->pb) != ff_get_fourcc("TTA1"))
46
        return -1; // not tta file
47
48
    url_fskip(&s->pb, 2); // FIXME: flags
49
    channels = get_le16(&s->pb);
50
    bps = get_le16(&s->pb);
51
    samplerate = get_le32(&s->pb);
52 a443a253 Michael Niedermayer
    if(samplerate <= 0 || samplerate > 1000000){
53
        av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
54
        return -1;
55
    }
56
57 aafaeabb Alex Beregszaszi
    datalen = get_le32(&s->pb);
58 a443a253 Michael Niedermayer
    if(datalen < 0){
59
        av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
60
        return -1;
61
    }
62
63 aafaeabb Alex Beregszaszi
    url_fskip(&s->pb, 4); // header crc
64
65 dc417a6e Michael Niedermayer
    framelen = samplerate*256/245;
66 aafaeabb Alex Beregszaszi
    c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
67
    c->currentframe = 0;
68
69 a443a253 Michael Niedermayer
    if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
70
        av_log(s, AV_LOG_ERROR, "totalframes too large\n");
71
        return -1;
72
    }
73 aafaeabb Alex Beregszaszi
    c->seektable = av_mallocz(sizeof(uint32_t)*c->totalframes);
74
    if (!c->seektable)
75
        return AVERROR_NOMEM;
76
77
    for (i = 0; i < c->totalframes; i++)
78 30a1380a Diego Biurrun
        c->seektable[i] = get_le32(&s->pb);
79 aafaeabb Alex Beregszaszi
    url_fskip(&s->pb, 4); // seektable crc
80
81
    st = av_new_stream(s, 0);
82
//    av_set_pts_info(st, 32, 1, 1000);
83
    if (!st)
84
        return AVERROR_NOMEM;
85
    st->codec->codec_type = CODEC_TYPE_AUDIO;
86
    st->codec->codec_id = CODEC_ID_TTA;
87
    st->codec->channels = channels;
88
    st->codec->sample_rate = samplerate;
89
    st->codec->bits_per_sample = bps;
90
91 09a62866 Michael Niedermayer
    st->codec->extradata_size = url_ftell(&s->pb);
92 a443a253 Michael Niedermayer
    if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
93
        //this check is redundant as get_buffer should fail
94
        av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
95
        return -1;
96
    }
97 aa11db2f Alex Beregszaszi
    st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
98 09a62866 Michael Niedermayer
    url_fseek(&s->pb, 0, SEEK_SET);
99 aafaeabb Alex Beregszaszi
    get_buffer(&s->pb, st->codec->extradata, st->codec->extradata_size);
100
101
    return 0;
102
}
103
104
static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
105
{
106
    TTAContext *c = s->priv_data;
107
    int ret, size;
108
109
    // FIXME!
110
    if (c->currentframe > c->totalframes)
111
        size = 0;
112
    else
113
        size = c->seektable[c->currentframe];
114
115
    c->currentframe++;
116
117
    if (av_new_packet(pkt, size) < 0)
118
        return AVERROR_IO;
119
120
    pkt->pos = url_ftell(&s->pb);
121
    pkt->stream_index = 0;
122
    ret = get_buffer(&s->pb, pkt->data, size);
123
    if (ret <= 0) {
124
        av_free_packet(pkt);
125
        return AVERROR_IO;
126
    }
127
    pkt->size = ret;
128
//    av_log(s, AV_LOG_INFO, "TTA packet #%d desired size: %d read size: %d at pos %d\n",
129
//        c->currentframe, size, ret, pkt->pos);
130
    return 0; //ret;
131
}
132
133
static int tta_read_close(AVFormatContext *s)
134
{
135
    TTAContext *c = s->priv_data;
136
    if (c->seektable)
137
        av_free(c->seektable);
138
    return 0;
139
}
140
141 d2a067d1 Måns Rullgård
AVInputFormat tta_demuxer = {
142 aafaeabb Alex Beregszaszi
    "tta",
143
    "true-audio",
144
    sizeof(TTAContext),
145
    tta_probe,
146
    tta_read_header,
147
    tta_read_packet,
148
    tta_read_close,
149
    .extensions = "tta",
150
};