[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 | |
---|
[2107] | 31 | #define FIFAC ((float)((uint64_t)1<<(ROAR_VORBIS_BITS-1))) |
---|
| 32 | |
---|
[741] | 33 | int _g_cf_vorbis_vfvio_return_err (void) { |
---|
| 34 | return -1; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | ov_callbacks _g_cf_vorbis_vfvio = { |
---|
| 38 | .read_func = cf_vorbis_vfvio_read, |
---|
[743] | 39 | .seek_func = (int (*)(void *, ogg_int64_t, int )) _g_cf_vorbis_vfvio_return_err, |
---|
| 40 | .close_func = (int (*)(void * )) _g_cf_vorbis_vfvio_return_err, |
---|
| 41 | .tell_func = (long (*)(void * )) _g_cf_vorbis_vfvio_return_err |
---|
[741] | 42 | }; |
---|
| 43 | |
---|
| 44 | size_t cf_vorbis_vfvio_read (void *ptr, size_t size, size_t nmemb, void *datasource) { |
---|
| 45 | ssize_t r; |
---|
[746] | 46 | |
---|
[741] | 47 | r = stream_vio_s_read(ROAR_STREAM_SERVER(datasource), ptr, size*nmemb); |
---|
| 48 | |
---|
[745] | 49 | ROAR_DBG("cf_vorbis_vfvio_read(ptr=%p, size=%lu, nmemb=%lu, datasource=%p): r=%i", ptr, size, nmemb, datasource, r); |
---|
[741] | 50 | |
---|
| 51 | if ( r == -1 ) |
---|
| 52 | return 0; |
---|
| 53 | |
---|
| 54 | if ( r > 0 ) |
---|
| 55 | errno = 0; |
---|
[744] | 56 | |
---|
| 57 | r /= size; |
---|
[741] | 58 | |
---|
[745] | 59 | ROAR_DBG("cf_vorbis_vfvio_read(ptr=%p, size=%lu, nmemb=%lu, datasource=%p) = %i", ptr, size, nmemb, datasource, r); |
---|
[744] | 60 | return r; |
---|
[741] | 61 | } |
---|
| 62 | |
---|
[205] | 63 | int cf_vorbis_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 64 | struct roar_stream_server * info, |
---|
| 65 | struct roar_codecfilter * filter) { |
---|
| 66 | struct codecfilter_vorbis_inst * self = malloc(sizeof(struct codecfilter_vorbis_inst)); |
---|
[569] | 67 | struct roar_stream * s = ROAR_STREAM(info); |
---|
[205] | 68 | |
---|
| 69 | if ( !self ) |
---|
| 70 | return -1; |
---|
| 71 | |
---|
| 72 | self->current_section = -1; |
---|
| 73 | self->last_section = -1; |
---|
[382] | 74 | self->opened = 0; |
---|
| 75 | self->got_it_running = 0; |
---|
[205] | 76 | self->stream = info; |
---|
| 77 | // self->outlen = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
[569] | 78 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
[1226] | 79 | self->encoding = 0; |
---|
| 80 | self->encoder.v_base_quality = 0.3; |
---|
[1238] | 81 | self->encoder.srn = -1; |
---|
[569] | 82 | #endif |
---|
[205] | 83 | |
---|
[486] | 84 | ROAR_DBG("cf_vorbis_open(*): info->id=%i", ROAR_STREAM(info)->id); |
---|
| 85 | |
---|
[205] | 86 | *inst = (CODECFILTER_USERDATA_T) self; |
---|
| 87 | |
---|
[569] | 88 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
| 89 | s->info.bits = 16; |
---|
| 90 | |
---|
| 91 | if ( s->dir == ROAR_DIR_PLAY ) { |
---|
| 92 | return 0; |
---|
[634] | 93 | } else if ( s->dir == ROAR_DIR_MONITOR || s->dir == ROAR_DIR_OUTPUT ) { |
---|
[569] | 94 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
| 95 | // set up the encoder here |
---|
[2087] | 96 | // this is delayed to the write function |
---|
| 97 | /* |
---|
[1226] | 98 | if ( cf_vorbis_encode_start(self) == -1 ) { |
---|
| 99 | free(self); |
---|
| 100 | return -1; |
---|
| 101 | } |
---|
[2087] | 102 | */ |
---|
[2107] | 103 | s->info.bits = ROAR_VORBIS_BITS; |
---|
[569] | 104 | #else |
---|
| 105 | free(self); |
---|
| 106 | return -1; |
---|
| 107 | #endif |
---|
| 108 | } else { |
---|
| 109 | free(self); |
---|
| 110 | return -1; |
---|
| 111 | } |
---|
[206] | 112 | |
---|
| 113 | return 0; |
---|
[205] | 114 | } |
---|
| 115 | |
---|
| 116 | int cf_vorbis_close(CODECFILTER_USERDATA_T inst) { |
---|
| 117 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
| 118 | |
---|
| 119 | if ( !inst ) |
---|
| 120 | return -1; |
---|
| 121 | |
---|
[382] | 122 | if ( self->got_it_running ) |
---|
| 123 | ov_clear(&(self->vf)); |
---|
[205] | 124 | |
---|
[569] | 125 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
| 126 | if ( self->encoding ) { |
---|
[1226] | 127 | cf_vorbis_encode_end(self); |
---|
[569] | 128 | } |
---|
| 129 | #endif |
---|
| 130 | |
---|
[205] | 131 | free(inst); |
---|
[206] | 132 | return 0; |
---|
[205] | 133 | } |
---|
| 134 | |
---|
[582] | 135 | int cf_vorbis_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 136 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
| 137 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
| 138 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
| 139 | ogg_packet header; |
---|
| 140 | ogg_packet header_comm; |
---|
| 141 | ogg_packet header_code; |
---|
[586] | 142 | float ** encbuf; |
---|
| 143 | int i, c; |
---|
| 144 | int chans; |
---|
| 145 | int end; |
---|
[2107] | 146 | #if ROAR_VORBIS_BITS == 8 |
---|
| 147 | int8_t * data = (int8_t *) buf; |
---|
| 148 | #elif ROAR_VORBIS_BITS == 16 |
---|
[586] | 149 | int16_t * data = (int16_t *) buf; |
---|
[2107] | 150 | #elif ROAR_VORBIS_BITS == 32 |
---|
| 151 | int32_t * data = (int32_t *) buf; |
---|
| 152 | #else |
---|
| 153 | #error value of ROAR_VORBIS_BITS not supported |
---|
| 154 | #endif |
---|
[582] | 155 | |
---|
[584] | 156 | if ( ! self->opened ) { |
---|
[2087] | 157 | if ( !self->encoding ) { |
---|
| 158 | if ( cf_vorbis_encode_start(self) == -1 ) { |
---|
| 159 | return -1; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
[582] | 163 | vorbis_analysis_headerout(&(self->encoder.vd), &(self->encoder.vc), &header, &header_comm, &header_code); |
---|
| 164 | |
---|
| 165 | ogg_stream_packetin(&(self->encoder.os), &header); |
---|
| 166 | ogg_stream_packetin(&(self->encoder.os), &header_comm); |
---|
| 167 | ogg_stream_packetin(&(self->encoder.os), &header_code); |
---|
| 168 | |
---|
| 169 | while (ogg_stream_flush(&(self->encoder.os), &(self->encoder.og))) { |
---|
[598] | 170 | if ( stream_vio_s_write(self->stream, self->encoder.og.header, self->encoder.og.header_len) |
---|
| 171 | != self->encoder.og.header_len || |
---|
| 172 | stream_vio_s_write(self->stream, self->encoder.og.body, self->encoder.og.body_len ) |
---|
| 173 | != self->encoder.og.body_len ) { |
---|
[582] | 174 | free(self); // TODO: do we need addional cleanup? |
---|
| 175 | return -1; |
---|
| 176 | } |
---|
| 177 | } |
---|
[584] | 178 | self->opened = 1; |
---|
[586] | 179 | } else { |
---|
| 180 | encbuf = vorbis_analysis_buffer(&(self->encoder.vd), len /* TODO: need to lookup the menaing of this */); |
---|
| 181 | chans = s->info.channels; |
---|
[2107] | 182 | end = len*8/(ROAR_VORBIS_BITS*chans); |
---|
[586] | 183 | |
---|
| 184 | if ( chans == 1 ) { // use optimized code |
---|
| 185 | for (i = 0; i < end; i++) |
---|
[2107] | 186 | encbuf[0][i] = data[i]/FIFAC; |
---|
[586] | 187 | |
---|
| 188 | } else if ( chans == 2 ) { // use optimized code |
---|
| 189 | for (i = 0; i < end; i++) { |
---|
[2107] | 190 | encbuf[0][i] = data[2*i ]/FIFAC; |
---|
| 191 | encbuf[1][i] = data[2*i+1]/FIFAC; |
---|
[586] | 192 | } |
---|
| 193 | } else { // use generic multi channel code |
---|
| 194 | for (i = 0; i < end; i++) { |
---|
| 195 | for (c = 0; c < chans; c++) { |
---|
[2107] | 196 | encbuf[c][i] = data[chans*i+c]/FIFAC; |
---|
[586] | 197 | } |
---|
| 198 | } |
---|
| 199 | } |
---|
[587] | 200 | |
---|
| 201 | vorbis_analysis_wrote(&(self->encoder.vd), i); |
---|
| 202 | |
---|
[1238] | 203 | if ( cf_vorbis_encode_flushout(self) == -1 ) |
---|
| 204 | return -1; |
---|
[584] | 205 | } |
---|
[582] | 206 | |
---|
| 207 | return len; // we assume every thing was written (at least into our dsp anaylises buffer |
---|
| 208 | #else |
---|
| 209 | errno = ENOSYS; |
---|
| 210 | return -1; |
---|
| 211 | #endif |
---|
| 212 | } |
---|
[205] | 213 | int cf_vorbis_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 214 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
| 215 | long r; |
---|
[206] | 216 | long todo = len; |
---|
| 217 | long done = 0; |
---|
[205] | 218 | |
---|
[206] | 219 | // printf("cf_vorbis_read(inst=%p, buf=%p, len=%i) = ?\n", inst, buf, len); |
---|
| 220 | |
---|
| 221 | self->opened++; |
---|
| 222 | if ( self->opened == 16 ) { |
---|
[744] | 223 | |
---|
[206] | 224 | //printf("cf_vorbis_read(*): opening...\n"); |
---|
[741] | 225 | if ( ov_open_callbacks((void*)self->stream, &(self->vf), NULL, 0, _g_cf_vorbis_vfvio) < 0 ) { |
---|
[206] | 226 | return 0; |
---|
[205] | 227 | } |
---|
[742] | 228 | errno = EAGAIN; |
---|
| 229 | return -1; |
---|
[205] | 230 | } |
---|
[206] | 231 | |
---|
| 232 | if ( self->opened < 16 ) { |
---|
[541] | 233 | errno = EAGAIN; |
---|
[206] | 234 | return -1; |
---|
| 235 | } |
---|
[205] | 236 | |
---|
[382] | 237 | |
---|
| 238 | self->got_it_running = 1; |
---|
| 239 | |
---|
[206] | 240 | while (todo) { |
---|
| 241 | r = ov_read(&(self->vf), buf+done, todo, 0, 2, 1, &(self->current_section)); |
---|
[744] | 242 | if ( r == OV_HOLE ) { |
---|
[745] | 243 | ROAR_DBG("cf_vorbis_read(*): Hole in stream"); |
---|
[744] | 244 | } else if ( r < 1 ) { |
---|
[206] | 245 | break; |
---|
| 246 | } else { |
---|
[207] | 247 | if ( self->last_section != self->current_section ) |
---|
| 248 | if ( cf_vorbis_update_stream(self) == -1 ) |
---|
| 249 | return 0; |
---|
| 250 | |
---|
| 251 | self->last_section = self->current_section; |
---|
[206] | 252 | todo -= r; |
---|
| 253 | done += r; |
---|
| 254 | } |
---|
| 255 | } |
---|
[205] | 256 | |
---|
[744] | 257 | //printf("ov_read(*) = %i\n", done); |
---|
[206] | 258 | |
---|
| 259 | if ( done == 0 ) { |
---|
[205] | 260 | // do some EOF handling... |
---|
[206] | 261 | return 0; |
---|
[205] | 262 | } else { |
---|
[206] | 263 | return len; |
---|
[205] | 264 | } |
---|
| 265 | } |
---|
| 266 | |
---|
[207] | 267 | int cf_vorbis_update_stream (struct codecfilter_vorbis_inst * self) { |
---|
[1493] | 268 | #ifdef ROAR_SUPPORT_META |
---|
[207] | 269 | vorbis_info *vi = ov_info(&(self->vf), -1); |
---|
| 270 | char **ptr = ov_comment(&(self->vf), -1)->user_comments; |
---|
[716] | 271 | char key[ROAR_META_MAX_NAMELEN] = {0}, value[LIBROAR_BUFFER_MSGDATA] = {0}; |
---|
[583] | 272 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
[207] | 273 | int type; |
---|
| 274 | int j, h = 0; |
---|
[222] | 275 | float rpg_track = 0, rpg_album = 0; |
---|
[716] | 276 | int meta_ok; |
---|
[207] | 277 | |
---|
| 278 | s->info.channels = vi->channels; |
---|
| 279 | s->info.rate = vi->rate; |
---|
| 280 | s->info.bits = 16; |
---|
| 281 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
| 282 | |
---|
| 283 | stream_meta_clear(s->id); |
---|
| 284 | |
---|
| 285 | while(*ptr){ |
---|
[716] | 286 | meta_ok = 1; |
---|
[207] | 287 | |
---|
[716] | 288 | for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) { |
---|
| 289 | if ( j == ROAR_META_MAX_NAMELEN ) { |
---|
| 290 | ROAR_ERR("cf_vorbis_update_stream(*): invalid meta data on stream %i: meta data key too long", s->id); |
---|
| 291 | meta_ok = 0; |
---|
| 292 | j = 0; |
---|
| 293 | break; |
---|
| 294 | } |
---|
| 295 | key[j] = (*ptr)[j]; |
---|
| 296 | } |
---|
| 297 | key[j] = 0; |
---|
[222] | 298 | |
---|
[716] | 299 | if ( meta_ok ) { |
---|
| 300 | for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) { |
---|
| 301 | if ( h == LIBROAR_BUFFER_MSGDATA ) { |
---|
| 302 | ROAR_ERR("update_stream(*): invalid meta data on stream %i: meta data value for key '%s' too long", s->id, key); |
---|
[717] | 303 | meta_ok = 0; |
---|
[716] | 304 | h = 0; |
---|
| 305 | break; |
---|
| 306 | } |
---|
| 307 | value[h++] = (*ptr)[j]; |
---|
| 308 | } |
---|
| 309 | value[h] = 0; |
---|
[222] | 310 | } |
---|
| 311 | |
---|
[716] | 312 | if ( meta_ok ) { |
---|
| 313 | type = roar_meta_inttype(key); |
---|
| 314 | if ( type != -1 ) |
---|
| 315 | stream_meta_set(s->id, type, "", value); |
---|
| 316 | |
---|
| 317 | ROAR_DBG("cf_vorbis_update_stream(*): Meta %-16s: %s", key, value); |
---|
| 318 | |
---|
| 319 | if ( strcmp(key, "REPLAYGAIN_TRACK_PEAK") == 0 ) { |
---|
| 320 | rpg_track = 1/atof(value); |
---|
| 321 | /* |
---|
| 322 | } else if ( strcmp(key, "REPLAYGAIN_TRACK_GAIN") == 0 ) { |
---|
| 323 | rpg_track = powf(10, atof(value)/20); |
---|
| 324 | */ |
---|
| 325 | } else if ( strcmp(key, "REPLAYGAIN_ALBUM_PEAK") == 0 ) { |
---|
| 326 | rpg_album = 1/atof(value); |
---|
| 327 | /* |
---|
| 328 | } else if ( strcmp(key, "REPLAYGAIN_ALBUM_GAIN") == 0 ) { |
---|
| 329 | rpg_album = powf(10, atof(value)/20); |
---|
| 330 | */ |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | ptr++; |
---|
[207] | 335 | } |
---|
| 336 | |
---|
[222] | 337 | if ( rpg_album ) { |
---|
| 338 | self->stream->mixer.rpg_div = 2718; // = int(exp(1)*1000) |
---|
| 339 | self->stream->mixer.rpg_mul = (float)rpg_album*2718; |
---|
| 340 | } else if ( rpg_track ) { |
---|
| 341 | self->stream->mixer.rpg_div = 2718; |
---|
| 342 | self->stream->mixer.rpg_mul = (float)rpg_track*2718; |
---|
| 343 | } |
---|
| 344 | |
---|
[1041] | 345 | stream_meta_finalize(s->id); |
---|
[1493] | 346 | #endif |
---|
[223] | 347 | //printf("RPG: mul=%i, div=%i\n", self->stream->mixer.rpg_mul, self->stream->mixer.rpg_div); |
---|
[207] | 348 | return 0; |
---|
| 349 | } |
---|
| 350 | |
---|
[1226] | 351 | int cf_vorbis_encode_start (struct codecfilter_vorbis_inst * self) { |
---|
| 352 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
[1238] | 353 | int srn = self->encoder.srn; // this value is allrady inited... |
---|
[1493] | 354 | #ifdef ROAR_SUPPORT_META |
---|
[1238] | 355 | int len = 0; |
---|
| 356 | int i; |
---|
| 357 | int types[ROAR_META_MAX_PER_STREAM]; |
---|
[1493] | 358 | #endif |
---|
[1238] | 359 | int sid = ROAR_STREAM(self->stream)->id; |
---|
[2087] | 360 | float v_base_quality = self->encoder.v_base_quality; |
---|
[1238] | 361 | char val[LIBROAR_BUFFER_MSGDATA]; |
---|
| 362 | |
---|
| 363 | val[LIBROAR_BUFFER_MSGDATA-1] = 0; |
---|
| 364 | |
---|
[1226] | 365 | memset(&(self->encoder), 0, sizeof(self->encoder)); |
---|
| 366 | |
---|
| 367 | self->encoding = 1; |
---|
[2087] | 368 | self->encoder.srn = srn + 1; |
---|
| 369 | self->encoder.v_base_quality = v_base_quality; |
---|
[1226] | 370 | |
---|
| 371 | vorbis_info_init(&(self->encoder.vi)); |
---|
| 372 | vorbis_comment_init(&(self->encoder.vc)); |
---|
| 373 | vorbis_comment_add_tag(&(self->encoder.vc), "SERVER", "RoarAudio"); |
---|
| 374 | vorbis_comment_add_tag(&(self->encoder.vc), "ENCODER", "RoarAudio Vorbis codecfilter"); |
---|
| 375 | |
---|
[1493] | 376 | #ifdef ROAR_SUPPORT_META |
---|
[1238] | 377 | if ( (len = stream_meta_list(sid, types, ROAR_META_MAX_PER_STREAM)) != -1 ) { |
---|
| 378 | for (i = 0; i < len; i++) { |
---|
| 379 | //int stream_meta_get (int id, int type, char * name, char * val, size_t len); |
---|
| 380 | if ( stream_meta_get(sid, types[i], NULL, val, LIBROAR_BUFFER_MSGDATA-1) == 0 ) |
---|
| 381 | vorbis_comment_add_tag(&(self->encoder.vc), roar_meta_strtype(types[i]), val); |
---|
| 382 | } |
---|
| 383 | } |
---|
[1493] | 384 | #endif |
---|
[1238] | 385 | |
---|
[2087] | 386 | ROAR_DBG("cf_vorbis_encode_start(*): q=%f", v_base_quality); |
---|
| 387 | |
---|
[1226] | 388 | if( vorbis_encode_init_vbr(&(self->encoder.vi), (long) ROAR_STREAM(self->stream)->info.channels, |
---|
| 389 | (long) ROAR_STREAM(self->stream)->info.rate, |
---|
[2087] | 390 | v_base_quality) != 0 ) { |
---|
[1226] | 391 | ROAR_ERR("cf_vorbis_encode_start(*): Can not vorbis_encode_init_vbr(*)!"); |
---|
| 392 | vorbis_info_clear(&(self->encoder.vi)); // TODO: do we need to free vc also? |
---|
| 393 | return -1; |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | vorbis_analysis_init(&(self->encoder.vd), &(self->encoder.vi)); |
---|
| 397 | vorbis_block_init(&(self->encoder.vd), &(self->encoder.vb)); |
---|
[362] | 398 | |
---|
[1239] | 399 | ROAR_DBG("cf_vorbis_encode_start(*): srn=%i", self->encoder.srn); |
---|
[1226] | 400 | // "RA"<<16 + PID<<8 + Stream ID |
---|
[1238] | 401 | ogg_stream_init(&(self->encoder.os), |
---|
[1239] | 402 | (((0x5241 + self->encoder.srn) & 0xffff)<<16) + |
---|
| 403 | ( (getpid() & 0x00ff)<< 8) + |
---|
| 404 | ( sid & 0x00ff)); |
---|
[1226] | 405 | return 0; |
---|
| 406 | #else |
---|
| 407 | return -1; |
---|
| 408 | #endif |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | int cf_vorbis_encode_end (struct codecfilter_vorbis_inst * self) { |
---|
| 412 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
| 413 | if ( self->encoding ) { |
---|
[1238] | 414 | // try to flush up to an EOS page... |
---|
| 415 | vorbis_analysis_buffer(&(self->encoder.vd), 2*ROAR_MAX_CHANNELS); |
---|
| 416 | vorbis_analysis_wrote(&(self->encoder.vd), 0); |
---|
| 417 | cf_vorbis_encode_flushout(self); |
---|
| 418 | |
---|
| 419 | // clean up... |
---|
[1226] | 420 | ogg_stream_clear(&(self->encoder.os)); |
---|
| 421 | vorbis_block_clear(&(self->encoder.vb)); |
---|
| 422 | vorbis_dsp_clear(&(self->encoder.vd)); |
---|
| 423 | vorbis_info_clear(&(self->encoder.vi)); |
---|
| 424 | self->opened = 0; |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | return 0; |
---|
| 428 | #else |
---|
| 429 | return -1; |
---|
| 430 | #endif |
---|
| 431 | } |
---|
| 432 | |
---|
[1238] | 433 | int cf_vorbis_encode_flushout(struct codecfilter_vorbis_inst * self) { |
---|
| 434 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
| 435 | while ( vorbis_analysis_blockout(&(self->encoder.vd), &(self->encoder.vb)) == 1 ) { |
---|
| 436 | vorbis_analysis(&(self->encoder.vb), &(self->encoder.op)); |
---|
| 437 | vorbis_bitrate_addblock(&(self->encoder.vb)); |
---|
| 438 | |
---|
| 439 | while ( vorbis_bitrate_flushpacket(&(self->encoder.vd), &(self->encoder.op)) ) { |
---|
| 440 | ogg_stream_packetin(&(self->encoder.os), &(self->encoder.op)); |
---|
| 441 | |
---|
| 442 | while( ogg_stream_pageout(&(self->encoder.os), &(self->encoder.og)) ) { |
---|
| 443 | if ( |
---|
| 444 | stream_vio_s_write(self->stream, self->encoder.og.header, self->encoder.og.header_len) == -1 || |
---|
| 445 | stream_vio_s_write(self->stream, self->encoder.og.body, self->encoder.og.body_len ) == -1 ) { |
---|
| 446 | return -1; |
---|
| 447 | } |
---|
| 448 | } |
---|
| 449 | } |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | return 0; |
---|
| 453 | #else |
---|
| 454 | return -1; |
---|
| 455 | #endif |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | int cf_vorbis_ctl(CODECFILTER_USERDATA_T inst, int cmd, void * data) { |
---|
| 459 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
[2084] | 460 | int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK; |
---|
| 461 | |
---|
| 462 | cmd -= type; |
---|
[1238] | 463 | |
---|
| 464 | switch (cmd) { |
---|
| 465 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_META_UPDATE): |
---|
[2084] | 466 | if ( type != ROAR_STREAM_CTL_TYPE_VOID ) |
---|
| 467 | return -1; |
---|
| 468 | |
---|
[1239] | 469 | ROAR_DBG("cf_vorbis_ctl(*): stoping stream..."); |
---|
[1238] | 470 | if ( cf_vorbis_encode_end(self) == -1 ) |
---|
| 471 | return -1; |
---|
[1239] | 472 | ROAR_DBG("cf_vorbis_ctl(*): restarting stream..."); |
---|
[1238] | 473 | if ( cf_vorbis_encode_start(self) == -1 ) |
---|
| 474 | return -1; |
---|
| 475 | |
---|
| 476 | return 0; |
---|
| 477 | break; |
---|
[2084] | 478 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_SET_Q): |
---|
| 479 | if ( type != ROAR_STREAM_CTL_TYPE_FLOAT ) |
---|
| 480 | return -1; |
---|
| 481 | |
---|
[2087] | 482 | ROAR_DBG("cf_vorbis_ctl(*): setting quality to q=%f", *(float*)data); |
---|
| 483 | |
---|
| 484 | self->encoder.v_base_quality = *(float*)data / 10; |
---|
[2084] | 485 | |
---|
| 486 | if ( self->encoding ) { |
---|
| 487 | ROAR_DBG("cf_vorbis_ctl(*): we are allready encoding, restart..."); |
---|
| 488 | ROAR_DBG("cf_vorbis_ctl(*): stoping stream..."); |
---|
| 489 | if ( cf_vorbis_encode_end(self) == -1 ) |
---|
| 490 | return -1; |
---|
| 491 | ROAR_DBG("cf_vorbis_ctl(*): restarting stream..."); |
---|
| 492 | if ( cf_vorbis_encode_start(self) == -1 ) |
---|
| 493 | return -1; |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | return 0; |
---|
| 497 | break; |
---|
[1238] | 498 | default: |
---|
[2087] | 499 | ROAR_DBG("cf_vorbis_ctl(*): Unknown command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
| 500 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
[1238] | 501 | return -1; |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | return -1; |
---|
| 505 | } |
---|
| 506 | |
---|
[1226] | 507 | #endif |
---|
[204] | 508 | //ll |
---|