kill some more unneeded includes
[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 struct ogg_datasource {
34 char *map;
35 off_t numbytes;
36 off_t fpos;
37 };
38
39 static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
40 {
41 struct ogg_datasource *ods = datasource;
42 size_t copy = PARA_MIN(ods->numbytes - ods->fpos, size * nmemb),
43 ret = copy / size;
44 if (!ret)
45 return 0;
46 memcpy(buf, ods->map + ods->fpos, copy);
47 // PARA_INFO_LOG("size: %zd, nmemb: %zd, ret: %zd\n", size, nmemb, ret);
48 ods->fpos += ret * size;
49 return ret;
50 }
51
52 static int cb_seek(void *datasource, ogg_int64_t offset,
53 int whence)
54 {
55 struct ogg_datasource *ods = datasource;
56 switch (whence) {
57 case SEEK_SET:
58 if (offset >= 0 && offset <= ods->numbytes) {
59 ods->fpos = offset;
60 return 0;
61 }
62 errno = EINVAL;
63 return -1;
64 break;
65 case SEEK_END:
66 if (offset <= 0 && -offset <= ods->numbytes) {
67 ods->fpos = ods->numbytes + offset;
68 return 0;
69 }
70 errno = EINVAL;
71 return -1;
72 break;
73 case SEEK_CUR:
74 if ((offset >= 0 && offset + ods->fpos > ods->numbytes) ||
75 (offset < 0 && offset + ods->fpos < 0)) {
76 errno = EINVAL;
77 return -1;
78 }
79 ods->fpos += offset;
80 return 0;
81 }
82 errno = EINVAL;
83 return -1;
84 }
85
86 /* don't do anything as vss still needs the open filehandle */
87 static int cb_close(__a_unused void *datasource)
88 {
89 return 0;
90 }
91
92 static long cb_tell(void *datasource)
93 {
94 struct ogg_datasource *ods = datasource;
95 return (unsigned long)ods->fpos;
96 }
97
98 static int ogg_open_callbacks(void *datasource, OggVorbis_File *vf, ov_callbacks c)
99 {
100 int ret = ov_open_callbacks(datasource, vf,
101 NULL, /* no initial buffer */
102 0, /* no initial bytes */
103 c); /* the ov_open_callbacks */
104
105 /* FIXME: provide better error codes */
106 if (ret == OV_EREAD)
107 return -E_OGG_READ;
108 if (ret == OV_ENOTVORBIS)
109 return -E_OGG_READ;
110 if (ret == OV_EVERSION)
111 return -E_OGG_READ;
112 if (ret == OV_EBADHEADER)
113 return -E_OGG_READ;
114 if (ret < 0)
115 return -E_OGG_READ;
116 return 1;
117
118 }
119
120 static void ogg_save_header(char *map, struct audio_format_info *afi)
121 {
122 afi->header = para_malloc(afi->header_len);
123 memcpy(afi->header, map, afi->header_len);
124 }
125
126 static int ogg_compute_header_len(char *map, off_t numbytes,
127 struct audio_format_info *afi)
128 {
129 int ret, len = PARA_MIN(numbytes, CHUNK_SIZE);
130 unsigned int serial;
131 char *buf;
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, len);
144 memcpy(buf, map, len);
145 ogg_sync_wrote(sync_in, 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 ogg_save_header(map, afi);
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, double 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, off_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 if (ret < 0)
291 free(afi->header);
292 return ret;
293 }
294
295 static const char* ogg_suffixes[] = {"ogg", NULL};
296
297 /**
298 * the init function of the ogg vorbis audio format handler
299 *
300 * \param afh pointer to the struct to initialize
301 */
302 void ogg_init(struct audio_format_handler *afh)
303 {
304 afh->get_file_info = ogg_get_file_info,
305 afh->suffixes = ogg_suffixes;
306 }