Statistics
| Branch: | Revision:

ffmpeg / libavformat / tta.c @ f59d8ff8

History | View | Annotate | Download (4.43 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 9106a698 Stefano Sabatini
#include "libavcodec/get_bits.h"
23 aafaeabb Alex Beregszaszi
#include "avformat.h"
24 22ff3361 Jai Menon
#include "id3v1.h"
25 aafaeabb Alex Beregszaszi
26
typedef struct {
27
    int totalframes, currentframe;
28
} TTAContext;
29
30
static int tta_probe(AVProbeData *p)
31
{
32
    const uint8_t *d = p->buf;
33 22ff3361 Jai Menon
34 aafaeabb Alex Beregszaszi
    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 22ff3361 Jai Menon
    uint64_t framepos, start_offset;
45
46
    if (!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
47
        ff_id3v1_read(s);
48 aafaeabb Alex Beregszaszi
49 22ff3361 Jai Menon
    start_offset = url_ftell(s->pb);
50 e63a3628 Anton Khirnov
    if (avio_rl32(s->pb) != AV_RL32("TTA1"))
51 aafaeabb Alex Beregszaszi
        return -1; // not tta file
52
53 899681cd Björn Axelsson
    url_fskip(s->pb, 2); // FIXME: flags
54 e63a3628 Anton Khirnov
    channels = avio_rl16(s->pb);
55
    bps = avio_rl16(s->pb);
56
    samplerate = avio_rl32(s->pb);
57 a443a253 Michael Niedermayer
    if(samplerate <= 0 || samplerate > 1000000){
58
        av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
59
        return -1;
60
    }
61
62 e63a3628 Anton Khirnov
    datalen = avio_rl32(s->pb);
63 a443a253 Michael Niedermayer
    if(datalen < 0){
64
        av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
65
        return -1;
66
    }
67
68 899681cd Björn Axelsson
    url_fskip(s->pb, 4); // header crc
69 aafaeabb Alex Beregszaszi
70 dc417a6e Michael Niedermayer
    framelen = samplerate*256/245;
71 aafaeabb Alex Beregszaszi
    c->totalframes = datalen / framelen + ((datalen % framelen) ? 1 : 0);
72
    c->currentframe = 0;
73
74 a443a253 Michael Niedermayer
    if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
75
        av_log(s, AV_LOG_ERROR, "totalframes too large\n");
76
        return -1;
77
    }
78 aafaeabb Alex Beregszaszi
79
    st = av_new_stream(s, 0);
80
    if (!st)
81 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
82 756fb7fe David Conrad
83
    av_set_pts_info(st, 64, 1, samplerate);
84
    st->start_time = 0;
85
    st->duration = datalen;
86
87 899681cd Björn Axelsson
    framepos = url_ftell(s->pb) + 4*c->totalframes + 4;
88 756fb7fe David Conrad
89
    for (i = 0; i < c->totalframes; i++) {
90 e63a3628 Anton Khirnov
        uint32_t size = avio_rl32(s->pb);
91 756fb7fe David Conrad
        av_add_index_entry(st, framepos, i*framelen, size, 0, AVINDEX_KEYFRAME);
92
        framepos += size;
93
    }
94 899681cd Björn Axelsson
    url_fskip(s->pb, 4); // seektable crc
95 756fb7fe David Conrad
96 72415b2a Stefano Sabatini
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
97 aafaeabb Alex Beregszaszi
    st->codec->codec_id = CODEC_ID_TTA;
98
    st->codec->channels = channels;
99
    st->codec->sample_rate = samplerate;
100 dd1c8f3e Luca Abeni
    st->codec->bits_per_coded_sample = bps;
101 aafaeabb Alex Beregszaszi
102 22ff3361 Jai Menon
    st->codec->extradata_size = url_ftell(s->pb) - start_offset;
103 a443a253 Michael Niedermayer
    if(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
104 e63a3628 Anton Khirnov
        //this check is redundant as avio_read should fail
105 a443a253 Michael Niedermayer
        av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
106
        return -1;
107
    }
108 aa11db2f Alex Beregszaszi
    st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
109 f59d8ff8 Anton Khirnov
    avio_seek(s->pb, start_offset, SEEK_SET);
110 e63a3628 Anton Khirnov
    avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
111 aafaeabb Alex Beregszaszi
112
    return 0;
113
}
114
115
static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
116
{
117
    TTAContext *c = s->priv_data;
118 756fb7fe David Conrad
    AVStream *st = s->streams[0];
119 160ab30f David Conrad
    int size, ret;
120 aafaeabb Alex Beregszaszi
121
    // FIXME!
122
    if (c->currentframe > c->totalframes)
123 756fb7fe David Conrad
        return -1;
124 aafaeabb Alex Beregszaszi
125 160ab30f David Conrad
    size = st->index_entries[c->currentframe].size;
126 aafaeabb Alex Beregszaszi
127 899681cd Björn Axelsson
    ret = av_get_packet(s->pb, pkt, size);
128 160ab30f David Conrad
    pkt->dts = st->index_entries[c->currentframe++].timestamp;
129
    return ret;
130 aafaeabb Alex Beregszaszi
}
131
132 70fa2486 David Conrad
static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
133
{
134
    TTAContext *c = s->priv_data;
135
    AVStream *st = s->streams[stream_index];
136
    int index = av_index_search_timestamp(st, timestamp, flags);
137
    if (index < 0)
138
        return -1;
139
140
    c->currentframe = index;
141 f59d8ff8 Anton Khirnov
    avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
142 70fa2486 David Conrad
143
    return 0;
144
}
145
146 66355be3 Diego Elio Pettenò
AVInputFormat ff_tta_demuxer = {
147 aafaeabb Alex Beregszaszi
    "tta",
148 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("True Audio"),
149 aafaeabb Alex Beregszaszi
    sizeof(TTAContext),
150
    tta_probe,
151
    tta_read_header,
152
    tta_read_packet,
153 756fb7fe David Conrad
    NULL,
154 70fa2486 David Conrad
    tta_read_seek,
155 aafaeabb Alex Beregszaszi
    .extensions = "tta",
156
};