2 * Copyright (C) 2004-2008 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>
20 /** must be big enough to hold header */
21 #define CHUNK_SIZE 32768
22 static double chunk_time = 0.25;
24 /** describes a memory-mapped ogg vorbis file */
25 struct ogg_datasource {
26 /** the memory mapping */
28 /** this size of the mapping */
30 /** the current position in the mapping */
34 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
36 struct ogg_datasource *ods = datasource;
37 size_t copy = PARA_MIN(ods->numbytes - ods->fpos, size * nmemb),
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;
47 static int cb_seek(void *datasource, ogg_int64_t offset,
50 struct ogg_datasource *ods = datasource;
53 if (offset >= 0 && offset <= ods->numbytes) {
61 if (offset <= 0 && -offset <= ods->numbytes) {
62 ods->fpos = ods->numbytes + offset;
69 if ((offset >= 0 && offset + ods->fpos > ods->numbytes) ||
70 (offset < 0 && offset + ods->fpos < 0)) {
81 /* don't do anything as vss still needs the open filehandle */
82 static int cb_close(__a_unused void *datasource)
87 static long cb_tell(void *datasource)
89 struct ogg_datasource *ods = datasource;
90 return (unsigned long)ods->fpos;
93 static int ogg_open_callbacks(void *datasource, OggVorbis_File *vf, ov_callbacks c)
95 int ret = ov_open_callbacks(datasource, vf,
96 NULL, /* no initial buffer */
97 0, /* no initial bytes */
98 c); /* the ov_open_callbacks */
102 if (ret == OV_ENOTVORBIS)
104 if (ret == OV_EVERSION)
105 return -E_OGG_VERSION;
106 if (ret == OV_EBADHEADER)
107 return -E_OGG_BAD_HEADER;
109 return -E_OGG_UNKNOWN_ERROR;
114 static int ogg_compute_header_len(char *map, size_t numbytes,
115 struct afh_info *afhi)
118 size_t len = PARA_MIN(numbytes, CHUNK_SIZE);
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));
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) {
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);
147 ret = -E_STREAM_PAGEIN;
150 ret = ogg_stream_packetout(stream_in, &packet);
152 ret = -E_STREAM_PACKETOUT;
156 if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
158 PARA_DEBUG_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
159 ogg_stream_packetin(stream_out, &packet);
160 ret = ogg_sync_pageout(sync_in, &page);
162 ret = -E_SYNC_PAGEOUT;
165 ogg_stream_pagein(stream_in, &page);
166 ogg_stream_packetout(stream_in, &packet);
167 ogg_stream_packetin(stream_out, &packet);
169 ret = ogg_sync_pageout(sync_in, &page);
171 ret = -E_SYNC_PAGEOUT;
174 ogg_stream_pagein(stream_in, &page);
175 ogg_stream_packetout(stream_in, &packet);
176 ogg_stream_packetin(stream_out, &packet);
178 afhi->header_len = 0;
179 while (ogg_stream_flush(stream_out, &page))
180 afhi->header_len += page.body_len + page.header_len;
181 PARA_DEBUG_LOG("header_len = %d\n", afhi->header_len);
182 afhi->header_offset = 0;
185 ogg_stream_destroy(stream_in);
186 ogg_stream_destroy(stream_out);
188 ogg_sync_destroy(sync_in);
189 vorbis_info_clear(&vi);
190 vorbis_comment_clear(&vc);
195 * Alloc and fill array table of byte offsets. chunk_table[i] is the
196 * offset in the current input file at which the sample containing time i *
197 * CHUNK_TIME begins. Always successful.
199 static long unsigned ogg_compute_chunk_table(OggVorbis_File *of,
200 struct afh_info *afhi, long unsigned time_total)
203 ssize_t max_chunk_len, pos = 0, min = 0, old_pos;
204 long unsigned num_chunks;
208 num = time_total / chunk_time + 3;
209 PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
211 afhi->chunk_table = para_malloc((num + 1) * sizeof(size_t));
212 afhi->chunk_table[0] = 0;
214 for (i = 1; ret <= num; i++) {
216 ret = ov_time_seek(of, i * chunk_time);
219 pos = ov_raw_tell(of);
220 diff = pos - old_pos;
221 max_chunk_len = PARA_MAX(max_chunk_len, diff);
222 min = (i == 1)? diff : PARA_MIN(min, diff);
223 afhi->chunk_table[i] = pos;
224 // if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
225 // PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
226 // "size: %zd\n", i - 1,
227 // i * chunk_time, pos, pos - old_pos);
231 //fi->chunk_table[i] = pos;
232 PARA_DEBUG_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
233 num_chunks, chunk_time, max_chunk_len, min);
237 static void ogg_write_info_string(OggVorbis_File *vf, struct afh_info *afhi)
240 vorbis_comment *vc = ov_comment(vf,-1);
243 char *artist, *title, *album, *year, *comment;
244 artist = vorbis_comment_query(vc, "artist", 0);
245 title = vorbis_comment_query(vc, "title", 0);
246 album = vorbis_comment_query(vc, "album", 0);
247 year = vorbis_comment_query(vc, "year", 0);
248 comment = vorbis_comment_query(vc, "comment", 0);
249 taginfo = make_taginfo(title, artist, album, year, comment);
251 taginfo = make_message("%s: (no vorbis comments found)\n%s:\n",
252 status_item_list[SI_TAGINFO1],
253 status_item_list[SI_TAGINFO2]);
254 afhi->info_string = make_message("%s:\n%s",
255 status_item_list[SI_AUDIO_FILE_INFO], taginfo);
260 * Init oggvorbis file and write some tech data to given pointers.
262 static int ogg_get_file_info(char *map, size_t numbytes, __a_unused int fd,
263 struct afh_info *afhi)
268 const ov_callbacks ovc = {
269 .read_func = cb_read,
270 .seek_func = cb_seek,
271 .close_func = cb_close,
274 struct ogg_datasource ods = {.map = map, .numbytes = numbytes, .fpos = 0};
276 ret = ogg_compute_header_len(map, numbytes, afhi);
279 ret = ogg_open_callbacks(&ods, &of, ovc);
283 vi = ov_info(&of, 0);
286 afhi->seconds_total = ov_time_total(&of, -1);
287 afhi->frequency = vi->rate;
288 afhi->bitrate = ov_bitrate(&of, 0) / 1000;
289 afhi->channels = vi->channels;
290 afhi->chunks_total = ogg_compute_chunk_table(&of, afhi, afhi->seconds_total);
291 afhi->chunk_tv.tv_sec = 0;
292 afhi->chunk_tv.tv_usec = 250 * 1000;
293 tv_scale(10 / afhi->channels, &afhi->chunk_tv, &afhi->eof_tv);
294 ogg_write_info_string(&of, afhi);
297 ov_clear(&of); /* keeps the file open */
301 static const char* ogg_suffixes[] = {"ogg", NULL};
304 * the init function of the ogg vorbis audio format handler
306 * \param afh pointer to the struct to initialize
308 void ogg_init(struct audio_format_handler *afh)
310 afh->get_file_info = ogg_get_file_info,
311 afh->suffixes = ogg_suffixes;