source: roaraudio/roard/codecfilter_vorbis.c @ 4987:9335631d4e00

Last change on this file since 4987:9335631d4e00 was 4987:9335631d4e00, checked in by phi, 13 years ago

get codecfilter for ogg_vorbis working again

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