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