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