source: roaraudio/roard/codecfilter_vorbis.c @ 2107:2dafbf9739f2

Last change on this file since 2107:2dafbf9739f2 was 2107:2dafbf9739f2, checked in by phi, 15 years ago

added support for 8/16/32 bit vorbis encoding

File size: 14.4 KB
Line 
1//codecfilter_vorbis.c:
2
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
25#define ROAR_REQUIRE_LIBVORBISFILE
26
27#include "roard.h"
28
29#ifdef ROAR_HAVE_LIBVORBISFILE
30
31#define FIFAC ((float)((uint64_t)1<<(ROAR_VORBIS_BITS-1)))
32
33int _g_cf_vorbis_vfvio_return_err (void) {
34 return -1;
35}
36
37ov_callbacks _g_cf_vorbis_vfvio = {
38  .read_func  = cf_vorbis_vfvio_read,
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
42};
43
44size_t cf_vorbis_vfvio_read (void *ptr, size_t size, size_t nmemb, void *datasource) {
45 ssize_t r;
46
47 r = stream_vio_s_read(ROAR_STREAM_SERVER(datasource), ptr, size*nmemb);
48
49 ROAR_DBG("cf_vorbis_vfvio_read(ptr=%p, size=%lu, nmemb=%lu, datasource=%p): r=%i", ptr, size, nmemb, datasource, r);
50
51 if ( r == -1 )
52  return 0;
53
54 if ( r > 0 )
55  errno = 0;
56
57 r /= size;
58 
59 ROAR_DBG("cf_vorbis_vfvio_read(ptr=%p, size=%lu, nmemb=%lu, datasource=%p) = %i", ptr, size, nmemb, datasource, r);
60 return r;
61}
62
63int 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));
67 struct roar_stream * s = ROAR_STREAM(info);
68
69 if ( !self )
70  return -1;
71
72 self->current_section      = -1;
73 self->last_section         = -1;
74 self->opened               =  0;
75 self->got_it_running       =  0;
76 self->stream               = info;
77// self->outlen               = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
78#ifdef ROAR_HAVE_LIBVORBISENC
79 self->encoding               = 0;
80 self->encoder.v_base_quality = 0.3;
81 self->encoder.srn            = -1;
82#endif
83
84 ROAR_DBG("cf_vorbis_open(*): info->id=%i", ROAR_STREAM(info)->id);
85
86 *inst = (CODECFILTER_USERDATA_T) self;
87
88 s->info.codec = ROAR_CODEC_DEFAULT;
89 s->info.bits  = 16;
90
91 if ( s->dir == ROAR_DIR_PLAY ) {
92  return 0;
93 } else if ( s->dir == ROAR_DIR_MONITOR || s->dir == ROAR_DIR_OUTPUT ) {
94#ifdef ROAR_HAVE_LIBVORBISENC
95  // set up the encoder here
96// this is delayed to the write function
97/*
98 if ( cf_vorbis_encode_start(self) == -1 ) {
99  free(self);
100  return -1;
101 }
102*/
103 s->info.bits  = ROAR_VORBIS_BITS;
104#else
105 free(self);
106 return -1;
107#endif
108 } else {
109  free(self);
110  return -1;
111 }
112
113 return 0;
114}
115
116int 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
122 if ( self->got_it_running )
123  ov_clear(&(self->vf));
124
125#ifdef ROAR_HAVE_LIBVORBISENC
126 if ( self->encoding ) {
127  cf_vorbis_encode_end(self);
128 }
129#endif
130
131 free(inst);
132 return 0;
133}
134
135int 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;
142 float ** encbuf;
143 int i, c;
144 int chans;
145 int end;
146#if ROAR_VORBIS_BITS == 8
147 int8_t  * data = (int8_t  *) buf;
148#elif ROAR_VORBIS_BITS == 16
149 int16_t * data = (int16_t *) buf;
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
155
156 if ( ! self->opened ) {
157  if ( !self->encoding ) {
158   if ( cf_vorbis_encode_start(self) == -1 ) {
159    return -1;
160   }
161  }
162
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))) {
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     ) {
174    free(self); // TODO: do we need addional cleanup?
175    return -1;
176   }
177  }
178  self->opened = 1;
179 } else {
180  encbuf = vorbis_analysis_buffer(&(self->encoder.vd), len /* TODO: need to lookup the menaing of this */);
181  chans  = s->info.channels;
182  end    = len*8/(ROAR_VORBIS_BITS*chans);
183
184  if ( chans == 1 ) { // use optimized code
185   for (i = 0; i < end; i++)
186    encbuf[0][i] = data[i]/FIFAC;
187
188  } else if ( chans == 2 ) { // use optimized code
189   for (i = 0; i < end; i++) {
190    encbuf[0][i] = data[2*i  ]/FIFAC;
191    encbuf[1][i] = data[2*i+1]/FIFAC;
192   }
193  } else { // use generic multi channel code
194   for (i = 0; i < end; i++) {
195    for (c = 0; c < chans; c++) {
196     encbuf[c][i] = data[chans*i+c]/FIFAC;
197    }
198   }
199  }
200
201  vorbis_analysis_wrote(&(self->encoder.vd), i);
202
203  if ( cf_vorbis_encode_flushout(self) == -1 )
204   return -1;
205 }
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}
213int 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;
216 long todo = len;
217 long done = 0;
218
219// printf("cf_vorbis_read(inst=%p, buf=%p, len=%i) = ?\n", inst, buf, len);
220
221 self->opened++;
222 if ( self->opened == 16 ) {
223
224  //printf("cf_vorbis_read(*): opening...\n");
225  if ( ov_open_callbacks((void*)self->stream, &(self->vf), NULL, 0, _g_cf_vorbis_vfvio) < 0 ) {
226   return 0;
227  }
228  errno = EAGAIN;
229  return -1;
230 }
231
232 if ( self->opened < 16 ) {
233  errno = EAGAIN;
234  return -1;
235 }
236
237
238 self->got_it_running = 1;
239
240 while (todo) {
241  r = ov_read(&(self->vf), buf+done, todo, 0, 2, 1, &(self->current_section));
242  if ( r == OV_HOLE ) {
243   ROAR_DBG("cf_vorbis_read(*): Hole in stream");
244  } else if ( r < 1 ) {
245   break;
246  } else {
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;
252   todo -= r;
253   done += r;
254  }
255 }
256
257//printf("ov_read(*) = %i\n", done);
258
259 if ( done == 0 ) {
260  // do some EOF handling...
261  return 0;
262 } else {
263  return len;
264 }
265}
266
267int cf_vorbis_update_stream (struct codecfilter_vorbis_inst * self) {
268#ifdef ROAR_SUPPORT_META
269 vorbis_info *vi = ov_info(&(self->vf), -1);
270 char **ptr = ov_comment(&(self->vf), -1)->user_comments;
271 char key[ROAR_META_MAX_NAMELEN] = {0}, value[LIBROAR_BUFFER_MSGDATA] = {0};
272 struct roar_stream * s = ROAR_STREAM(self->stream);
273 int type;
274 int j, h = 0;
275 float rpg_track = 0, rpg_album = 0;
276 int meta_ok;
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){
286  meta_ok = 1;
287
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;
298
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);
303      meta_ok = 0;
304      h = 0;
305      break;
306     }
307     value[h++] = (*ptr)[j];
308    }
309    value[h]   = 0;
310   }
311
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++;
335 }
336
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
345 stream_meta_finalize(s->id);
346#endif
347 //printf("RPG: mul=%i, div=%i\n", self->stream->mixer.rpg_mul, self->stream->mixer.rpg_div);
348 return 0;
349}
350
351int cf_vorbis_encode_start  (struct codecfilter_vorbis_inst * self) {
352#ifdef ROAR_HAVE_LIBVORBISENC
353 int srn = self->encoder.srn; // this value is allrady inited...
354#ifdef ROAR_SUPPORT_META
355 int len = 0;
356 int i;
357 int types[ROAR_META_MAX_PER_STREAM];
358#endif
359 int sid = ROAR_STREAM(self->stream)->id;
360 float v_base_quality = self->encoder.v_base_quality;
361 char val[LIBROAR_BUFFER_MSGDATA];
362
363  val[LIBROAR_BUFFER_MSGDATA-1] = 0;
364
365  memset(&(self->encoder), 0, sizeof(self->encoder));
366
367  self->encoding = 1;
368  self->encoder.srn = srn + 1;
369  self->encoder.v_base_quality = v_base_quality;
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
376#ifdef ROAR_SUPPORT_META
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  }
384#endif
385
386  ROAR_DBG("cf_vorbis_encode_start(*): q=%f", v_base_quality);
387
388  if( vorbis_encode_init_vbr(&(self->encoder.vi), (long) ROAR_STREAM(self->stream)->info.channels,
389                                                  (long) ROAR_STREAM(self->stream)->info.rate,
390                                                  v_base_quality) != 0 ) {
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));
398
399  ROAR_DBG("cf_vorbis_encode_start(*): srn=%i", self->encoder.srn);
400                                     //  "RA"<<16 + PID<<8 + Stream ID
401  ogg_stream_init(&(self->encoder.os),
402                   (((0x5241 + self->encoder.srn) & 0xffff)<<16) +
403                   ( (getpid()                    & 0x00ff)<< 8) +
404                   (  sid                         & 0x00ff));
405 return 0;
406#else
407 return -1;
408#endif
409}
410
411int cf_vorbis_encode_end    (struct codecfilter_vorbis_inst * self) {
412#ifdef ROAR_HAVE_LIBVORBISENC
413 if ( self->encoding ) {
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...
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
433int 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
458int cf_vorbis_ctl(CODECFILTER_USERDATA_T   inst, int cmd, void * data) {
459 struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst;
460 int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK;
461
462 cmd -= type;
463
464 switch (cmd) {
465  case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_META_UPDATE):
466    if ( type != ROAR_STREAM_CTL_TYPE_VOID )
467     return -1;
468
469    ROAR_DBG("cf_vorbis_ctl(*): stoping stream...");
470    if ( cf_vorbis_encode_end(self) == -1 )
471     return -1;
472    ROAR_DBG("cf_vorbis_ctl(*): restarting stream...");
473    if ( cf_vorbis_encode_start(self) == -1 )
474     return -1;
475
476    return 0;
477   break;
478  case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_SET_Q):
479    if ( type != ROAR_STREAM_CTL_TYPE_FLOAT )
480     return -1;
481
482    ROAR_DBG("cf_vorbis_ctl(*): setting quality to q=%f", *(float*)data);
483
484    self->encoder.v_base_quality = *(float*)data / 10;
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;
498  default:
499    ROAR_DBG("cf_vorbis_ctl(*): Unknown command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x",
500                    cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd));
501    return -1;
502 }
503
504 return -1;
505}
506
507#endif
508//ll
Note: See TracBrowser for help on using the repository browser.