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