]> git.tuebingen.mpg.de Git - paraslash.git/blob - ogg_afh.c
fix some signed warnings in aac and mp3 decoders
[paraslash.git] / ogg_afh.c
1 /*
2  * Copyright (C) 2004-2007 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18 /** \file ogg_afh.c para_server's ogg vorbis audio format handler */
19
20 #include <inttypes.h>
21 #include <ogg/ogg.h>
22 #include <vorbis/codec.h>
23 #include <vorbis/vorbisfile.h>
24
25 #include "server.h"
26 #include "error.h"
27 #include "string.h"
28
29 /** must be big enough to hold header */
30 #define CHUNK_SIZE 32768
31 static double chunk_time = 0.25;
32
33 struct ogg_datasource {
34         char *map;
35         off_t numbytes;
36         off_t fpos;
37 };
38
39 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
40 {
41         struct ogg_datasource *ods = datasource;
42         size_t copy = PARA_MIN(ods->numbytes - ods->fpos, size * nmemb),
43                 ret = copy / size;
44         if (!ret)
45                 return 0;
46         memcpy(buf, ods->map + ods->fpos, copy);
47 //      PARA_INFO_LOG("size: %zd, nmemb: %zd, ret: %zd\n", size, nmemb, ret);
48         ods->fpos += ret * size;
49         return ret;
50 }
51
52 static int cb_seek(void *datasource, ogg_int64_t offset,
53                 int whence)
54 {
55         struct ogg_datasource *ods = datasource;
56         switch (whence) {
57         case SEEK_SET:
58                 if (offset >= 0 && offset <= ods->numbytes) {
59                         ods->fpos = offset;
60                         return 0;
61                 }
62                 errno = EINVAL;
63                 return -1;
64                 break;
65         case SEEK_END:
66                 if (offset <= 0 && -offset <= ods->numbytes) {
67                         ods->fpos = ods->numbytes + offset;
68                         return 0;
69                 }
70                 errno = EINVAL;
71                 return -1;
72                 break;
73         case SEEK_CUR:
74                 if ((offset >= 0 && offset + ods->fpos > ods->numbytes) ||
75                                 (offset < 0 && offset + ods->fpos < 0)) {
76                         errno = EINVAL;
77                         return -1;
78                 }
79                 ods->fpos += offset;
80                 return 0;
81         }
82         errno = EINVAL;
83         return -1;
84 }
85
86 /* don't do anything as vss still needs the open filehandle */
87 static int cb_close(__a_unused void *datasource)
88 {
89         return 0;
90 }
91
92 static long cb_tell(void *datasource)
93 {
94         struct ogg_datasource *ods = datasource;
95         return (unsigned long)ods->fpos;
96 }
97
98 static int ogg_open_callbacks(void *datasource, OggVorbis_File *vf, ov_callbacks c)
99 {
100         int ret = ov_open_callbacks(datasource, vf,
101                 NULL, /* no initial buffer */
102                 0, /* no initial bytes */
103                 c); /* the ov_open_callbacks */
104
105         /* FIXME: provide better error codes */
106         if (ret == OV_EREAD)
107                 return -E_OGG_READ;
108         if (ret == OV_ENOTVORBIS)
109                 return -E_OGG_READ;
110         if (ret == OV_EVERSION)
111                 return -E_OGG_READ;
112         if (ret == OV_EBADHEADER)
113                 return -E_OGG_READ;
114         if (ret < 0)
115                 return -E_OGG_READ;
116         return 1;
117
118 }
119
120 static int ogg_compute_header_len(char *map, size_t numbytes,
121                 struct audio_format_info *afi)
122 {
123         int ret;
124         size_t len = PARA_MIN(numbytes, CHUNK_SIZE);
125         int serial;
126         char *buf;
127
128         ogg_page page;
129         ogg_packet packet;
130         vorbis_comment vc;
131         vorbis_info vi;
132         ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
133         ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
134         ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
135
136         ogg_sync_init(sync_in);
137         vorbis_info_init(&vi);
138         vorbis_comment_init(&vc);
139         buf = ogg_sync_buffer(sync_in, (long)len);
140         memcpy(buf, map, len);
141         ogg_sync_wrote(sync_in, (long)len);
142         ret = -E_SYNC_PAGEOUT;
143         if (ogg_sync_pageout(sync_in, &page) <= 0)
144                 goto err1;
145         serial = ogg_page_serialno(&page);
146         ogg_stream_init(stream_in, serial);
147         ogg_stream_init(stream_out, serial);
148         ret = ogg_stream_pagein(stream_in, &page);
149         if (ret < 0) {
150                 ret = E_STREAM_PAGEIN;
151                 goto err2;
152         }
153         ret = ogg_stream_packetout(stream_in, &packet);
154         if (ret != 1) {
155                 ret = -E_STREAM_PACKETOUT;
156                 goto err2;
157         }
158         ret = -E_VORBIS;
159         if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
160                 goto err2;
161         PARA_INFO_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
162         ogg_stream_packetin(stream_out, &packet);
163
164         ret = ogg_sync_pageout(sync_in, &page);
165         if (ret <= 0) {
166                 ret = -E_SYNC_PAGEOUT;
167                 goto err2;
168         }
169         ogg_stream_pagein(stream_in, &page);
170         ogg_stream_packetout(stream_in, &packet);
171         ogg_stream_packetin(stream_out, &packet);
172
173         ret = ogg_sync_pageout(sync_in, &page);
174         if (ret <= 0) {
175                 ret = -E_SYNC_PAGEOUT;
176                 goto err2;
177         }
178         ogg_stream_pagein(stream_in, &page);
179         ogg_stream_packetout(stream_in, &packet);
180         ogg_stream_packetin(stream_out, &packet);
181
182         afi->header_len = 0;
183         while (ogg_stream_flush(stream_out, &page))
184                 afi->header_len += page.body_len + page.header_len;
185         PARA_INFO_LOG("header_len = %d\n", afi->header_len);
186         afi->header_offset = 0;
187         ret = 1;
188 err2:
189         ogg_stream_destroy(stream_in);
190         ogg_stream_destroy(stream_out);
191 err1:
192         ogg_sync_destroy(sync_in);
193         vorbis_info_clear(&vi);
194         vorbis_comment_clear(&vc);
195         return ret;
196 }
197
198 /*
199  * Alloc and fill array table of byte offsets. chunk_table[i] is the
200  * offset in the current input file at which the sample containing time i *
201  * CHUNK_TIME begins. Always successful.
202  */
203 static long unsigned ogg_compute_chunk_table(OggVorbis_File *of,
204         struct audio_format_info *afi, long unsigned time_total)
205 {
206         int i, ret, num;
207         ssize_t max_chunk_len, pos = 0, min = 0, old_pos;
208         long unsigned num_chunks;
209
210         old_pos = 0;
211         ret = 0;
212         num = time_total / chunk_time + 3;
213         PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
214                 chunk_time, num);
215         afi->chunk_table = para_malloc((num + 1) * sizeof(size_t));
216         afi->chunk_table[0] = 0;
217         max_chunk_len = 0;
218         for (i = 1; ret <= num; i++) {
219                 ogg_int64_t diff;
220                 ret = ov_time_seek(of, i * chunk_time);
221                 if (ret)
222                         break;
223                 pos = ov_raw_tell(of);
224                 diff = pos - old_pos;
225                 max_chunk_len = PARA_MAX(max_chunk_len, diff);
226                 min = (i == 1)? diff : PARA_MIN(min, diff);
227                 afi->chunk_table[i] = pos;
228 //              if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
229 //                      PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
230 //                              "size: %zd\n", i - 1,
231 //                              i * chunk_time, pos, pos - old_pos);
232                 old_pos = pos;
233         }
234         num_chunks = i - 1;
235 //fi->chunk_table[i] = pos;
236         PARA_INFO_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
237                 num_chunks, chunk_time, max_chunk_len, min);
238         return num_chunks;
239 }
240
241 /*
242  * Init oggvorbis file and write some tech data to given pointers.
243  */
244 static int ogg_get_file_info(char *map, size_t numbytes,
245                 struct audio_format_info *afi)
246 {
247         int ret;
248         vorbis_info *vi;
249         OggVorbis_File of;
250         const ov_callbacks ovc = {
251                 .read_func = cb_read,
252                 .seek_func = cb_seek,
253                 .close_func = cb_close,
254                 .tell_func = cb_tell
255         };
256         struct ogg_datasource ods = {.map = map, .numbytes = numbytes, .fpos = 0};
257
258         ret = ogg_compute_header_len(map, numbytes, afi);
259         if (ret < 0)
260                 return ret;
261         ret = ogg_open_callbacks(&ods, &of, ovc);
262         if (ret < 0)
263                 goto err;
264         ret = -E_OGG_INFO;
265         vi = ov_info(&of, 0);
266         if (!vi)
267                 goto err;
268         afi->seconds_total = ov_time_total(&of, -1);
269         afi->frequency = vi->rate;
270         afi->bitrate = ov_bitrate(&of, 0);
271         afi->channels = vi->channels;
272         afi->chunks_total = ogg_compute_chunk_table(&of, afi, afi->seconds_total);
273         sprintf(afi->info_string, "audio_file_info1:%lu x %lu, %ukHz, "
274                 "%d channels, %ukbps\n"
275                 "audio_file_info2: \n"
276                 "audio_file_info3: \n",
277                 afi->chunks_total, (long unsigned) (chunk_time * 1000 * 1000),
278                 afi->frequency / 1000, vi->channels, afi->bitrate / 1000
279                 );
280         afi->chunk_tv.tv_sec = 0;
281         afi->chunk_tv.tv_usec = 250 * 1000;
282         tv_scale(3, &afi->chunk_tv, &afi->eof_tv);
283         ret = 1;
284 err:
285         ov_clear(&of); /* keeps the file open */
286         return ret;
287 }
288
289 static const char* ogg_suffixes[] = {"ogg", NULL};
290
291 /**
292  * the init function of the ogg vorbis audio format handler
293  *
294  * \param afh pointer to the struct to initialize
295  */
296 void ogg_init(struct audio_format_handler *afh)
297 {
298         afh->get_file_info = ogg_get_file_info,
299         afh->suffixes = ogg_suffixes;
300 }