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