ogg_afh.c: add documentation of struct ogg_datasource
[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         /* FIXME: provide better error codes */
110         if (ret == OV_EREAD)
111                 return -E_OGG_READ;
112         if (ret == OV_ENOTVORBIS)
113                 return -E_OGG_READ;
114         if (ret == OV_EVERSION)
115                 return -E_OGG_READ;
116         if (ret == OV_EBADHEADER)
117                 return -E_OGG_READ;
118         if (ret < 0)
119                 return -E_OGG_READ;
120         return 1;
121
122 }
123
124 static int ogg_compute_header_len(char *map, size_t numbytes,
125                 struct audio_format_info *afi)
126 {
127         int ret;
128         size_t len = PARA_MIN(numbytes, CHUNK_SIZE);
129         int serial;
130         char *buf;
131
132         ogg_page page;
133         ogg_packet packet;
134         vorbis_comment vc;
135         vorbis_info vi;
136         ogg_stream_state *stream_in = para_malloc(sizeof(ogg_stream_state));
137         ogg_stream_state *stream_out = para_malloc(sizeof(ogg_stream_state));
138         ogg_sync_state *sync_in = para_malloc(sizeof(ogg_sync_state));
139
140         ogg_sync_init(sync_in);
141         vorbis_info_init(&vi);
142         vorbis_comment_init(&vc);
143         buf = ogg_sync_buffer(sync_in, (long)len);
144         memcpy(buf, map, len);
145         ogg_sync_wrote(sync_in, (long)len);
146         ret = -E_SYNC_PAGEOUT;
147         if (ogg_sync_pageout(sync_in, &page) <= 0)
148                 goto err1;
149         serial = ogg_page_serialno(&page);
150         ogg_stream_init(stream_in, serial);
151         ogg_stream_init(stream_out, serial);
152         ret = ogg_stream_pagein(stream_in, &page);
153         if (ret < 0) {
154                 ret = E_STREAM_PAGEIN;
155                 goto err2;
156         }
157         ret = ogg_stream_packetout(stream_in, &packet);
158         if (ret != 1) {
159                 ret = -E_STREAM_PACKETOUT;
160                 goto err2;
161         }
162         ret = -E_VORBIS;
163         if (vorbis_synthesis_headerin(&vi, &vc, &packet) < 0)
164                 goto err2;
165         PARA_INFO_LOG("channels: %i, rate: %li\n", vi.channels, vi.rate);
166         ogg_stream_packetin(stream_out, &packet);
167
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         afi->header_len = 0;
187         while (ogg_stream_flush(stream_out, &page))
188                 afi->header_len += page.body_len + page.header_len;
189         PARA_INFO_LOG("header_len = %d\n", afi->header_len);
190         afi->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 audio_format_info *afi, long unsigned time_total)
209 {
210         int i, ret, num;
211         ssize_t max_chunk_len, pos = 0, min = 0, old_pos;
212         long unsigned num_chunks;
213
214         old_pos = 0;
215         ret = 0;
216         num = time_total / chunk_time + 3;
217         PARA_DEBUG_LOG("chunk time: %g allocating %d chunk pointers\n",
218                 chunk_time, num);
219         afi->chunk_table = para_malloc((num + 1) * sizeof(size_t));
220         afi->chunk_table[0] = 0;
221         max_chunk_len = 0;
222         for (i = 1; ret <= num; i++) {
223                 ogg_int64_t diff;
224                 ret = ov_time_seek(of, i * chunk_time);
225                 if (ret)
226                         break;
227                 pos = ov_raw_tell(of);
228                 diff = pos - old_pos;
229                 max_chunk_len = PARA_MAX(max_chunk_len, diff);
230                 min = (i == 1)? diff : PARA_MIN(min, diff);
231                 afi->chunk_table[i] = pos;
232 //              if (i < 11 || !((i - 1) % 1000)|| i > num - 11)
233 //                      PARA_DEBUG_LOG("chunk #%d: %g secs, pos: %zd, "
234 //                              "size: %zd\n", i - 1,
235 //                              i * chunk_time, pos, pos - old_pos);
236                 old_pos = pos;
237         }
238         num_chunks = i - 1;
239 //fi->chunk_table[i] = pos;
240         PARA_INFO_LOG("%lu chunks (%fs), max chunk: %zd, min chunk: %zd\n",
241                 num_chunks, chunk_time, max_chunk_len, min);
242         return num_chunks;
243 }
244
245 /*
246  * Init oggvorbis file and write some tech data to given pointers.
247  */
248 static int ogg_get_file_info(char *map, size_t numbytes,
249                 struct audio_format_info *afi)
250 {
251         int ret;
252         vorbis_info *vi;
253         OggVorbis_File of;
254         const ov_callbacks ovc = {
255                 .read_func = cb_read,
256                 .seek_func = cb_seek,
257                 .close_func = cb_close,
258                 .tell_func = cb_tell
259         };
260         struct ogg_datasource ods = {.map = map, .numbytes = numbytes, .fpos = 0};
261
262         ret = ogg_compute_header_len(map, numbytes, afi);
263         if (ret < 0)
264                 return ret;
265         ret = ogg_open_callbacks(&ods, &of, ovc);
266         if (ret < 0)
267                 goto err;
268         ret = -E_OGG_INFO;
269         vi = ov_info(&of, 0);
270         if (!vi)
271                 goto err;
272         afi->seconds_total = ov_time_total(&of, -1);
273         afi->frequency = vi->rate;
274         afi->bitrate = ov_bitrate(&of, 0);
275         afi->channels = vi->channels;
276         afi->chunks_total = ogg_compute_chunk_table(&of, afi, afi->seconds_total);
277         sprintf(afi->info_string, "audio_file_info1:%lu x %lu, %ukHz, "
278                 "%d channels, %ukbps\n"
279                 "audio_file_info2: \n"
280                 "audio_file_info3: \n",
281                 afi->chunks_total, (long unsigned) (chunk_time * 1000 * 1000),
282                 afi->frequency / 1000, vi->channels, afi->bitrate / 1000
283                 );
284         afi->chunk_tv.tv_sec = 0;
285         afi->chunk_tv.tv_usec = 250 * 1000;
286         tv_scale(3, &afi->chunk_tv, &afi->eof_tv);
287         ret = 1;
288 err:
289         ov_clear(&of); /* keeps the file open */
290         return ret;
291 }
292
293 static const char* ogg_suffixes[] = {"ogg", NULL};
294
295 /**
296  * the init function of the ogg vorbis audio format handler
297  *
298  * \param afh pointer to the struct to initialize
299  */
300 void ogg_init(struct audio_format_handler *afh)
301 {
302         afh->get_file_info = ogg_get_file_info,
303         afh->suffixes = ogg_suffixes;
304 }