source: roaraudio/roard/codecfilter_vorbis.c @ 741:9be48345d290

Last change on this file since 741:9be48345d290 was 741:9be48345d290, checked in by phi, 16 years ago

tryed a lot with libvorbisfile to use stream_vio*, seems to to work very well

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