40aa6b64e9e81d58345ee4039ca68cc6a466c764
[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 "afh.h"
16 #include "error.h"
17 #include "string.h"
18 #include "afs.h"
19 #include "server.h"
20
21 /** must be big enough to hold header */
22 #define CHUNK_SIZE 32768
23 static double chunk_time = 0.25;
24
25 /** describes a memory-mapped ogg vorbis file */
26 struct ogg_datasource {
27         /** the memory mapping */
28         char *map;
29         /** this size of the mapping */
30         off_t numbytes;
31         /** the current position in the mapping */
32         off_t fpos;
33 };
34
35 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
36 {
37         struct ogg_datasource *ods = datasource;
38         size_t copy, ret;
39
40         if (!size)
41                 return 0;
42
43         assert(ods->numbytes >= ods->fpos);
44         ret = ods->numbytes - ods->fpos;
45         copy = PARA_MIN(ret, size * nmemb);
46         ret = copy / size;
47         if (!ret)
48                 return 0;
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;
52         return ret;
53 }
54
55 static int cb_seek(void *datasource, ogg_int64_t offset,
56                 int whence)
57 {
58         struct ogg_datasource *ods = datasource;
59         switch (whence) {
60         case SEEK_SET:
61                 if (offset >= 0 && offset <= ods->numbytes) {
62                         ods->fpos = offset;
63                         return 0;
64                 }
65                 errno = EINVAL;
66                 return -1;
67                 break;
68         case SEEK_END:
69                 if (offset <= 0 && -offset <= ods->numbytes) {
70                         ods->fpos = ods->numbytes + offset;
71                         return 0;
72                 }
73                 errno = EINVAL;
74                 return -1;
75                 break;
76         case SEEK_CUR:
77                 if ((offset >= 0 && offset + ods->fpos > ods->numbytes) ||
78                                 (offset < 0 && offset + ods->fpos < 0)) {
79                         errno = EINVAL;
80                         return -1;
81                 }
82                 ods->fpos += offset;
83                 return 0;
84         }
85         errno = EINVAL;
86         return -1;
87 }
88
89 /* don't do anything as vss still needs the open filehandle */
90 static int cb_close(__a_unused void *datasource)
91 {
92         return 0;
93 }
94
95 static long cb_tell(void *datasource)
96 {
97         struct ogg_datasource *ods = datasource;
98         return (unsigned long)ods->fpos;
99 }
100
101 static int ogg_open_callbacks(void *datasource, OggVorbis_File *vf, ov_callbacks c)
102 {
103         int ret = ov_open_callbacks(datasource, vf,
104                 NULL, /* no initial buffer */
105                 0, /* no initial bytes */
106                 c); /* the ov_open_callbacks */
107
108         if (ret == OV_EREAD)
109                 return -E_OGG_READ;
110         if (ret == OV_ENOTVORBIS)
111                 return -E_VORBIS;
112         if (ret == OV_EVERSION)
113                 return -E_OGG_VERSION;
114         if (ret == OV_EBADHEADER)
115                 return -E_OGG_BAD_HEADER;
116         if (ret < 0)
117                 return -E_OGG_UNKNOWN_ERROR;
118         return 1;
119
120 }
121
122 static int ogg_compute_header_len(char *map, size_t numbytes,
123                 struct afh_info *afhi)
124 {
125         int ret;
126         size_t len = PARA_MIN(numbytes, (size_t)CHUNK_SIZE);
127         int serial;
128         char *buf;
129
130         ogg_page page;
131         ogg_packet packet;
132         vorbis_comment vc;
133         vorbis_info vi;
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));
137
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) {
146                 free(stream_in);
147                 free(stream_out);
148                 goto err1;
149         }
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);
154         if (ret < 0) {
155                 ret = -E_STREAM_PAGEIN;
156                 goto err2;
157         }
158         ret = ogg_stream_packetout(stream_in, &packet);
159         if (ret != 1) {
160                 ret = -E_STREAM_PACKETOUT;
161                 goto err2;
162         }
163         ret = -E_VORBIS;
164         if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
165                 goto err2;
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);
169         if (ret <= 0) {
170                 ret = -E_SYNC_PAGEOUT;
171                 goto err2;
172         }
173         ogg_stream_pagein(stream_in, &page);
174         ogg_stream_packetout(stream_in, &packet);
175         ogg_stream_packetin(stream_out, &packet);
176
177         ret = ogg_sync_pageout(sync_in, &page);
178         if (ret <= 0) {
179                 ret = -E_SYNC_PAGEOUT;
180                 goto err2;
181         }
182         ogg_stream_pagein(stream_in, &page);
183         ogg_stream_packetout(stream_in, &packet);
184         ogg_stream_packetin(stream_out, &packet);
185
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;
191         ret = 1;
192 err2:
193         ogg_stream_destroy(stream_in);
194         ogg_stream_destroy(stream_out);
195 err1:
196         ogg_sync_destroy(sync_in);
197         vorbis_info_clear(&vi);
198         vorbis_comment_clear(&vc);
199         return ret;
200 }
201
202 /*
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.
206  */
207 static long unsigned ogg_compute_chunk_table(OggVorbis_File *of,
208         struct afh_info *afhi, long unsigned time_total)
209 {
210         int i, ret, num;
211         long unsigned num_chunks;
212         ogg_int64_t max = 0, min = 0, old_pos = 0;
213
214         num = time_total / chunk_time + 3;
215         PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
216                 chunk_time, num);
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);
222                 if (ret)
223                         break;
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;
229                 old_pos = pos;
230         }
231         num_chunks = i - 1;
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);
234         return num_chunks;
235 }
236
237 static void ogg_write_info_string(OggVorbis_File *vf, struct afh_info *afhi)
238 {
239         char *taginfo;
240         vorbis_comment *vc = ov_comment(vf,-1);
241
242         if (vc) {
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);
250         } else
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);
256         free(taginfo);
257 }
258
259 /*
260  * Init oggvorbis file and write some tech data to given pointers.
261  */
262 static int ogg_get_file_info(char *map, size_t numbytes, __a_unused int fd,
263                 struct afh_info *afhi)
264 {
265         int ret;
266         vorbis_info *vi;
267         OggVorbis_File of;
268         const ov_callbacks ovc = {
269                 .read_func = cb_read,
270                 .seek_func = cb_seek,
271                 .close_func = cb_close,
272                 .tell_func = cb_tell
273         };
274         struct ogg_datasource ods = {.map = map, .numbytes = numbytes, .fpos = 0};
275
276         ret = ogg_compute_header_len(map, numbytes, afhi);
277         if (ret < 0)
278                 return ret;
279         ret = ogg_open_callbacks(&ods, &of, ovc);
280         if (ret < 0)
281                 goto err;
282         ret = -E_OGG_INFO;
283         vi = ov_info(&of, 0);
284         if (!vi)
285                 goto err;
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);
295         ret = 1;
296 err:
297         ov_clear(&of); /* keeps the file open */
298         return ret;
299 }
300
301 static const char* ogg_suffixes[] = {"ogg", NULL};
302
303 /**
304  * the init function of the ogg vorbis audio format handler
305  *
306  * \param afh pointer to the struct to initialize
307  */
308 void ogg_init(struct audio_format_handler *afh)
309 {
310         afh->get_file_info = ogg_get_file_info,
311         afh->suffixes = ogg_suffixes;
312 }