1 | //codecfilter_vorbis.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include "roard.h" |
---|
26 | |
---|
27 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
28 | |
---|
29 | int _g_cf_vorbis_vfvio_return_err (void) { |
---|
30 | return -1; |
---|
31 | } |
---|
32 | |
---|
33 | ov_callbacks _g_cf_vorbis_vfvio = { |
---|
34 | .read_func = cf_vorbis_vfvio_read, |
---|
35 | .seek_func = (int (*)(void *, ogg_int64_t, int )) _g_cf_vorbis_vfvio_return_err, |
---|
36 | .close_func = (int (*)(void * )) _g_cf_vorbis_vfvio_return_err, |
---|
37 | .tell_func = (long (*)(void * )) _g_cf_vorbis_vfvio_return_err |
---|
38 | }; |
---|
39 | |
---|
40 | size_t cf_vorbis_vfvio_read (void *ptr, size_t size, size_t nmemb, void *datasource) { |
---|
41 | ssize_t r; |
---|
42 | |
---|
43 | r = stream_vio_s_read(ROAR_STREAM_SERVER(datasource), ptr, size*nmemb); |
---|
44 | |
---|
45 | ROAR_DBG("cf_vorbis_vfvio_read(ptr=%p, size=%lu, nmemb=%lu, datasource=%p): r=%i", ptr, size, nmemb, datasource, r); |
---|
46 | |
---|
47 | if ( r == -1 ) |
---|
48 | return 0; |
---|
49 | |
---|
50 | if ( r > 0 ) |
---|
51 | errno = 0; |
---|
52 | |
---|
53 | r /= size; |
---|
54 | |
---|
55 | ROAR_DBG("cf_vorbis_vfvio_read(ptr=%p, size=%lu, nmemb=%lu, datasource=%p) = %i", ptr, size, nmemb, datasource, r); |
---|
56 | return r; |
---|
57 | } |
---|
58 | |
---|
59 | int cf_vorbis_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
60 | struct roar_stream_server * info, |
---|
61 | struct roar_codecfilter * filter) { |
---|
62 | struct codecfilter_vorbis_inst * self = malloc(sizeof(struct codecfilter_vorbis_inst)); |
---|
63 | struct roar_stream * s = ROAR_STREAM(info); |
---|
64 | |
---|
65 | if ( !self ) |
---|
66 | return -1; |
---|
67 | |
---|
68 | self->current_section = -1; |
---|
69 | self->last_section = -1; |
---|
70 | self->opened = 0; |
---|
71 | self->got_it_running = 0; |
---|
72 | self->stream = info; |
---|
73 | // self->outlen = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
74 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
75 | self->encoding = 0; |
---|
76 | #endif |
---|
77 | |
---|
78 | ROAR_DBG("cf_vorbis_open(*): info->id=%i", ROAR_STREAM(info)->id); |
---|
79 | |
---|
80 | *inst = (CODECFILTER_USERDATA_T) self; |
---|
81 | |
---|
82 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
83 | s->info.bits = 16; |
---|
84 | |
---|
85 | if ( s->dir == ROAR_DIR_PLAY ) { |
---|
86 | return 0; |
---|
87 | } else if ( s->dir == ROAR_DIR_MONITOR || s->dir == ROAR_DIR_OUTPUT ) { |
---|
88 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
89 | // set up the encoder here |
---|
90 | |
---|
91 | memset(&(self->encoder), 0, sizeof(self->encoder)); |
---|
92 | |
---|
93 | self->encoding = 1; |
---|
94 | |
---|
95 | vorbis_info_init(&(self->encoder.vi)); |
---|
96 | vorbis_comment_init(&(self->encoder.vc)); |
---|
97 | vorbis_comment_add_tag(&(self->encoder.vc), "SERVER", "RoarAudio"); |
---|
98 | vorbis_comment_add_tag(&(self->encoder.vc), "ENCODER", "RoarAudio Vorbis codecfilter"); |
---|
99 | |
---|
100 | if( vorbis_encode_init_vbr(&(self->encoder.vi), (long) s->info.channels, (long) s->info.rate, |
---|
101 | self->encoder.v_base_quality) != 0 ) { |
---|
102 | ROAR_ERR("cf_vorbis_open(*): Can not vorbis_encode_init_vbr(*)!"); |
---|
103 | vorbis_info_clear(&(self->encoder.vi)); // TODO: do we need to free vc also? |
---|
104 | free(self); |
---|
105 | return -1; |
---|
106 | } |
---|
107 | |
---|
108 | vorbis_analysis_init(&(self->encoder.vd), &(self->encoder.vi)); |
---|
109 | vorbis_block_init(&(self->encoder.vd), &(self->encoder.vb)); |
---|
110 | |
---|
111 | // "RA"<<16 + PID<<8 + Stream ID |
---|
112 | ogg_stream_init(&(self->encoder.os), 0x52410000 + ((getpid() & 0xff)<<8) + s->id); |
---|
113 | |
---|
114 | #else |
---|
115 | free(self); |
---|
116 | return -1; |
---|
117 | #endif |
---|
118 | } else { |
---|
119 | free(self); |
---|
120 | return -1; |
---|
121 | } |
---|
122 | |
---|
123 | return 0; |
---|
124 | } |
---|
125 | |
---|
126 | int cf_vorbis_close(CODECFILTER_USERDATA_T inst) { |
---|
127 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
128 | |
---|
129 | if ( !inst ) |
---|
130 | return -1; |
---|
131 | |
---|
132 | if ( self->got_it_running ) |
---|
133 | ov_clear(&(self->vf)); |
---|
134 | |
---|
135 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
136 | if ( self->encoding ) { |
---|
137 | ogg_stream_clear(&(self->encoder.os)); |
---|
138 | vorbis_block_clear(&(self->encoder.vb)); |
---|
139 | vorbis_dsp_clear(&(self->encoder.vd)); |
---|
140 | vorbis_info_clear(&(self->encoder.vi)); |
---|
141 | } |
---|
142 | #endif |
---|
143 | |
---|
144 | free(inst); |
---|
145 | return 0; |
---|
146 | } |
---|
147 | |
---|
148 | int cf_vorbis_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
149 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
150 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
151 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
152 | ogg_packet header; |
---|
153 | ogg_packet header_comm; |
---|
154 | ogg_packet header_code; |
---|
155 | float ** encbuf; |
---|
156 | int i, c; |
---|
157 | int chans; |
---|
158 | int end; |
---|
159 | int16_t * data = (int16_t *) buf; |
---|
160 | |
---|
161 | if ( ! self->opened ) { |
---|
162 | vorbis_analysis_headerout(&(self->encoder.vd), &(self->encoder.vc), &header, &header_comm, &header_code); |
---|
163 | |
---|
164 | ogg_stream_packetin(&(self->encoder.os), &header); |
---|
165 | ogg_stream_packetin(&(self->encoder.os), &header_comm); |
---|
166 | ogg_stream_packetin(&(self->encoder.os), &header_code); |
---|
167 | |
---|
168 | while (ogg_stream_flush(&(self->encoder.os), &(self->encoder.og))) { |
---|
169 | if ( stream_vio_s_write(self->stream, self->encoder.og.header, self->encoder.og.header_len) |
---|
170 | != self->encoder.og.header_len || |
---|
171 | stream_vio_s_write(self->stream, self->encoder.og.body, self->encoder.og.body_len ) |
---|
172 | != self->encoder.og.body_len ) { |
---|
173 | free(self); // TODO: do we need addional cleanup? |
---|
174 | return -1; |
---|
175 | } |
---|
176 | } |
---|
177 | self->opened = 1; |
---|
178 | } else { |
---|
179 | encbuf = vorbis_analysis_buffer(&(self->encoder.vd), len /* TODO: need to lookup the menaing of this */); |
---|
180 | chans = s->info.channels; |
---|
181 | end = len/(2*chans); |
---|
182 | |
---|
183 | if ( chans == 1 ) { // use optimized code |
---|
184 | for (i = 0; i < end; i++) |
---|
185 | encbuf[0][i] = data[i]/32768.0; |
---|
186 | |
---|
187 | } else if ( chans == 2 ) { // use optimized code |
---|
188 | for (i = 0; i < end; i++) { |
---|
189 | encbuf[0][i] = data[2*i ]/32768.0; |
---|
190 | encbuf[1][i] = data[2*i+1]/32768.0; |
---|
191 | } |
---|
192 | } else { // use generic multi channel code |
---|
193 | for (i = 0; i < end; i++) { |
---|
194 | for (c = 0; c < chans; c++) { |
---|
195 | encbuf[c][i] = data[chans*i+c]/32768.0; |
---|
196 | } |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | vorbis_analysis_wrote(&(self->encoder.vd), i); |
---|
201 | |
---|
202 | while ( vorbis_analysis_blockout(&(self->encoder.vd), &(self->encoder.vb)) == 1 ) { |
---|
203 | vorbis_analysis(&(self->encoder.vb), &(self->encoder.op)); |
---|
204 | vorbis_bitrate_addblock(&(self->encoder.vb)); |
---|
205 | |
---|
206 | while ( vorbis_bitrate_flushpacket(&(self->encoder.vd), &(self->encoder.op)) ) { |
---|
207 | ogg_stream_packetin(&(self->encoder.os), &(self->encoder.op)); |
---|
208 | |
---|
209 | while( ogg_stream_pageout(&(self->encoder.os), &(self->encoder.og)) ) { |
---|
210 | if ( |
---|
211 | stream_vio_s_write(self->stream, self->encoder.og.header, self->encoder.og.header_len) == -1 || |
---|
212 | stream_vio_s_write(self->stream, self->encoder.og.body, self->encoder.og.body_len ) == -1 ) { |
---|
213 | return -1; |
---|
214 | } |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | return len; // we assume every thing was written (at least into our dsp anaylises buffer |
---|
221 | #else |
---|
222 | errno = ENOSYS; |
---|
223 | return -1; |
---|
224 | #endif |
---|
225 | } |
---|
226 | int cf_vorbis_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
227 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
228 | long r; |
---|
229 | long todo = len; |
---|
230 | long done = 0; |
---|
231 | |
---|
232 | // printf("cf_vorbis_read(inst=%p, buf=%p, len=%i) = ?\n", inst, buf, len); |
---|
233 | |
---|
234 | self->opened++; |
---|
235 | if ( self->opened == 16 ) { |
---|
236 | |
---|
237 | //printf("cf_vorbis_read(*): opening...\n"); |
---|
238 | if ( ov_open_callbacks((void*)self->stream, &(self->vf), NULL, 0, _g_cf_vorbis_vfvio) < 0 ) { |
---|
239 | return 0; |
---|
240 | } |
---|
241 | errno = EAGAIN; |
---|
242 | return -1; |
---|
243 | } |
---|
244 | |
---|
245 | if ( self->opened < 16 ) { |
---|
246 | errno = EAGAIN; |
---|
247 | return -1; |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | self->got_it_running = 1; |
---|
252 | |
---|
253 | while (todo) { |
---|
254 | r = ov_read(&(self->vf), buf+done, todo, 0, 2, 1, &(self->current_section)); |
---|
255 | if ( r == OV_HOLE ) { |
---|
256 | ROAR_DBG("cf_vorbis_read(*): Hole in stream"); |
---|
257 | } else if ( r < 1 ) { |
---|
258 | break; |
---|
259 | } else { |
---|
260 | if ( self->last_section != self->current_section ) |
---|
261 | if ( cf_vorbis_update_stream(self) == -1 ) |
---|
262 | return 0; |
---|
263 | |
---|
264 | self->last_section = self->current_section; |
---|
265 | todo -= r; |
---|
266 | done += r; |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | //printf("ov_read(*) = %i\n", done); |
---|
271 | |
---|
272 | if ( done == 0 ) { |
---|
273 | // do some EOF handling... |
---|
274 | return 0; |
---|
275 | } else { |
---|
276 | return len; |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | int cf_vorbis_update_stream (struct codecfilter_vorbis_inst * self) { |
---|
281 | vorbis_info *vi = ov_info(&(self->vf), -1); |
---|
282 | char **ptr = ov_comment(&(self->vf), -1)->user_comments; |
---|
283 | char key[ROAR_META_MAX_NAMELEN] = {0}, value[LIBROAR_BUFFER_MSGDATA] = {0}; |
---|
284 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
285 | int type; |
---|
286 | int j, h = 0; |
---|
287 | float rpg_track = 0, rpg_album = 0; |
---|
288 | int meta_ok; |
---|
289 | |
---|
290 | s->info.channels = vi->channels; |
---|
291 | s->info.rate = vi->rate; |
---|
292 | s->info.bits = 16; |
---|
293 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
294 | |
---|
295 | stream_meta_clear(s->id); |
---|
296 | |
---|
297 | while(*ptr){ |
---|
298 | meta_ok = 1; |
---|
299 | |
---|
300 | for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) { |
---|
301 | if ( j == ROAR_META_MAX_NAMELEN ) { |
---|
302 | ROAR_ERR("cf_vorbis_update_stream(*): invalid meta data on stream %i: meta data key too long", s->id); |
---|
303 | meta_ok = 0; |
---|
304 | j = 0; |
---|
305 | break; |
---|
306 | } |
---|
307 | key[j] = (*ptr)[j]; |
---|
308 | } |
---|
309 | key[j] = 0; |
---|
310 | |
---|
311 | if ( meta_ok ) { |
---|
312 | for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) { |
---|
313 | if ( h == LIBROAR_BUFFER_MSGDATA ) { |
---|
314 | ROAR_ERR("update_stream(*): invalid meta data on stream %i: meta data value for key '%s' too long", s->id, key); |
---|
315 | meta_ok = 0; |
---|
316 | h = 0; |
---|
317 | break; |
---|
318 | } |
---|
319 | value[h++] = (*ptr)[j]; |
---|
320 | } |
---|
321 | value[h] = 0; |
---|
322 | } |
---|
323 | |
---|
324 | if ( meta_ok ) { |
---|
325 | type = roar_meta_inttype(key); |
---|
326 | if ( type != -1 ) |
---|
327 | stream_meta_set(s->id, type, "", value); |
---|
328 | |
---|
329 | ROAR_DBG("cf_vorbis_update_stream(*): Meta %-16s: %s", key, value); |
---|
330 | |
---|
331 | if ( strcmp(key, "REPLAYGAIN_TRACK_PEAK") == 0 ) { |
---|
332 | rpg_track = 1/atof(value); |
---|
333 | /* |
---|
334 | } else if ( strcmp(key, "REPLAYGAIN_TRACK_GAIN") == 0 ) { |
---|
335 | rpg_track = powf(10, atof(value)/20); |
---|
336 | */ |
---|
337 | } else if ( strcmp(key, "REPLAYGAIN_ALBUM_PEAK") == 0 ) { |
---|
338 | rpg_album = 1/atof(value); |
---|
339 | /* |
---|
340 | } else if ( strcmp(key, "REPLAYGAIN_ALBUM_GAIN") == 0 ) { |
---|
341 | rpg_album = powf(10, atof(value)/20); |
---|
342 | */ |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | ptr++; |
---|
347 | } |
---|
348 | |
---|
349 | if ( rpg_album ) { |
---|
350 | self->stream->mixer.rpg_div = 2718; // = int(exp(1)*1000) |
---|
351 | self->stream->mixer.rpg_mul = (float)rpg_album*2718; |
---|
352 | } else if ( rpg_track ) { |
---|
353 | self->stream->mixer.rpg_div = 2718; |
---|
354 | self->stream->mixer.rpg_mul = (float)rpg_track*2718; |
---|
355 | } |
---|
356 | |
---|
357 | //printf("RPG: mul=%i, div=%i\n", self->stream->mixer.rpg_mul, self->stream->mixer.rpg_div); |
---|
358 | return 0; |
---|
359 | } |
---|
360 | |
---|
361 | #endif |
---|
362 | |
---|
363 | //ll |
---|