[204] | 1 | //codecfilter_vorbis.c: |
---|
| 2 | |
---|
[668] | 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 | |
---|
[204] | 25 | #include "roard.h" |
---|
| 26 | |
---|
[362] | 27 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
| 28 | |
---|
[741] | 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 = _g_cf_vorbis_vfvio_return_err, |
---|
| 36 | .close_func = _g_cf_vorbis_vfvio_return_err, |
---|
| 37 | .tell_func = _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 | r = stream_vio_s_read(ROAR_STREAM_SERVER(datasource), ptr, size*nmemb); |
---|
| 43 | |
---|
| 44 | ROAR_WARN("cf_vorbis_vfvio_read(ptr=%p, size=%lu, nmemb=%lu, datasource=%p): r=%i", ptr, size, nmemb, datasource, r); |
---|
| 45 | |
---|
| 46 | if ( r == -1 ) |
---|
| 47 | return 0; |
---|
| 48 | |
---|
| 49 | if ( r > 0 ) |
---|
| 50 | errno = 0; |
---|
| 51 | |
---|
| 52 | return r/nmemb; |
---|
| 53 | } |
---|
| 54 | |
---|
[205] | 55 | int cf_vorbis_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 56 | struct roar_stream_server * info, |
---|
| 57 | struct roar_codecfilter * filter) { |
---|
| 58 | struct codecfilter_vorbis_inst * self = malloc(sizeof(struct codecfilter_vorbis_inst)); |
---|
[569] | 59 | struct roar_stream * s = ROAR_STREAM(info); |
---|
[205] | 60 | |
---|
| 61 | if ( !self ) |
---|
| 62 | return -1; |
---|
| 63 | |
---|
| 64 | self->current_section = -1; |
---|
| 65 | self->last_section = -1; |
---|
[382] | 66 | self->opened = 0; |
---|
| 67 | self->got_it_running = 0; |
---|
[205] | 68 | self->stream = info; |
---|
| 69 | // self->outlen = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
[569] | 70 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
| 71 | self->encoding = 0; |
---|
| 72 | #endif |
---|
[205] | 73 | |
---|
[486] | 74 | ROAR_DBG("cf_vorbis_open(*): info->id=%i", ROAR_STREAM(info)->id); |
---|
| 75 | |
---|
[741] | 76 | /* |
---|
[569] | 77 | if ( (self->in = fdopen(s->fh, "r")) == NULL ) { |
---|
[205] | 78 | free((void*)self); |
---|
| 79 | return -1; |
---|
| 80 | } |
---|
[741] | 81 | */ |
---|
[205] | 82 | |
---|
| 83 | *inst = (CODECFILTER_USERDATA_T) self; |
---|
| 84 | |
---|
[569] | 85 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
| 86 | s->info.bits = 16; |
---|
| 87 | |
---|
| 88 | if ( s->dir == ROAR_DIR_PLAY ) { |
---|
| 89 | return 0; |
---|
[634] | 90 | } else if ( s->dir == ROAR_DIR_MONITOR || s->dir == ROAR_DIR_OUTPUT ) { |
---|
[569] | 91 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
| 92 | // set up the encoder here |
---|
| 93 | |
---|
| 94 | memset(&(self->encoder), 0, sizeof(self->encoder)); |
---|
| 95 | |
---|
| 96 | self->encoding = 1; |
---|
| 97 | |
---|
| 98 | vorbis_info_init(&(self->encoder.vi)); |
---|
| 99 | vorbis_comment_init(&(self->encoder.vc)); |
---|
| 100 | vorbis_comment_add_tag(&(self->encoder.vc), "SERVER", "RoarAudio"); |
---|
| 101 | vorbis_comment_add_tag(&(self->encoder.vc), "ENCODER", "RoarAudio Vorbis codecfilter"); |
---|
| 102 | |
---|
| 103 | if( vorbis_encode_init_vbr(&(self->encoder.vi), (long) s->info.channels, (long) s->info.rate, |
---|
| 104 | self->encoder.v_base_quality) != 0 ) { |
---|
| 105 | ROAR_ERR("cf_vorbis_open(*): Can not vorbis_encode_init_vbr(*)!"); |
---|
| 106 | vorbis_info_clear(&(self->encoder.vi)); // TODO: do we need to free vc also? |
---|
| 107 | free(self); |
---|
| 108 | return -1; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | vorbis_analysis_init(&(self->encoder.vd), &(self->encoder.vi)); |
---|
| 112 | vorbis_block_init(&(self->encoder.vd), &(self->encoder.vb)); |
---|
| 113 | |
---|
| 114 | // "RA"<<16 + PID<<8 + Stream ID |
---|
| 115 | ogg_stream_init(&(self->encoder.os), 0x52410000 + ((getpid() & 0xff)<<8) + s->id); |
---|
| 116 | |
---|
| 117 | #else |
---|
| 118 | free(self); |
---|
| 119 | return -1; |
---|
| 120 | #endif |
---|
| 121 | } else { |
---|
| 122 | free(self); |
---|
| 123 | return -1; |
---|
| 124 | } |
---|
[206] | 125 | |
---|
| 126 | return 0; |
---|
[205] | 127 | } |
---|
| 128 | |
---|
| 129 | int cf_vorbis_close(CODECFILTER_USERDATA_T inst) { |
---|
| 130 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
| 131 | |
---|
| 132 | if ( !inst ) |
---|
| 133 | return -1; |
---|
| 134 | |
---|
[382] | 135 | if ( self->got_it_running ) |
---|
| 136 | ov_clear(&(self->vf)); |
---|
[205] | 137 | |
---|
[569] | 138 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
| 139 | if ( self->encoding ) { |
---|
| 140 | ogg_stream_clear(&(self->encoder.os)); |
---|
| 141 | vorbis_block_clear(&(self->encoder.vb)); |
---|
| 142 | vorbis_dsp_clear(&(self->encoder.vd)); |
---|
| 143 | vorbis_info_clear(&(self->encoder.vi)); |
---|
| 144 | } |
---|
| 145 | #endif |
---|
| 146 | |
---|
[205] | 147 | free(inst); |
---|
[206] | 148 | return 0; |
---|
[205] | 149 | } |
---|
| 150 | |
---|
[582] | 151 | int cf_vorbis_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 152 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
| 153 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
| 154 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
| 155 | ogg_packet header; |
---|
| 156 | ogg_packet header_comm; |
---|
| 157 | ogg_packet header_code; |
---|
[586] | 158 | float ** encbuf; |
---|
| 159 | int i, c; |
---|
| 160 | int chans; |
---|
| 161 | int end; |
---|
| 162 | int16_t * data = (int16_t *) buf; |
---|
[582] | 163 | |
---|
[584] | 164 | if ( ! self->opened ) { |
---|
[582] | 165 | vorbis_analysis_headerout(&(self->encoder.vd), &(self->encoder.vc), &header, &header_comm, &header_code); |
---|
| 166 | |
---|
| 167 | ogg_stream_packetin(&(self->encoder.os), &header); |
---|
| 168 | ogg_stream_packetin(&(self->encoder.os), &header_comm); |
---|
| 169 | ogg_stream_packetin(&(self->encoder.os), &header_code); |
---|
| 170 | |
---|
| 171 | while (ogg_stream_flush(&(self->encoder.os), &(self->encoder.og))) { |
---|
[598] | 172 | if ( stream_vio_s_write(self->stream, self->encoder.og.header, self->encoder.og.header_len) |
---|
| 173 | != self->encoder.og.header_len || |
---|
| 174 | stream_vio_s_write(self->stream, self->encoder.og.body, self->encoder.og.body_len ) |
---|
| 175 | != self->encoder.og.body_len ) { |
---|
[582] | 176 | free(self); // TODO: do we need addional cleanup? |
---|
| 177 | return -1; |
---|
| 178 | } |
---|
| 179 | } |
---|
[584] | 180 | self->opened = 1; |
---|
[586] | 181 | } else { |
---|
| 182 | encbuf = vorbis_analysis_buffer(&(self->encoder.vd), len /* TODO: need to lookup the menaing of this */); |
---|
| 183 | chans = s->info.channels; |
---|
| 184 | end = len/(2*chans); |
---|
| 185 | |
---|
| 186 | if ( chans == 1 ) { // use optimized code |
---|
| 187 | for (i = 0; i < end; i++) |
---|
| 188 | encbuf[0][i] = data[i]/32768.0; |
---|
| 189 | |
---|
| 190 | } else if ( chans == 2 ) { // use optimized code |
---|
| 191 | for (i = 0; i < end; i++) { |
---|
| 192 | encbuf[0][i] = data[2*i ]/32768.0; |
---|
| 193 | encbuf[1][i] = data[2*i+1]/32768.0; |
---|
| 194 | } |
---|
| 195 | } else { // use generic multi channel code |
---|
| 196 | for (i = 0; i < end; i++) { |
---|
| 197 | for (c = 0; c < chans; c++) { |
---|
| 198 | encbuf[c][i] = data[chans*i+c]/32768.0; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | } |
---|
[587] | 202 | |
---|
| 203 | vorbis_analysis_wrote(&(self->encoder.vd), i); |
---|
| 204 | |
---|
| 205 | while ( vorbis_analysis_blockout(&(self->encoder.vd), &(self->encoder.vb)) == 1 ) { |
---|
| 206 | vorbis_analysis(&(self->encoder.vb), &(self->encoder.op)); |
---|
| 207 | vorbis_bitrate_addblock(&(self->encoder.vb)); |
---|
| 208 | |
---|
| 209 | while ( vorbis_bitrate_flushpacket(&(self->encoder.vd), &(self->encoder.op)) ) { |
---|
| 210 | ogg_stream_packetin(&(self->encoder.os), &(self->encoder.op)); |
---|
| 211 | |
---|
| 212 | while( ogg_stream_pageout(&(self->encoder.os), &(self->encoder.og)) ) { |
---|
[602] | 213 | if ( |
---|
| 214 | stream_vio_s_write(self->stream, self->encoder.og.header, self->encoder.og.header_len) == -1 || |
---|
| 215 | stream_vio_s_write(self->stream, self->encoder.og.body, self->encoder.og.body_len ) == -1 ) { |
---|
| 216 | return -1; |
---|
| 217 | } |
---|
[587] | 218 | } |
---|
| 219 | } |
---|
| 220 | } |
---|
[584] | 221 | } |
---|
[582] | 222 | |
---|
| 223 | return len; // we assume every thing was written (at least into our dsp anaylises buffer |
---|
| 224 | #else |
---|
| 225 | errno = ENOSYS; |
---|
| 226 | return -1; |
---|
| 227 | #endif |
---|
| 228 | } |
---|
[205] | 229 | int cf_vorbis_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 230 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
| 231 | long r; |
---|
[206] | 232 | long todo = len; |
---|
| 233 | long done = 0; |
---|
[205] | 234 | |
---|
[206] | 235 | // printf("cf_vorbis_read(inst=%p, buf=%p, len=%i) = ?\n", inst, buf, len); |
---|
| 236 | |
---|
| 237 | self->opened++; |
---|
| 238 | if ( self->opened == 16 ) { |
---|
| 239 | //printf("cf_vorbis_read(*): opening...\n"); |
---|
[741] | 240 | //int ov_open_callbacks(void *datasource, OggVorbis_File *vf, char *initial, long ibytes, ov_callbacks callbacks); |
---|
| 241 | if ( ov_open_callbacks((void*)self->stream, &(self->vf), NULL, 0, _g_cf_vorbis_vfvio) < 0 ) { |
---|
| 242 | // if ( ov_open(self->in, &(self->vf), NULL, 0) < 0 ) { |
---|
[382] | 243 | // free((void*)self); |
---|
[206] | 244 | return 0; |
---|
[205] | 245 | } |
---|
| 246 | } |
---|
[206] | 247 | |
---|
| 248 | if ( self->opened < 16 ) { |
---|
[541] | 249 | errno = EAGAIN; |
---|
[206] | 250 | return -1; |
---|
| 251 | } |
---|
[205] | 252 | |
---|
[382] | 253 | |
---|
| 254 | self->got_it_running = 1; |
---|
| 255 | |
---|
[206] | 256 | while (todo) { |
---|
| 257 | r = ov_read(&(self->vf), buf+done, todo, 0, 2, 1, &(self->current_section)); |
---|
| 258 | if ( r < 1 ) { |
---|
| 259 | break; |
---|
| 260 | } else { |
---|
[207] | 261 | if ( self->last_section != self->current_section ) |
---|
| 262 | if ( cf_vorbis_update_stream(self) == -1 ) |
---|
| 263 | return 0; |
---|
| 264 | |
---|
| 265 | self->last_section = self->current_section; |
---|
[206] | 266 | todo -= r; |
---|
| 267 | done += r; |
---|
| 268 | } |
---|
| 269 | } |
---|
[205] | 270 | |
---|
[206] | 271 | //printf("ov_read(*) = %i\n", done); |
---|
| 272 | |
---|
| 273 | if ( done == 0 ) { |
---|
[205] | 274 | // do some EOF handling... |
---|
[206] | 275 | return 0; |
---|
[205] | 276 | } else { |
---|
[206] | 277 | return len; |
---|
[205] | 278 | } |
---|
| 279 | } |
---|
| 280 | |
---|
[207] | 281 | int cf_vorbis_update_stream (struct codecfilter_vorbis_inst * self) { |
---|
| 282 | vorbis_info *vi = ov_info(&(self->vf), -1); |
---|
| 283 | char **ptr = ov_comment(&(self->vf), -1)->user_comments; |
---|
[716] | 284 | char key[ROAR_META_MAX_NAMELEN] = {0}, value[LIBROAR_BUFFER_MSGDATA] = {0}; |
---|
[583] | 285 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
[207] | 286 | int type; |
---|
| 287 | int j, h = 0; |
---|
[222] | 288 | float rpg_track = 0, rpg_album = 0; |
---|
[716] | 289 | int meta_ok; |
---|
[207] | 290 | |
---|
| 291 | s->info.channels = vi->channels; |
---|
| 292 | s->info.rate = vi->rate; |
---|
| 293 | s->info.bits = 16; |
---|
| 294 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
| 295 | |
---|
| 296 | stream_meta_clear(s->id); |
---|
| 297 | |
---|
| 298 | while(*ptr){ |
---|
[716] | 299 | meta_ok = 1; |
---|
[207] | 300 | |
---|
[716] | 301 | for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) { |
---|
| 302 | if ( j == ROAR_META_MAX_NAMELEN ) { |
---|
| 303 | ROAR_ERR("cf_vorbis_update_stream(*): invalid meta data on stream %i: meta data key too long", s->id); |
---|
| 304 | meta_ok = 0; |
---|
| 305 | j = 0; |
---|
| 306 | break; |
---|
| 307 | } |
---|
| 308 | key[j] = (*ptr)[j]; |
---|
| 309 | } |
---|
| 310 | key[j] = 0; |
---|
[222] | 311 | |
---|
[716] | 312 | if ( meta_ok ) { |
---|
| 313 | for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) { |
---|
| 314 | if ( h == LIBROAR_BUFFER_MSGDATA ) { |
---|
| 315 | ROAR_ERR("update_stream(*): invalid meta data on stream %i: meta data value for key '%s' too long", s->id, key); |
---|
[717] | 316 | meta_ok = 0; |
---|
[716] | 317 | h = 0; |
---|
| 318 | break; |
---|
| 319 | } |
---|
| 320 | value[h++] = (*ptr)[j]; |
---|
| 321 | } |
---|
| 322 | value[h] = 0; |
---|
[222] | 323 | } |
---|
| 324 | |
---|
[716] | 325 | if ( meta_ok ) { |
---|
| 326 | type = roar_meta_inttype(key); |
---|
| 327 | if ( type != -1 ) |
---|
| 328 | stream_meta_set(s->id, type, "", value); |
---|
| 329 | |
---|
| 330 | ROAR_DBG("cf_vorbis_update_stream(*): Meta %-16s: %s", key, value); |
---|
| 331 | |
---|
| 332 | if ( strcmp(key, "REPLAYGAIN_TRACK_PEAK") == 0 ) { |
---|
| 333 | rpg_track = 1/atof(value); |
---|
| 334 | /* |
---|
| 335 | } else if ( strcmp(key, "REPLAYGAIN_TRACK_GAIN") == 0 ) { |
---|
| 336 | rpg_track = powf(10, atof(value)/20); |
---|
| 337 | */ |
---|
| 338 | } else if ( strcmp(key, "REPLAYGAIN_ALBUM_PEAK") == 0 ) { |
---|
| 339 | rpg_album = 1/atof(value); |
---|
| 340 | /* |
---|
| 341 | } else if ( strcmp(key, "REPLAYGAIN_ALBUM_GAIN") == 0 ) { |
---|
| 342 | rpg_album = powf(10, atof(value)/20); |
---|
| 343 | */ |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | ptr++; |
---|
[207] | 348 | } |
---|
| 349 | |
---|
[222] | 350 | if ( rpg_album ) { |
---|
| 351 | self->stream->mixer.rpg_div = 2718; // = int(exp(1)*1000) |
---|
| 352 | self->stream->mixer.rpg_mul = (float)rpg_album*2718; |
---|
| 353 | } else if ( rpg_track ) { |
---|
| 354 | self->stream->mixer.rpg_div = 2718; |
---|
| 355 | self->stream->mixer.rpg_mul = (float)rpg_track*2718; |
---|
| 356 | } |
---|
| 357 | |
---|
[223] | 358 | //printf("RPG: mul=%i, div=%i\n", self->stream->mixer.rpg_mul, self->stream->mixer.rpg_div); |
---|
[207] | 359 | return 0; |
---|
| 360 | } |
---|
| 361 | |
---|
[362] | 362 | #endif |
---|
| 363 | |
---|
[204] | 364 | //ll |
---|