]> git.sesse.net Git - ffmpeg/blob - libavformat/tta.c
omx: Add support for broadcom OMX on raspberry pi
[ffmpeg] / libavformat / tta.c
1 /*
2  * TTA demuxer
3  * Copyright (c) 2006 Alex Beregszaszi
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "internal.h"
25 #include "id3v1.h"
26 #include "libavutil/dict.h"
27
28 typedef struct TTAContext {
29     int totalframes, currentframe;
30     int frame_size;
31     int last_frame_size;
32 } TTAContext;
33
34 static int tta_probe(AVProbeData *p)
35 {
36     const uint8_t *d = p->buf;
37
38     if (d[0] == 'T' && d[1] == 'T' && d[2] == 'A' && d[3] == '1')
39         return AVPROBE_SCORE_EXTENSION + 30;
40     return 0;
41 }
42
43 static int tta_read_header(AVFormatContext *s)
44 {
45     TTAContext *c = s->priv_data;
46     AVStream *st;
47     int i, channels, bps, samplerate, datalen;
48     int64_t framepos, start_offset;
49
50     if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
51         ff_id3v1_read(s);
52
53     start_offset = avio_tell(s->pb);
54     if (start_offset < 0)
55         return start_offset;
56     if (avio_rl32(s->pb) != AV_RL32("TTA1"))
57         return -1; // not tta file
58
59     avio_skip(s->pb, 2); // FIXME: flags
60     channels = avio_rl16(s->pb);
61     bps = avio_rl16(s->pb);
62     samplerate = avio_rl32(s->pb);
63     if(samplerate <= 0 || samplerate > 1000000){
64         av_log(s, AV_LOG_ERROR, "nonsense samplerate\n");
65         return -1;
66     }
67
68     datalen = avio_rl32(s->pb);
69     if(datalen < 0){
70         av_log(s, AV_LOG_ERROR, "nonsense datalen\n");
71         return -1;
72     }
73
74     avio_skip(s->pb, 4); // header crc
75
76     c->frame_size      = samplerate * 256 / 245;
77     c->last_frame_size = datalen % c->frame_size;
78     if (!c->last_frame_size)
79         c->last_frame_size = c->frame_size;
80     c->totalframes = datalen / c->frame_size + (c->last_frame_size < c->frame_size);
81     c->currentframe = 0;
82
83     if(c->totalframes >= UINT_MAX/sizeof(uint32_t)){
84         av_log(s, AV_LOG_ERROR, "totalframes too large\n");
85         return -1;
86     }
87
88     st = avformat_new_stream(s, NULL);
89     if (!st)
90         return AVERROR(ENOMEM);
91
92     avpriv_set_pts_info(st, 64, 1, samplerate);
93     st->start_time = 0;
94     st->duration = datalen;
95
96     framepos = avio_tell(s->pb);
97     if (framepos < 0)
98         return framepos;
99     framepos += 4 * c->totalframes + 4;
100
101     for (i = 0; i < c->totalframes; i++) {
102         uint32_t size = avio_rl32(s->pb);
103         av_add_index_entry(st, framepos, i * c->frame_size, size, 0,
104                            AVINDEX_KEYFRAME);
105         framepos += size;
106     }
107     avio_skip(s->pb, 4); // seektable crc
108
109     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
110     st->codecpar->codec_id = AV_CODEC_ID_TTA;
111     st->codecpar->channels = channels;
112     st->codecpar->sample_rate = samplerate;
113     st->codecpar->bits_per_coded_sample = bps;
114
115     st->codecpar->extradata_size = avio_tell(s->pb) - start_offset;
116     if (st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codecpar->extradata_size) {
117         //this check is redundant as avio_read should fail
118         av_log(s, AV_LOG_ERROR, "extradata_size too large\n");
119         return -1;
120     }
121     st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
122     if (!st->codecpar->extradata) {
123         st->codecpar->extradata_size = 0;
124         return AVERROR(ENOMEM);
125     }
126     avio_seek(s->pb, start_offset, SEEK_SET);
127     avio_read(s->pb, st->codecpar->extradata, st->codecpar->extradata_size);
128
129     return 0;
130 }
131
132 static int tta_read_packet(AVFormatContext *s, AVPacket *pkt)
133 {
134     TTAContext *c = s->priv_data;
135     AVStream *st = s->streams[0];
136     int size, ret;
137
138     // FIXME!
139     if (c->currentframe >= c->totalframes)
140         return AVERROR_EOF;
141
142     size = st->index_entries[c->currentframe].size;
143
144     ret = av_get_packet(s->pb, pkt, size);
145     pkt->dts = st->index_entries[c->currentframe++].timestamp;
146     pkt->duration = c->currentframe == c->totalframes ? c->last_frame_size :
147                                                         c->frame_size;
148     return ret;
149 }
150
151 static int tta_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
152 {
153     TTAContext *c = s->priv_data;
154     AVStream *st = s->streams[stream_index];
155     int index = av_index_search_timestamp(st, timestamp, flags);
156     if (index < 0)
157         return -1;
158     if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
159         return -1;
160
161     c->currentframe = index;
162
163     return 0;
164 }
165
166 AVInputFormat ff_tta_demuxer = {
167     .name           = "tta",
168     .long_name      = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
169     .priv_data_size = sizeof(TTAContext),
170     .read_probe     = tta_probe,
171     .read_header    = tta_read_header,
172     .read_packet    = tta_read_packet,
173     .read_seek      = tta_read_seek,
174     .extensions     = "tta",
175 };