]> git.tuebingen.mpg.de Git - paraslash.git/blob - ogg_afh.c
ogg_afh.c: Switch to ogg vorbis callbacks
[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.cmdline.h"
26 #include "server.h"
27 #include "vss.h"
28 #include "afh.h"
29 #include "error.h"
30 #include "string.h"
31
32 /** must be big enough to hold header */
33 #define CHUNK_SIZE 32768
34 static double chunk_time = 0.25;
35
36 FILE *audio_file;
37 static int header_len;
38 static char *header;
39 static ssize_t *chunk_table;
40 static struct audio_format_handler *af;
41
42 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
43 {
44         FILE *f = datasource;
45         return fread(buf, size, nmemb, f);
46 }
47
48 static int cb_seek(__a_unused void *datasource, ogg_int64_t offset,
49                 int whence)
50 {
51         FILE *f = datasource;
52         return fseek(f, offset, whence);
53 }
54
55 /* don't do anything as vss still needs the open filehandle */
56 static int cb_close(__a_unused void *datasource)
57 {
58         return 0;
59 }
60
61 long cb_tell(void *datasource)
62 {
63         FILE *f = datasource;
64         return ftell(f);
65 }
66
67 int ogg_open_callbacks(void *datasource, OggVorbis_File *vf, ov_callbacks c)
68 {
69         int ret = ov_open_callbacks(datasource, vf,
70                 NULL, /* no initial buffer */
71                 0, /* no initial bytes */
72                 c); /* the ov_open_callbacks */
73
74         /* FIXME: provide better error codes */
75         if (ret == OV_EREAD)
76                 return -E_OGG_READ;
77         if (ret == OV_ENOTVORBIS)
78                 return -E_OGG_READ;
79         if (ret == OV_EVERSION)
80                 return -E_OGG_READ;
81         if (ret == OV_EBADHEADER)
82                 return -E_OGG_READ;
83         if (ret < 0)
84                 return -E_OGG_READ;
85         return 1;
86
87 }
88
89 static int ogg_compute_header_len(FILE *file)
90 {
91         int ret, len, in = fileno(file);
92         unsigned int serial;
93         char *buf;
94         ogg_page page;
95         ogg_packet packet;
96         vorbis_comment vc;
97         vorbis_info vi;
98         ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
99         ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
100         ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
101
102         ogg_sync_init(sync_in);
103         vorbis_info_init(&vi);
104         vorbis_comment_init(&vc);
105         buf = ogg_sync_buffer(sync_in, CHUNK_SIZE);
106         len = read(in, buf, CHUNK_SIZE);
107         ret = -E_OGG_READ;
108         if (len <= 0)
109                 goto err1;
110         ogg_sync_wrote(sync_in, len);
111         ret = -E_SYNC_PAGEOUT;
112         if (ogg_sync_pageout(sync_in, &page) <= 0)
113                 goto err1;
114         serial = ogg_page_serialno(&page);
115         ogg_stream_init(stream_in, serial);
116         ogg_stream_init(stream_out, serial);
117         ret = ogg_stream_pagein(stream_in, &page);
118         if (ret < 0) {
119                 ret = E_STREAM_PAGEIN;
120                 goto err2;
121         }
122         ret = ogg_stream_packetout(stream_in, &packet);
123         if (ret != 1) {
124                 ret = -E_STREAM_PACKETOUT;
125                 goto err2;
126         }
127         ret = -E_VORBIS;
128         if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
129                 goto err2;
130         PARA_INFO_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
131         ogg_stream_packetin(stream_out, &packet);
132
133         ret = ogg_sync_pageout(sync_in, &page);
134         if (ret <= 0) {
135                 ret = -E_SYNC_PAGEOUT;
136                 goto err2;
137         }
138         ogg_stream_pagein(stream_in, &page);
139         ogg_stream_packetout(stream_in, &packet);
140         ogg_stream_packetin(stream_out, &packet);
141
142         ret = ogg_sync_pageout(sync_in, &page);
143         if (ret <= 0) {
144                 ret = -E_SYNC_PAGEOUT;
145                 goto err2;
146         }
147         ogg_stream_pagein(stream_in, &page);
148         ogg_stream_packetout(stream_in, &packet);
149         ogg_stream_packetin(stream_out, &packet);
150
151         header_len = 0;
152         while (ogg_stream_flush(stream_out, &page))
153                 header_len += page.body_len + page.header_len;
154         ret = len;
155         PARA_INFO_LOG("header_len = %d\n", header_len);
156 err2:
157         ogg_stream_destroy(stream_in);
158         ogg_stream_destroy(stream_out);
159 err1:
160         ogg_sync_destroy(sync_in);
161         vorbis_info_clear(&vi);
162         vorbis_comment_clear(&vc);
163         return ret;
164 }
165
166 static void tunetable(long unsigned num_chunks)
167 {
168         int i = 1, j = -1, lp = 1;
169         while (i < num_chunks) {
170                 if (chunk_table[i] == chunk_table[lp]) {
171                         i++;
172                         continue;
173                 }
174                 if (j < 0)
175                         tv_scale(i, &af->chunk_tv, &af->eof_tv);
176                 for (j = lp; j < i; j++)
177                         chunk_table[j] = chunk_table[i];
178                 lp = i;
179         }
180 #if 1
181         for (i = 2; i < num_chunks; i++)
182                 if (chunk_table[i] != chunk_table[1])
183                         break;
184         lp = i;
185         for (i = 2; i < num_chunks - lp; i++)
186                 chunk_table[i] = chunk_table[i + lp];
187 #endif
188 }
189
190
191 /*
192  * Alloc and fill array table of byte offsets. chunk_table[i] is the
193  * offset in the current input file at which the sample containing time i *
194  * CHUNK_TIME begins. Always successful.
195  */
196 static long unsigned ogg_compute_chunk_table(OggVorbis_File *of,
197         double time_total)
198 {
199         int i, ret, num;
200         ssize_t max_chunk_len, pos = 0, min = 0, old_pos;
201         long unsigned num_chunks;
202
203         old_pos = 0;
204         ret = 0;
205         num = time_total / chunk_time + 3;
206         PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
207                 chunk_time, num);
208         chunk_table = para_malloc(num * sizeof(size_t));
209         chunk_table[0] = 0;
210         max_chunk_len = 0;
211         for (i = 1; ret == 0; i++) {
212                 ogg_int64_t diff;
213                 ret = ov_time_seek(of, i * chunk_time);
214                 if (ret)
215                         break;
216                 pos = ov_raw_tell(of);
217                 diff = pos - old_pos;
218                 max_chunk_len = PARA_MAX(max_chunk_len, diff);
219                 min = (i == 1)? diff : PARA_MIN(min, diff);
220                 chunk_table[i] = pos;
221                 if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
222                         PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
223                                 "size: %zd\n", i - 1,
224                                 i * chunk_time, pos, pos - old_pos);
225                 old_pos = pos;
226         }
227         num_chunks = i - 1;
228         chunk_table[i] = pos;
229 //      tunetable(num_chunks);
230         PARA_INFO_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
231                 num_chunks, chunk_time, max_chunk_len, min);
232         return num_chunks;
233 }
234
235 static void ogg_close_audio_file(void)
236 {
237         free(header);
238         free(chunk_table);
239         fclose(audio_file);
240 }
241
242 static int ogg_save_header(FILE *file, int len)
243 {
244         int ret;
245
246         header = para_malloc(len);
247         rewind(file);
248         ret = read(fileno(file), header, len);
249         if (ret == len)
250                 return 1;
251         free(header);
252         return -E_OGG_READ;
253 }
254
255 /*
256  * Init oggvorbis file and write some tech data to given pointers.
257  */
258 static int ogg_get_file_info(FILE *file, char *info_str, long unsigned *frames,
259         int *seconds, size_t **vss_chunk_table)
260 {
261         int ret;
262         double time_total;
263         vorbis_info *vi;
264         ogg_int64_t raw_total;
265         long vi_sampling_rate, vi_bitrate;
266         OggVorbis_File of;
267         static const ov_callbacks ovc = {
268                 .read_func = cb_read,
269                 .seek_func = cb_seek,
270                 .close_func = cb_close,
271                 .tell_func = cb_tell
272         };
273
274         if (!file)
275                 return -E_OGG_NO_FILE;
276         ret = ogg_compute_header_len(file);
277         if (ret < 0)
278                 return ret;
279         ret = ogg_save_header(file, header_len);
280         if (ret < 0)
281                 return ret;
282         rewind(file);
283         ret = ogg_open_callbacks(file, &of, ovc);
284         if (ret < 0)
285                 goto err;
286         ret = -E_OGG_INFO;
287         vi = ov_info(&of, 0);
288         if (!vi)
289                 goto err;
290         time_total = ov_time_total(&of, -1);
291         raw_total = ov_raw_total(&of, -1);
292         *seconds = time_total;
293         vi_sampling_rate = vi->rate;
294         vi_bitrate = ov_bitrate(&of, 0);
295         rewind(file);
296         *frames = ogg_compute_chunk_table(&of, time_total);
297         rewind(file);
298         *vss_chunk_table = chunk_table;
299         sprintf(info_str, "audio_file_info1:%lu x %lu, %ldkHz, %d channels, %ldkbps\n"
300                 "audio_file_info2: \n"
301                 "audio_file_info3: \n",
302                 *frames, (long unsigned) (chunk_time * 1000 * 1000),
303                 vi_sampling_rate / 1000, vi->channels, vi_bitrate / 1000
304                 );
305         rewind(file);
306         audio_file = file;
307         ret = 1;
308 err:
309         ov_clear(&of); /* keeps the file open */
310         if (ret < 0)
311                 free(header);
312         return ret;
313 }
314
315 static char *ogg_get_header_info(int *len)
316 {
317         *len = header_len;
318         return header;
319 }
320
321 static const char* ogg_suffixes[] = {"ogg", NULL};
322
323 /**
324  * the init function of the ogg vorbis audio format handler
325  *
326  * \param p pointer to the struct to initialize
327  */
328 void ogg_init(struct audio_format_handler *p)
329 {
330         af = p;
331         af->get_file_info = ogg_get_file_info,
332         af->close_audio_file = ogg_close_audio_file;
333         af->get_header_info = ogg_get_header_info;
334         af->chunk_tv.tv_sec = 0;
335         af->chunk_tv.tv_usec = 250 * 1000;
336         tv_scale(3, &af->chunk_tv, &af->eof_tv);
337         af->suffixes = ogg_suffixes;
338 }