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