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