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