Merge /fml/ag-raetsch/home/maan/scm/paraslash_meins/paraslash
[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, off_t numbytes,
121 struct audio_format_info *afi)
122 {
123 int ret, len = PARA_MIN(numbytes, CHUNK_SIZE);
124 unsigned int serial;
125 char *buf;
126 ogg_page page;
127 ogg_packet packet;
128 vorbis_comment vc;
129 vorbis_info vi;
130 ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
131 ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
132 ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
133
134 ogg_sync_init(sync_in);
135 vorbis_info_init(&vi);
136 vorbis_comment_init(&vc);
137 buf = ogg_sync_buffer(sync_in, len);
138 memcpy(buf, map, len);
139 ogg_sync_wrote(sync_in, len);
140 ret = -E_SYNC_PAGEOUT;
141 if (ogg_sync_pageout(sync_in, &page) <= 0)
142 goto err1;
143 serial = ogg_page_serialno(&page);
144 ogg_stream_init(stream_in, serial);
145 ogg_stream_init(stream_out, serial);
146 ret = ogg_stream_pagein(stream_in, &page);
147 if (ret < 0) {
148 ret = E_STREAM_PAGEIN;
149 goto err2;
150 }
151 ret = ogg_stream_packetout(stream_in, &packet);
152 if (ret != 1) {
153 ret = -E_STREAM_PACKETOUT;
154 goto err2;
155 }
156 ret = -E_VORBIS;
157 if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
158 goto err2;
159 PARA_INFO_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
160 ogg_stream_packetin(stream_out, &packet);
161
162 ret = ogg_sync_pageout(sync_in, &page);
163 if (ret <= 0) {
164 ret = -E_SYNC_PAGEOUT;
165 goto err2;
166 }
167 ogg_stream_pagein(stream_in, &page);
168 ogg_stream_packetout(stream_in, &packet);
169 ogg_stream_packetin(stream_out, &packet);
170
171 ret = ogg_sync_pageout(sync_in, &page);
172 if (ret <= 0) {
173 ret = -E_SYNC_PAGEOUT;
174 goto err2;
175 }
176 ogg_stream_pagein(stream_in, &page);
177 ogg_stream_packetout(stream_in, &packet);
178 ogg_stream_packetin(stream_out, &packet);
179
180 afi->header_len = 0;
181 while (ogg_stream_flush(stream_out, &page))
182 afi->header_len += page.body_len + page.header_len;
183 PARA_INFO_LOG("header_len = %d\n", afi->header_len);
184 afi->header_offset = 0;
185 ret = 1;
186 err2:
187 ogg_stream_destroy(stream_in);
188 ogg_stream_destroy(stream_out);
189 err1:
190 ogg_sync_destroy(sync_in);
191 vorbis_info_clear(&vi);
192 vorbis_comment_clear(&vc);
193 return ret;
194 }
195
196 /*
197 * Alloc and fill array table of byte offsets. chunk_table[i] is the
198 * offset in the current input file at which the sample containing time i *
199 * CHUNK_TIME begins. Always successful.
200 */
201 static long unsigned ogg_compute_chunk_table(OggVorbis_File *of,
202 struct audio_format_info *afi, double time_total)
203 {
204 int i, ret, num;
205 ssize_t max_chunk_len, pos = 0, min = 0, old_pos;
206 long unsigned num_chunks;
207
208 old_pos = 0;
209 ret = 0;
210 num = time_total / chunk_time + 3;
211 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
212 chunk_time, num);
213 afi->chunk_table = para_malloc((num + 1) * sizeof(size_t));
214 afi->chunk_table[0] = 0;
215 max_chunk_len = 0;
216 for (i = 1; ret <= num; i++) {
217 ogg_int64_t diff;
218 ret = ov_time_seek(of, i * chunk_time);
219 if (ret)
220 break;
221 pos = ov_raw_tell(of);
222 diff = pos - old_pos;
223 max_chunk_len = PARA_MAX(max_chunk_len, diff);
224 min = (i == 1)? diff : PARA_MIN(min, diff);
225 afi->chunk_table[i] = pos;
226 // if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
227 // PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
228 // "size: %zd\n", i - 1,
229 // i * chunk_time, pos, pos - old_pos);
230 old_pos = pos;
231 }
232 num_chunks = i - 1;
233 //fi->chunk_table[i] = pos;
234 PARA_INFO_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
235 num_chunks, chunk_time, max_chunk_len, min);
236 return num_chunks;
237 }
238
239 /*
240 * Init oggvorbis file and write some tech data to given pointers.
241 */
242 static int ogg_get_file_info(char *map, off_t numbytes,
243 struct audio_format_info *afi)
244 {
245 int ret;
246 vorbis_info *vi;
247 OggVorbis_File of;
248 const ov_callbacks ovc = {
249 .read_func = cb_read,
250 .seek_func = cb_seek,
251 .close_func = cb_close,
252 .tell_func = cb_tell
253 };
254 struct ogg_datasource ods = {.map = map, .numbytes = numbytes, .fpos = 0};
255
256 ret = ogg_compute_header_len(map, numbytes, afi);
257 if (ret < 0)
258 return ret;
259 ret = ogg_open_callbacks(&ods, &of, ovc);
260 if (ret < 0)
261 goto err;
262 ret = -E_OGG_INFO;
263 vi = ov_info(&of, 0);
264 if (!vi)
265 goto err;
266 afi->seconds_total = ov_time_total(&of, -1);
267 afi->frequency = vi->rate;
268 afi->bitrate = ov_bitrate(&of, 0);
269 afi->channels = vi->channels;
270 afi->chunks_total = ogg_compute_chunk_table(&of, afi, afi->seconds_total);
271 sprintf(afi->info_string, "audio_file_info1:%lu x %lu, %ukHz, "
272 "%d channels, %ukbps\n"
273 "audio_file_info2: \n"
274 "audio_file_info3: \n",
275 afi->chunks_total, (long unsigned) (chunk_time * 1000 * 1000),
276 afi->frequency / 1000, vi->channels, afi->bitrate / 1000
277 );
278 afi->chunk_tv.tv_sec = 0;
279 afi->chunk_tv.tv_usec = 250 * 1000;
280 tv_scale(3, &afi->chunk_tv, &afi->eof_tv);
281 ret = 1;
282 err:
283 ov_clear(&of); /* keeps the file open */
284 return ret;
285 }
286
287 static const char* ogg_suffixes[] = {"ogg", NULL};
288
289 /**
290 * the init function of the ogg vorbis audio format handler
291 *
292 * \param afh pointer to the struct to initialize
293 */
294 void ogg_init(struct audio_format_handler *afh)
295 {
296 afh->get_file_info = ogg_get_file_info,
297 afh->suffixes = ogg_suffixes;
298 }