Fix an off-by-one bug.
[paraslash.git] / ogg_afh.c
1 /*
2 * Copyright (C) 2004-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 /** \file ogg_afh.c para_server's ogg vorbis audio format handler */
7
8 #include <inttypes.h>
9 #include <ogg/ogg.h>
10 #include <vorbis/codec.h>
11 #include <vorbis/vorbisfile.h>
12
13 #include "para.h"
14 #include "afh.h"
15 #include "error.h"
16 #include "string.h"
17 #include "afs.h"
18 #include "server.h"
19
20 /** must be big enough to hold header */
21 #define CHUNK_SIZE 32768
22 static double chunk_time = 0.25;
23
24 /** describes a memory-mapped ogg vorbis file */
25 struct ogg_datasource {
26 /** the memory mapping */
27 char *map;
28 /** this size of the mapping */
29 off_t numbytes;
30 /** the current position in the mapping */
31 off_t fpos;
32 };
33
34 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
35 {
36 struct ogg_datasource *ods = datasource;
37 size_t copy = PARA_MIN(ods->numbytes - ods->fpos, size * nmemb),
38 ret = copy / size;
39 if (!ret)
40 return 0;
41 memcpy(buf, ods->map + ods->fpos, copy);
42 // PARA_INFO_LOG("size: %zd, nmemb: %zd, ret: %zd\n", size, nmemb, ret);
43 ods->fpos += ret * size;
44 return ret;
45 }
46
47 static int cb_seek(void *datasource, ogg_int64_t offset,
48 int whence)
49 {
50 struct ogg_datasource *ods = datasource;
51 switch (whence) {
52 case SEEK_SET:
53 if (offset >= 0 && offset <= ods->numbytes) {
54 ods->fpos = offset;
55 return 0;
56 }
57 errno = EINVAL;
58 return -1;
59 break;
60 case SEEK_END:
61 if (offset <= 0 && -offset <= ods->numbytes) {
62 ods->fpos = ods->numbytes + offset;
63 return 0;
64 }
65 errno = EINVAL;
66 return -1;
67 break;
68 case SEEK_CUR:
69 if ((offset >= 0 && offset + ods->fpos > ods->numbytes) ||
70 (offset < 0 && offset + ods->fpos < 0)) {
71 errno = EINVAL;
72 return -1;
73 }
74 ods->fpos += offset;
75 return 0;
76 }
77 errno = EINVAL;
78 return -1;
79 }
80
81 /* don't do anything as vss still needs the open filehandle */
82 static int cb_close(__a_unused void *datasource)
83 {
84 return 0;
85 }
86
87 static long cb_tell(void *datasource)
88 {
89 struct ogg_datasource *ods = datasource;
90 return (unsigned long)ods->fpos;
91 }
92
93 static int ogg_open_callbacks(void *datasource, OggVorbis_File *vf, ov_callbacks c)
94 {
95 int ret = ov_open_callbacks(datasource, vf,
96 NULL, /* no initial buffer */
97 0, /* no initial bytes */
98 c); /* the ov_open_callbacks */
99
100 if (ret == OV_EREAD)
101 return -E_OGG_READ;
102 if (ret == OV_ENOTVORBIS)
103 return -E_VORBIS;
104 if (ret == OV_EVERSION)
105 return -E_OGG_VERSION;
106 if (ret == OV_EBADHEADER)
107 return -E_OGG_BAD_HEADER;
108 if (ret < 0)
109 return -E_OGG_UNKNOWN_ERROR;
110 return 1;
111
112 }
113
114 static int ogg_compute_header_len(char *map, size_t numbytes,
115 struct afh_info *afhi)
116 {
117 int ret;
118 size_t len = PARA_MIN(numbytes, CHUNK_SIZE);
119 int serial;
120 char *buf;
121
122 ogg_page page;
123 ogg_packet packet;
124 vorbis_comment vc;
125 vorbis_info vi;
126 ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
127 ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
128 ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
129
130 ogg_sync_init(sync_in);
131 vorbis_info_init(&vi);
132 vorbis_comment_init(&vc);
133 buf = ogg_sync_buffer(sync_in, (long)len);
134 memcpy(buf, map, len);
135 ogg_sync_wrote(sync_in, (long)len);
136 ret = -E_SYNC_PAGEOUT;
137 if (ogg_sync_pageout(sync_in, &page) <= 0) {
138 free(stream_in);
139 free(stream_out);
140 goto err1;
141 }
142 serial = ogg_page_serialno(&page);
143 ogg_stream_init(stream_in, serial);
144 ogg_stream_init(stream_out, serial);
145 ret = ogg_stream_pagein(stream_in, &page);
146 if (ret < 0) {
147 ret = -E_STREAM_PAGEIN;
148 goto err2;
149 }
150 ret = ogg_stream_packetout(stream_in, &packet);
151 if (ret != 1) {
152 ret = -E_STREAM_PACKETOUT;
153 goto err2;
154 }
155 ret = -E_VORBIS;
156 if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
157 goto err2;
158 PARA_DEBUG_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
159 ogg_stream_packetin(stream_out, &packet);
160
161 ret = ogg_sync_pageout(sync_in, &page);
162 if (ret <= 0) {
163 ret = -E_SYNC_PAGEOUT;
164 goto err2;
165 }
166 ogg_stream_pagein(stream_in, &page);
167 ogg_stream_packetout(stream_in, &packet);
168 ogg_stream_packetin(stream_out, &packet);
169
170 ret = ogg_sync_pageout(sync_in, &page);
171 if (ret <= 0) {
172 ret = -E_SYNC_PAGEOUT;
173 goto err2;
174 }
175 ogg_stream_pagein(stream_in, &page);
176 ogg_stream_packetout(stream_in, &packet);
177 ogg_stream_packetin(stream_out, &packet);
178
179 afhi->header_len = 0;
180 while (ogg_stream_flush(stream_out, &page))
181 afhi->header_len += page.body_len + page.header_len;
182 PARA_DEBUG_LOG("header_len = %d\n", afhi->header_len);
183 afhi->header_offset = 0;
184 ret = 1;
185 err2:
186 ogg_stream_destroy(stream_in);
187 ogg_stream_destroy(stream_out);
188 err1:
189 ogg_sync_destroy(sync_in);
190 vorbis_info_clear(&vi);
191 vorbis_comment_clear(&vc);
192 return ret;
193 }
194
195 /*
196 * Alloc and fill array table of byte offsets. chunk_table[i] is the
197 * offset in the current input file at which the sample containing time i *
198 * CHUNK_TIME begins. Always successful.
199 */
200 static long unsigned ogg_compute_chunk_table(OggVorbis_File *of,
201 struct afh_info *afhi, long unsigned time_total)
202 {
203 int i, ret, num;
204 ssize_t max_chunk_len, pos = 0, min = 0, old_pos;
205 long unsigned num_chunks;
206
207 old_pos = 0;
208 ret = 0;
209 num = time_total / chunk_time + 3;
210 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
211 chunk_time, num);
212 afhi->chunk_table = para_malloc((num + 1) * sizeof(size_t));
213 afhi->chunk_table[0] = 0;
214 max_chunk_len = 0;
215 for (i = 1; ret <= num; i++) {
216 ogg_int64_t diff;
217 ret = ov_time_seek(of, i * chunk_time);
218 if (ret)
219 break;
220 pos = ov_raw_tell(of);
221 diff = pos - old_pos;
222 max_chunk_len = PARA_MAX(max_chunk_len, diff);
223 min = (i == 1)? diff : PARA_MIN(min, diff);
224 afhi->chunk_table[i] = pos;
225 // if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
226 // PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
227 // "size: %zd\n", i - 1,
228 // i * chunk_time, pos, pos - old_pos);
229 old_pos = pos;
230 }
231 num_chunks = i - 1;
232 //fi->chunk_table[i] = pos;
233 PARA_DEBUG_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
234 num_chunks, chunk_time, max_chunk_len, min);
235 return num_chunks;
236 }
237
238 /*
239 * Init oggvorbis file and write some tech data to given pointers.
240 */
241 static int ogg_get_file_info(char *map, size_t numbytes,
242 struct afh_info *afhi)
243 {
244 int ret;
245 vorbis_info *vi;
246 OggVorbis_File of;
247 const ov_callbacks ovc = {
248 .read_func = cb_read,
249 .seek_func = cb_seek,
250 .close_func = cb_close,
251 .tell_func = cb_tell
252 };
253 struct ogg_datasource ods = {.map = map, .numbytes = numbytes, .fpos = 0};
254
255 ret = ogg_compute_header_len(map, numbytes, afhi);
256 if (ret < 0)
257 return ret;
258 ret = ogg_open_callbacks(&ods, &of, ovc);
259 if (ret < 0)
260 goto err;
261 ret = -E_OGG_INFO;
262 vi = ov_info(&of, 0);
263 if (!vi)
264 goto err;
265 afhi->seconds_total = ov_time_total(&of, -1);
266 afhi->frequency = vi->rate;
267 afhi->bitrate = ov_bitrate(&of, 0) / 1000;
268 afhi->channels = vi->channels;
269 afhi->chunks_total = ogg_compute_chunk_table(&of, afhi, afhi->seconds_total);
270 sprintf(afhi->info_string, "audio_file_info1:%lu x %lu, %ukHz, "
271 "%d channels, %ukbps\n"
272 "audio_file_info2: \n"
273 "audio_file_info3: \n",
274 afhi->chunks_total, (long unsigned) (chunk_time * 1000 * 1000),
275 afhi->frequency / 1000, vi->channels, afhi->bitrate
276 );
277 afhi->chunk_tv.tv_sec = 0;
278 afhi->chunk_tv.tv_usec = 250 * 1000;
279 tv_scale(3, &afhi->chunk_tv, &afhi->eof_tv);
280 ret = 1;
281 err:
282 ov_clear(&of); /* keeps the file open */
283 return ret;
284 }
285
286 static const char* ogg_suffixes[] = {"ogg", NULL};
287
288 /**
289 * the init function of the ogg vorbis audio format handler
290 *
291 * \param afh pointer to the struct to initialize
292 */
293 void ogg_init(struct audio_format_handler *afh)
294 {
295 afh->get_file_info = ogg_get_file_info,
296 afh->suffixes = ogg_suffixes;
297 }