Statistics
| Branch: | Revision:

ffmpeg / libavformat / tta.c @ bd09e8a7

History | View | Annotate | Download (4.23 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 e5a389a1 Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 aafaeabb Alex Beregszaszi
 */
21 245976da Diego Biurrun
22
#include "libavcodec/bitstream.h"
23 aafaeabb Alex Beregszaszi
#include "avformat.h"
24
25
typedef struct {
26
    int totalframes, currentframe;
27
} TTAContext;
28
29
static int tta_probe(AVProbeData *p)
30
{
31
    const uint8_t *d = p->buf;
32
    if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
33
        return 80;
34
    return 0;
35
}
36
37
static int tta_read_header(AVFormatContext *s, AVFormatParameters *ap)
38
{
39
    TTAContext *c = s->priv_data;
40
    AVStream *st;
41 09a62866 Michael Niedermayer
    int i, channels, bps, samplerate, datalen, framelen;
42 756fb7fe David Conrad
    uint64_t framepos;
43 aafaeabb Alex Beregszaszi
44 899681cd Björn Axelsson
    if (get_le32(s->pb) != ff_get_fourcc("TTA1"))
45 aafaeabb Alex Beregszaszi
        return -1; // not tta file
46
47 899681cd Björn Axelsson
    url_fskip(s->pb, 2); // FIXME: flags
48
    channels = get_le16(s->pb);
49
    bps = get_le16(s->pb);
50
    samplerate = get_le32(s->pb);
51 a443a253 Michael Niedermayer
    if(samplerate <= 0 || samplerate > 1000000){
52
        av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
53
        return -1;
54
    }
55
56 899681cd Björn Axelsson
    datalen = get_le32(s->pb);
57 a443a253 Michael Niedermayer
    if(datalen < 0){
58
        av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
59
        return -1;
60
    }
61
62 899681cd Björn Axelsson
    url_fskip(s->pb, 4); // header crc
63 aafaeabb Alex Beregszaszi
64 dc417a6e Michael Niedermayer
    framelen = samplerate*256/245;
65 aafaeabb Alex Beregszaszi
    c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
66
    c->currentframe = 0;
67
68 a443a253 Michael Niedermayer
    if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
69
        av_log(s, AV_LOG_ERROR, "totalframes too large\n");
70
        return -1;
71
    }
72 aafaeabb Alex Beregszaszi
73
    st = av_new_stream(s, 0);
74
    if (!st)
75 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
76 756fb7fe David Conrad
77
    av_set_pts_info(st, 64, 1, samplerate);
78
    st->start_time = 0;
79
    st->duration = datalen;
80
81 899681cd Björn Axelsson
    framepos = url_ftell(s->pb) + 4*c->totalframes + 4;
82 756fb7fe David Conrad
83
    for (i = 0; i < c->totalframes; i++) {
84 899681cd Björn Axelsson
        uint32_t size = get_le32(s->pb);
85 756fb7fe David Conrad
        av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
86
        framepos += size;
87
    }
88 899681cd Björn Axelsson
    url_fskip(s->pb, 4); // seektable crc
89 756fb7fe David Conrad
90 aafaeabb Alex Beregszaszi
    st->codec->codec_type = CODEC_TYPE_AUDIO;
91
    st->codec->codec_id = CODEC_ID_TTA;
92
    st->codec->channels = channels;
93
    st->codec->sample_rate = samplerate;
94 dd1c8f3e Luca Abeni
    st->codec->bits_per_coded_sample = bps;
95 aafaeabb Alex Beregszaszi
96 899681cd Björn Axelsson
    st->codec->extradata_size = url_ftell(s->pb);
97 a443a253 Michael Niedermayer
    if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
98
        //this check is redundant as get_buffer should fail
99
        av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
100
        return -1;
101
    }
102 aa11db2f Alex Beregszaszi
    st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
103 899681cd Björn Axelsson
    url_fseek(s->pb, 0, SEEK_SET);
104
    get_buffer(s->pb, st->codec->extradata, st->codec->extradata_size);
105 aafaeabb Alex Beregszaszi
106
    return 0;
107
}
108
109
static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
110
{
111
    TTAContext *c = s->priv_data;
112 756fb7fe David Conrad
    AVStream *st = s->streams[0];
113 160ab30f David Conrad
    int size, ret;
114 aafaeabb Alex Beregszaszi
115
    // FIXME!
116
    if (c->currentframe > c->totalframes)
117 756fb7fe David Conrad
        return -1;
118 aafaeabb Alex Beregszaszi
119 160ab30f David Conrad
    size = st->index_entries[c->currentframe].size;
120 aafaeabb Alex Beregszaszi
121 899681cd Björn Axelsson
    ret = av_get_packet(s->pb, pkt, size);
122 160ab30f David Conrad
    pkt->dts = st->index_entries[c->currentframe++].timestamp;
123
    return ret;
124 aafaeabb Alex Beregszaszi
}
125
126 70fa2486 David Conrad
static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
127
{
128
    TTAContext *c = s->priv_data;
129
    AVStream *st = s->streams[stream_index];
130
    int index = av_index_search_timestamp(st, timestamp, flags);
131
    if (index < 0)
132
        return -1;
133
134
    c->currentframe = index;
135 899681cd Björn Axelsson
    url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
136 70fa2486 David Conrad
137
    return 0;
138
}
139
140 d2a067d1 Måns Rullgård
AVInputFormat tta_demuxer = {
141 aafaeabb Alex Beregszaszi
    "tta",
142 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("True Audio"),
143 aafaeabb Alex Beregszaszi
    sizeof(TTAContext),
144
    tta_probe,
145
    tta_read_header,
146
    tta_read_packet,
147 756fb7fe David Conrad
    NULL,
148 70fa2486 David Conrad
    tta_read_seek,
149 aafaeabb Alex Beregszaszi
    .extensions = "tta",
150
};