2 * Copyright (C) 2004-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 /** \file ogg_afh.c para_server's ogg vorbis audio format handler */
10 #include <vorbis/codec.h>
11 #include <vorbis/vorbisfile.h>
21 /** must be big enough to hold header */
22 #define CHUNK_SIZE 32768
23 static double chunk_time = 0.25;
25 /** describes a memory-mapped ogg vorbis file */
26 struct ogg_datasource {
27 /** the memory mapping */
29 /** this size of the mapping */
31 /** the current position in the mapping */
35 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
37 struct ogg_datasource *ods = datasource;
43 assert(ods->numbytes >= ods->fpos);
44 ret = ods->numbytes - ods->fpos;
45 copy = PARA_MIN(ret, size * nmemb);
49 memcpy(buf, ods->map + ods->fpos, copy);
50 // PARA_INFO_LOG("size: %zd, nmemb: %zd, ret: %zd\n", size, nmemb, ret);
51 ods->fpos += ret * size;
55 static int cb_seek(void *datasource, ogg_int64_t offset,
58 struct ogg_datasource *ods = datasource;
61 if (offset >= 0 && offset <= ods->numbytes) {
69 if (offset <= 0 && -offset <= ods->numbytes) {
70 ods->fpos = ods->numbytes + offset;
77 if ((offset >= 0 && offset + ods->fpos > ods->numbytes) ||
78 (offset < 0 && offset + ods->fpos < 0)) {
89 /* don't do anything as vss still needs the open filehandle */
90 static int cb_close(__a_unused void *datasource)
95 static long cb_tell(void *datasource)
97 struct ogg_datasource *ods = datasource;
98 return (unsigned long)ods->fpos;
101 static int ogg_open_callbacks(void *datasource, OggVorbis_File *vf, ov_callbacks c)
103 int ret = ov_open_callbacks(datasource, vf,
104 NULL, /* no initial buffer */
105 0, /* no initial bytes */
106 c); /* the ov_open_callbacks */
110 if (ret == OV_ENOTVORBIS)
112 if (ret == OV_EVERSION)
113 return -E_OGG_VERSION;
114 if (ret == OV_EBADHEADER)
115 return -E_OGG_BAD_HEADER;
117 return -E_OGG_UNKNOWN_ERROR;
122 static int ogg_compute_header_len(char *map, size_t numbytes,
123 struct afh_info *afhi)
126 size_t len = PARA_MIN(numbytes, (size_t)CHUNK_SIZE);
134 ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
135 ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
136 ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
138 ogg_sync_init(sync_in);
139 vorbis_info_init(&vi);
140 vorbis_comment_init(&vc);
141 buf = ogg_sync_buffer(sync_in, (long)len);
142 memcpy(buf, map, len);
143 ogg_sync_wrote(sync_in, (long)len);
144 ret = -E_SYNC_PAGEOUT;
145 if (ogg_sync_pageout(sync_in, &page) <= 0) {
150 serial = ogg_page_serialno(&page);
151 ogg_stream_init(stream_in, serial);
152 ogg_stream_init(stream_out, serial);
153 ret = ogg_stream_pagein(stream_in, &page);
155 ret = -E_STREAM_PAGEIN;
158 ret = ogg_stream_packetout(stream_in, &packet);
160 ret = -E_STREAM_PACKETOUT;
164 if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
166 PARA_DEBUG_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
167 ogg_stream_packetin(stream_out, &packet);
168 ret = ogg_sync_pageout(sync_in, &page);
170 ret = -E_SYNC_PAGEOUT;
173 ogg_stream_pagein(stream_in, &page);
174 ogg_stream_packetout(stream_in, &packet);
175 ogg_stream_packetin(stream_out, &packet);
177 ret = ogg_sync_pageout(sync_in, &page);
179 ret = -E_SYNC_PAGEOUT;
182 ogg_stream_pagein(stream_in, &page);
183 ogg_stream_packetout(stream_in, &packet);
184 ogg_stream_packetin(stream_out, &packet);
186 afhi->header_len = 0;
187 while (ogg_stream_flush(stream_out, &page))
188 afhi->header_len += page.body_len + page.header_len;
189 PARA_DEBUG_LOG("header_len = %d\n", afhi->header_len);
190 afhi->header_offset = 0;
193 ogg_stream_destroy(stream_in);
194 ogg_stream_destroy(stream_out);
196 ogg_sync_destroy(sync_in);
197 vorbis_info_clear(&vi);
198 vorbis_comment_clear(&vc);
203 * Alloc and fill array table of byte offsets. chunk_table[i] is the
204 * offset in the current input file at which the sample containing time i *
205 * CHUNK_TIME begins. Always successful.
207 static long unsigned ogg_compute_chunk_table(OggVorbis_File *of,
208 struct afh_info *afhi, long unsigned time_total)
211 long unsigned num_chunks;
212 ogg_int64_t max = 0, min = 0, old_pos = 0;
214 num = time_total / chunk_time + 3;
215 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
217 afhi->chunk_table = para_malloc((num + 1) * sizeof(size_t));
218 afhi->chunk_table[0] = 0;
219 for (i = 1; i <= num; i++) {
220 ogg_int64_t diff, pos;
221 ret = ov_time_seek(of, i * chunk_time);
224 pos = ov_raw_tell(of);
225 diff = pos - old_pos;
226 max = PARA_MAX(max, diff);
227 min = (i == 1)? diff : PARA_MIN(min, diff);
228 afhi->chunk_table[i] = pos;
232 PARA_DEBUG_LOG("%lu chunks (%fs), max chunk: %lld, min chunk: %lld\n",
233 num_chunks, chunk_time, (long long)max, (long long)min);
237 static void ogg_get_vorbis_comments(OggVorbis_File *vf, struct afh_info *afhi)
239 vorbis_comment *vc = ov_comment(vf,-1);
243 afhi->tags.artist = para_strdup(vorbis_comment_query(vc, "artist", 0));
244 afhi->tags.title = para_strdup(vorbis_comment_query(vc, "title", 0));
245 afhi->tags.album = para_strdup(vorbis_comment_query(vc, "album", 0));
246 afhi->tags.year = para_strdup(vorbis_comment_query(vc, "year", 0));
247 afhi->tags.comment = para_strdup(vorbis_comment_query(vc, "comment", 0));
251 * Init oggvorbis file and write some tech data to given pointers.
253 static int ogg_get_file_info(char *map, size_t numbytes, __a_unused int fd,
254 struct afh_info *afhi)
259 const ov_callbacks ovc = {
260 .read_func = cb_read,
261 .seek_func = cb_seek,
262 .close_func = cb_close,
265 struct ogg_datasource ods = {.map = map, .numbytes = numbytes, .fpos = 0};
267 ret = ogg_compute_header_len(map, numbytes, afhi);
270 ret = ogg_open_callbacks(&ods, &of, ovc);
274 vi = ov_info(&of, 0);
277 afhi->seconds_total = ov_time_total(&of, -1);
278 afhi->frequency = vi->rate;
279 afhi->bitrate = ov_bitrate(&of, 0) / 1000;
280 afhi->channels = vi->channels;
281 afhi->chunks_total = ogg_compute_chunk_table(&of, afhi, afhi->seconds_total);
282 afhi->chunk_tv.tv_sec = 0;
283 afhi->chunk_tv.tv_usec = 250 * 1000;
284 ogg_get_vorbis_comments(&of, afhi);
287 ov_clear(&of); /* keeps the file open */
291 static const char* ogg_suffixes[] = {"ogg", NULL};
294 * the init function of the ogg vorbis audio format handler
296 * \param afh pointer to the struct to initialize
298 void ogg_init(struct audio_format_handler *afh)
300 afh->get_file_info = ogg_get_file_info,
301 afh->suffixes = ogg_suffixes;