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