94383c4564156cb7f608c9eaa9a7b6b5e8846614
[paraslash.git] / ogg_afh.c
1 /*
2  * Copyright (C) 2004-2007 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18 /** \file ogg_afh.c para_server's ogg vorbis audio format handler */
19
20 #include <inttypes.h>
21 #include <ogg/ogg.h>
22 #include <vorbis/codec.h>
23 #include <vorbis/vorbisfile.h>
24
25 #include "server.h"
26 #include "error.h"
27 #include "string.h"
28
29 /** must be big enough to hold header */
30 #define CHUNK_SIZE 32768
31 static double chunk_time = 0.25;
32
33 /** describes a memory-mapped ogg vorbis file */
34 struct ogg_datasource {
35         /** the memory mapping */
36         char *map;
37         /** this size of the mapping */
38         off_t numbytes;
39         /** the current position in the mapping */
40         off_t fpos;
41 };
42
43 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
44 {
45         struct ogg_datasource *ods = datasource;
46         size_t copy = PARA_MIN(ods->numbytes - ods->fpos, size * nmemb),
47                 ret = copy / size;
48         if (!ret)
49                 return 0;
50         memcpy(buf, ods->map + ods->fpos, copy);
51 //      PARA_INFO_LOG("size: %zd, nmemb: %zd, ret: %zd\n", size, nmemb, ret);
52         ods->fpos += ret * size;
53         return ret;
54 }
55
56 static int cb_seek(void *datasource, ogg_int64_t offset,
57                 int whence)
58 {
59         struct ogg_datasource *ods = datasource;
60         switch (whence) {
61         case SEEK_SET:
62                 if (offset >= 0 && offset <= ods->numbytes) {
63                         ods->fpos = offset;
64                         return 0;
65                 }
66                 errno = EINVAL;
67                 return -1;
68                 break;
69         case SEEK_END:
70                 if (offset <= 0 && -offset <= ods->numbytes) {
71                         ods->fpos = ods->numbytes + offset;
72                         return 0;
73                 }
74                 errno = EINVAL;
75                 return -1;
76                 break;
77         case SEEK_CUR:
78                 if ((offset >= 0 && offset + ods->fpos > ods->numbytes) ||
79                                 (offset < 0 && offset + ods->fpos < 0)) {
80                         errno = EINVAL;
81                         return -1;
82                 }
83                 ods->fpos += offset;
84                 return 0;
85         }
86         errno = EINVAL;
87         return -1;
88 }
89
90 /* don't do anything as vss still needs the open filehandle */
91 static int cb_close(__a_unused void *datasource)
92 {
93         return 0;
94 }
95
96 static long cb_tell(void *datasource)
97 {
98         struct ogg_datasource *ods = datasource;
99         return (unsigned long)ods->fpos;
100 }
101
102 static int ogg_open_callbacks(void *datasource, OggVorbis_File *vf, ov_callbacks c)
103 {
104         int ret = ov_open_callbacks(datasource, vf,
105                 NULL, /* no initial buffer */
106                 0, /* no initial bytes */
107                 c); /* the ov_open_callbacks */
108
109         if (ret == OV_EREAD)
110                 return -E_OGG_READ;
111         if (ret == OV_ENOTVORBIS)
112                 return -E_VORBIS;
113         if (ret == OV_EVERSION)
114                 return -E_OGG_VERSION;
115         if (ret == OV_EBADHEADER)
116                 return -E_OGG_BAD_HEADER;
117         if (ret < 0)
118                 return -E_OGG_UNKNOWN_ERROR;
119         return 1;
120
121 }
122
123 static int ogg_compute_header_len(char *map, size_t numbytes,
124                 struct audio_format_info *afi)
125 {
126         int ret;
127         size_t len = PARA_MIN(numbytes, CHUNK_SIZE);
128         int serial;
129         char *buf;
130
131         ogg_page page;
132         ogg_packet packet;
133         vorbis_comment vc;
134         vorbis_info vi;
135         ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
136         ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
137         ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
138
139         ogg_sync_init(sync_in);
140         vorbis_info_init(&vi);
141         vorbis_comment_init(&vc);
142         buf = ogg_sync_buffer(sync_in, (long)len);
143         memcpy(buf, map, len);
144         ogg_sync_wrote(sync_in, (long)len);
145         ret = -E_SYNC_PAGEOUT;
146         if (ogg_sync_pageout(sync_in, &page) <= 0)
147                 goto err1;
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_INFO_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
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         ret = ogg_sync_pageout(sync_in, &page);
177         if (ret <= 0) {
178                 ret = -E_SYNC_PAGEOUT;
179                 goto err2;
180         }
181         ogg_stream_pagein(stream_in, &page);
182         ogg_stream_packetout(stream_in, &packet);
183         ogg_stream_packetin(stream_out, &packet);
184
185         afi->header_len = 0;
186         while (ogg_stream_flush(stream_out, &page))
187                 afi->header_len += page.body_len + page.header_len;
188         PARA_INFO_LOG("header_len = %d\n", afi->header_len);
189         afi->header_offset = 0;
190         ret = 1;
191 err2:
192         ogg_stream_destroy(stream_in);
193         ogg_stream_destroy(stream_out);
194 err1:
195         ogg_sync_destroy(sync_in);
196         vorbis_info_clear(&vi);
197         vorbis_comment_clear(&vc);
198         return ret;
199 }
200
201 /*
202  * Alloc and fill array table of byte offsets. chunk_table[i] is the
203  * offset in the current input file at which the sample containing time i *
204  * CHUNK_TIME begins. Always successful.
205  */
206 static long unsigned ogg_compute_chunk_table(OggVorbis_File *of,
207         struct audio_format_info *afi, long unsigned time_total)
208 {
209         int i, ret, num;
210         ssize_t max_chunk_len, pos = 0, min = 0, old_pos;
211         long unsigned num_chunks;
212
213         old_pos = 0;
214         ret = 0;
215         num = time_total / chunk_time + 3;
216         PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
217                 chunk_time, num);
218         afi->chunk_table = para_malloc((num + 1) * sizeof(size_t));
219         afi->chunk_table[0] = 0;
220         max_chunk_len = 0;
221         for (i = 1; ret <= num; i++) {
222                 ogg_int64_t diff;
223                 ret = ov_time_seek(of, i * chunk_time);
224                 if (ret)
225                         break;
226                 pos = ov_raw_tell(of);
227                 diff = pos - old_pos;
228                 max_chunk_len = PARA_MAX(max_chunk_len, diff);
229                 min = (i == 1)? diff : PARA_MIN(min, diff);
230                 afi->chunk_table[i] = pos;
231 //              if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
232 //                      PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
233 //                              "size: %zd\n", i - 1,
234 //                              i * chunk_time, pos, pos - old_pos);
235                 old_pos = pos;
236         }
237         num_chunks = i - 1;
238 //fi->chunk_table[i] = pos;
239         PARA_INFO_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
240                 num_chunks, chunk_time, max_chunk_len, min);
241         return num_chunks;
242 }
243
244 /*
245  * Init oggvorbis file and write some tech data to given pointers.
246  */
247 static int ogg_get_file_info(char *map, size_t numbytes,
248                 struct audio_format_info *afi)
249 {
250         int ret;
251         vorbis_info *vi;
252         OggVorbis_File of;
253         const ov_callbacks ovc = {
254                 .read_func = cb_read,
255                 .seek_func = cb_seek,
256                 .close_func = cb_close,
257                 .tell_func = cb_tell
258         };
259         struct ogg_datasource ods = {.map = map, .numbytes = numbytes, .fpos = 0};
260
261         ret = ogg_compute_header_len(map, numbytes, afi);
262         if (ret < 0)
263                 return ret;
264         ret = ogg_open_callbacks(&ods, &of, ovc);
265         if (ret < 0)
266                 goto err;
267         ret = -E_OGG_INFO;
268         vi = ov_info(&of, 0);
269         if (!vi)
270                 goto err;
271         afi->seconds_total = ov_time_total(&of, -1);
272         afi->frequency = vi->rate;
273         afi->bitrate = ov_bitrate(&of, 0);
274         afi->channels = vi->channels;
275         afi->chunks_total = ogg_compute_chunk_table(&of, afi, afi->seconds_total);
276         sprintf(afi->info_string, "audio_file_info1:%lu x %lu, %ukHz, "
277                 "%d channels, %ukbps\n"
278                 "audio_file_info2: \n"
279                 "audio_file_info3: \n",
280                 afi->chunks_total, (long unsigned) (chunk_time * 1000 * 1000),
281                 afi->frequency / 1000, vi->channels, afi->bitrate / 1000
282                 );
283         afi->chunk_tv.tv_sec = 0;
284         afi->chunk_tv.tv_usec = 250 * 1000;
285         tv_scale(3, &afi->chunk_tv, &afi->eof_tv);
286         ret = 1;
287 err:
288         ov_clear(&of); /* keeps the file open */
289         return ret;
290 }
291
292 static const char* ogg_suffixes[] = {"ogg", NULL};
293
294 /**
295  * the init function of the ogg vorbis audio format handler
296  *
297  * \param afh pointer to the struct to initialize
298  */
299 void ogg_init(struct audio_format_handler *afh)
300 {
301         afh->get_file_info = ogg_get_file_info,
302         afh->suffixes = ogg_suffixes;
303 }