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