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