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