Rename error handler() to event_handler()
[paraslash.git] / ogg_afh.c
1 /*
2  * Copyright (C) 2004-2006 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 <ogg/ogg.h>
21 #include <vorbis/codec.h>
22 #include <vorbis/vorbisfile.h>
23
24 #include "server.cmdline.h"
25 #include "server.h"
26 #include "afs.h"
27 #include "afh.h"
28 #include "error.h"
29 #include "string.h"
30
31 /* must be big enough to hold header */
32 #define CHUNK_SIZE 32768
33 static double chunk_time = 0.25;
34
35 static OggVorbis_File *oggvorbis_file;
36 static FILE *infile;
37 static int header_len, oggbuf_len, vi_channels;
38 static char *header, *oggbuf;
39 static ssize_t *chunk_table, max_chunk_len;
40 struct audio_format_handler *af;
41 static long vi_sampling_rate, vi_bitrate, vi_bitrate_nominal,
42         num_chunks;
43
44 static int ogg_compute_header_len(void)
45 {
46         int ret, len, in = fileno(infile);
47         unsigned int serial;
48         char *buf;
49         ogg_page page;
50         ogg_packet packet;
51         vorbis_comment vc;
52         vorbis_info vi;
53         ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
54         ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
55         ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
56
57         ogg_sync_init(sync_in);
58         vorbis_info_init(&vi);
59         vorbis_comment_init(&vc);
60         buf = ogg_sync_buffer(sync_in, CHUNK_SIZE);
61         len = read(in, buf, CHUNK_SIZE);
62         ret = -E_OGG_READ;
63         if (len <= 0)
64                 goto err1;
65         ogg_sync_wrote(sync_in, len);
66         ret = -E_SYNC_PAGEOUT;
67         if (ogg_sync_pageout(sync_in, &page) <= 0)
68                 goto err1;
69         serial = ogg_page_serialno(&page);
70         ogg_stream_init(stream_in, serial);
71         ogg_stream_init(stream_out, serial);
72         ret = ogg_stream_pagein(stream_in, &page);
73         if (ret < 0) {
74                 ret = E_STREAM_PAGEIN;
75                 goto err2;
76         }
77         ret = ogg_stream_packetout(stream_in, &packet);
78         if (ret != 1) {
79                 ret = -E_STREAM_PACKETOUT;
80                 goto err2;
81         }
82         ret = -E_VORBIS;
83         if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0) {
84                 goto err2;
85         } else
86                 PARA_INFO_LOG("channels: %i, rate: %li\n", vi.channels,
87                         vi.rate);
88         ogg_stream_packetin(stream_out, &packet);
89
90         ret = ogg_sync_pageout(sync_in, &page);
91         if (ret <= 0) {
92                 ret = -E_SYNC_PAGEOUT;
93                 goto err2;
94         }
95         ogg_stream_pagein(stream_in, &page);
96         ogg_stream_packetout(stream_in, &packet);
97         ogg_stream_packetin(stream_out, &packet);
98
99         ret = ogg_sync_pageout(sync_in, &page);
100         if (ret <= 0) {
101                 ret = -E_SYNC_PAGEOUT;
102                 goto err2;
103         }
104         ogg_stream_pagein(stream_in, &page);
105         ogg_stream_packetout(stream_in, &packet);
106         ogg_stream_packetin(stream_out, &packet);
107
108         header_len = 0;
109         while (ogg_stream_flush(stream_out, &page))
110                 header_len += page.body_len + page.header_len;
111         ret = len;
112         PARA_INFO_LOG("header_len = %d\n", header_len);
113 err2:
114         ogg_stream_destroy(stream_in);
115         ogg_stream_destroy(stream_out);
116 err1:
117         ogg_sync_destroy(sync_in);
118         vorbis_info_clear(&vi);
119         vorbis_comment_clear(&vc);
120         return ret;
121 }
122
123 static void tunetable(void)
124 {
125         int i = 1, j = -1, lp = 1;
126         while (i < num_chunks) {
127                 if (chunk_table[i] == chunk_table[lp]) {
128                         i++;
129                         continue;
130                 }
131                 if (j < 0)
132                         tv_scale(i + 2, &af->chunk_tv, &af->eof_tv);
133                 for (j = lp; j < i; j++)
134                         chunk_table[j] = chunk_table[i];
135                 lp = i;
136         }
137 #if 1
138         for (i = 2; i < num_chunks; i++)
139                 if (chunk_table[i] != chunk_table[1])
140                         break;
141         lp = i;
142         for (i = 2; i < num_chunks - lp; i++)
143                 chunk_table[i] = chunk_table[i + lp];
144 #endif 
145 }
146
147
148 /*
149  * Alloc and fill array table of byte offsets. chunk_table[i] is the
150  * offset in the current infile at which the sample containing time i *
151  * CHUNK_TIME begins.
152  */
153 static void ogg_compute_chunk_table(double time_total)
154 {
155         int i, ret, num;
156         ssize_t pos = 0, min = 0, old_pos;
157
158         old_pos = 0;
159         ret = 0;
160         num = time_total / chunk_time + 3;
161         PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
162                 chunk_time, num);
163         chunk_table = para_malloc(num * sizeof(ogg_int64_t));
164         chunk_table[0] = 0;
165         max_chunk_len = 0;
166         rewind(infile);
167         for (i = 1; ret == 0; i++) {
168                 ogg_int64_t diff;
169                 ret = ov_time_seek(oggvorbis_file, i * chunk_time);
170                 if (ret)
171                         break;
172                 pos = ov_raw_tell(oggvorbis_file);
173                 diff = pos - old_pos;
174                 max_chunk_len = PARA_MAX(max_chunk_len, diff);
175                 min = (i == 1)? diff : PARA_MIN(min, diff);
176                 chunk_table[i] = pos;
177                 if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
178                         PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
179                                 "size: %zd\n", i - 1,
180                                 i * chunk_time, pos, pos - old_pos);
181                 old_pos = pos;
182         }
183         num_chunks = i - 1;
184         chunk_table[i] = pos;
185         tunetable();
186         PARA_INFO_LOG("%li chunks (%fs), max chunk: %zd, min chunk: %zd\n",
187                 num_chunks, chunk_time, max_chunk_len, min);
188         rewind(infile);
189 }
190
191 static void ogg_close_audio_file(void)
192 {
193         if (oggvorbis_file) {
194                 PARA_DEBUG_LOG("%s", "ov_clear\n");
195                 ov_clear(oggvorbis_file);
196                 free(oggvorbis_file);
197                 oggvorbis_file = NULL;
198         }
199         free(header);
200         header = NULL;
201         header_len = 0;
202         free(chunk_table);
203         chunk_table = NULL;
204         num_chunks = 0;
205         free(oggbuf);
206         oggbuf = NULL;
207         oggbuf_len = 0;
208 }
209
210 static int ogg_save_header(FILE *file, int header_len)
211 {
212         int ret;
213
214         header = para_malloc(header_len);
215         rewind(file);
216         ret = read(fileno(file), header, header_len);
217         if (ret != header_len)
218                 return -E_OGG_READ;
219         return 1;
220 }
221
222 /*
223  * Init oggvorbis file and write some tech data to given pointers.
224  */
225 static int ogg_get_file_info(FILE *file, char *info_str, long unsigned *frames,
226         int *seconds)
227 {
228         int ret;
229         double time_total;
230         vorbis_info *vi;
231         ogg_int64_t raw_total;
232
233         infile = file;
234         if (!file)
235                 return -E_OGG_NO_FILE;
236         ret = ogg_compute_header_len();
237         if (ret < 0)
238                 return ret;
239         ret = ogg_save_header(file, header_len);
240         if (ret < 0)
241                 return ret;
242         rewind(file);
243         oggvorbis_file = para_malloc(sizeof(OggVorbis_File));
244         ret = ov_open(file, oggvorbis_file, NULL, 0);
245         if (ret < 0) {
246                 free(oggvorbis_file);
247                 free(header);
248                 return -E_OGG_OPEN;
249         }
250         ret = -E_OGG_INFO;
251         vi = ov_info(oggvorbis_file, 0);
252         if (!vi)
253                 goto err;
254         time_total = ov_time_total(oggvorbis_file, -1);
255         raw_total = ov_raw_total(oggvorbis_file, -1);
256         *seconds = time_total;
257         vi_sampling_rate = vi->rate;
258         vi_bitrate = ov_bitrate(oggvorbis_file, 0);
259         vi_bitrate_nominal = vi->bitrate_nominal;
260         vi_channels = vi->channels;
261         ogg_compute_chunk_table(time_total);
262         *frames = num_chunks;
263         sprintf(info_str, "audio_file_info1:%lu x %lu, %ldkHz, %d channels, %ldkbps\n"
264                 "audio_file_info2: \n"
265                 "audio_file_info3: \n",
266                 num_chunks, (long unsigned) (chunk_time * 1000 * 1000),
267                 vi_sampling_rate / 1000, vi_channels, vi_bitrate / 1000
268                 );
269         rewind(file);
270         return 1;
271 err:
272         ogg_close_audio_file();
273         return ret;
274 }
275
276 /*
277  * Simple stream reposition routine
278  */
279 static int ogg_reposition_stream(long unsigned request)
280 {
281         int ret;
282         long seek;
283
284         seek = chunk_table[request];
285         ret = fseek(infile, seek, SEEK_SET);
286         PARA_DEBUG_LOG("seek to %li returned %d. offset:%li\n", seek,
287                 ret, ftell(infile));
288         return ret < 0? -E_OGG_REPOS : 1;
289 }
290
291 static ogg_int64_t get_chunk_size(long unsigned chunk_num)
292 {
293         ogg_int64_t ret;
294         if (chunk_num >= num_chunks)
295                 return -1;
296         ret = chunk_table[chunk_num + 1] - chunk_table[chunk_num];
297         if (!ret)
298                 return ret;
299 #if 0
300         PARA_DEBUG_LOG("chunk %d: %lli-%lli (%lli bytes)\n",
301                 chunk_num,
302                 chunk_table[chunk_num],
303                 chunk_table[chunk_num + 1] - 1,
304                 ret);
305 #endif
306         return ret;
307 }
308
309 char *ogg_read_chunk(long unsigned current_chunk, ssize_t *len)
310 {
311         int ret;
312         ogg_int64_t cs = get_chunk_size(current_chunk);
313         if (!cs) { /* nothing to send for this run */
314                 *len = 0;
315                 return oggbuf;
316         }
317         if (cs < 0) { /* eof */
318                 *len = 0;
319                 return NULL;
320         }
321         *len = cs;
322         if (!oggbuf || oggbuf_len < *len) {
323                 PARA_INFO_LOG("increasing ogg buffer size (%d -> %zu)\n",
324                         oggbuf_len, *len);
325                 oggbuf = para_realloc(oggbuf, *len);
326                 oggbuf_len = *len;
327         }
328         ret = read(fileno(infile), oggbuf, *len);
329         if (!ret) {
330                 *len = 0;
331                 return NULL;
332         }
333         if (ret < 0) {
334                 *len = -E_OGG_READ;
335                 return NULL;
336         }
337         if (ret != *len)
338                 PARA_WARNING_LOG("short read (%d/%zd)\n", ret, *len);
339         *len = ret;
340         return oggbuf;
341 }
342
343 static char *ogg_get_header_info(int *len)
344 {
345         *len = header_len;
346         return header;
347 }
348
349 static const char* ogg_suffixes[] = {"ogg", NULL};
350 void ogg_init(struct audio_format_handler *p)
351 {
352         af = p;
353         af->reposition_stream = ogg_reposition_stream;
354         af->get_file_info = ogg_get_file_info,
355         af->read_chunk = ogg_read_chunk;
356         af->close_audio_file = ogg_close_audio_file;
357         af->get_header_info = ogg_get_header_info;
358         af->chunk_tv.tv_sec = 0;
359         af->chunk_tv.tv_usec = 250 * 1000;
360         tv_scale(3, &af->chunk_tv, &af->eof_tv);
361         af->suffixes = ogg_suffixes;
362 }