1 | //codecfilter_vorbis.c: |
---|
2 | |
---|
3 | #include "roard.h" |
---|
4 | |
---|
5 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
6 | |
---|
7 | int cf_vorbis_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
8 | struct roar_stream_server * info, |
---|
9 | struct roar_codecfilter * filter) { |
---|
10 | struct codecfilter_vorbis_inst * self = malloc(sizeof(struct codecfilter_vorbis_inst)); |
---|
11 | struct roar_stream * s = ROAR_STREAM(info); |
---|
12 | |
---|
13 | if ( !self ) |
---|
14 | return -1; |
---|
15 | |
---|
16 | self->current_section = -1; |
---|
17 | self->last_section = -1; |
---|
18 | self->opened = 0; |
---|
19 | self->got_it_running = 0; |
---|
20 | self->stream = info; |
---|
21 | // self->outlen = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
22 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
23 | self->encoding = 0; |
---|
24 | #endif |
---|
25 | |
---|
26 | ROAR_DBG("cf_vorbis_open(*): info->id=%i", ROAR_STREAM(info)->id); |
---|
27 | |
---|
28 | if ( (self->in = fdopen(s->fh, "r")) == NULL ) { |
---|
29 | free((void*)self); |
---|
30 | return -1; |
---|
31 | } |
---|
32 | |
---|
33 | *inst = (CODECFILTER_USERDATA_T) self; |
---|
34 | |
---|
35 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
36 | s->info.bits = 16; |
---|
37 | |
---|
38 | if ( s->dir == ROAR_DIR_PLAY ) { |
---|
39 | return 0; |
---|
40 | } else if ( s->dir == ROAR_DIR_MONITOR ) { |
---|
41 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
42 | // set up the encoder here |
---|
43 | |
---|
44 | memset(&(self->encoder), 0, sizeof(self->encoder)); |
---|
45 | |
---|
46 | self->encoding = 1; |
---|
47 | |
---|
48 | vorbis_info_init(&(self->encoder.vi)); |
---|
49 | vorbis_comment_init(&(self->encoder.vc)); |
---|
50 | vorbis_comment_add_tag(&(self->encoder.vc), "SERVER", "RoarAudio"); |
---|
51 | vorbis_comment_add_tag(&(self->encoder.vc), "ENCODER", "RoarAudio Vorbis codecfilter"); |
---|
52 | |
---|
53 | if( vorbis_encode_init_vbr(&(self->encoder.vi), (long) s->info.channels, (long) s->info.rate, |
---|
54 | self->encoder.v_base_quality) != 0 ) { |
---|
55 | ROAR_ERR("cf_vorbis_open(*): Can not vorbis_encode_init_vbr(*)!"); |
---|
56 | vorbis_info_clear(&(self->encoder.vi)); // TODO: do we need to free vc also? |
---|
57 | free(self); |
---|
58 | return -1; |
---|
59 | } |
---|
60 | |
---|
61 | vorbis_analysis_init(&(self->encoder.vd), &(self->encoder.vi)); |
---|
62 | vorbis_block_init(&(self->encoder.vd), &(self->encoder.vb)); |
---|
63 | |
---|
64 | // "RA"<<16 + PID<<8 + Stream ID |
---|
65 | ogg_stream_init(&(self->encoder.os), 0x52410000 + ((getpid() & 0xff)<<8) + s->id); |
---|
66 | |
---|
67 | #else |
---|
68 | free(self); |
---|
69 | return -1; |
---|
70 | #endif |
---|
71 | } else { |
---|
72 | free(self); |
---|
73 | return -1; |
---|
74 | } |
---|
75 | |
---|
76 | return 0; |
---|
77 | } |
---|
78 | |
---|
79 | int cf_vorbis_close(CODECFILTER_USERDATA_T inst) { |
---|
80 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
81 | |
---|
82 | if ( !inst ) |
---|
83 | return -1; |
---|
84 | |
---|
85 | if ( self->got_it_running ) |
---|
86 | ov_clear(&(self->vf)); |
---|
87 | |
---|
88 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
89 | if ( self->encoding ) { |
---|
90 | ogg_stream_clear(&(self->encoder.os)); |
---|
91 | vorbis_block_clear(&(self->encoder.vb)); |
---|
92 | vorbis_dsp_clear(&(self->encoder.vd)); |
---|
93 | vorbis_info_clear(&(self->encoder.vi)); |
---|
94 | } |
---|
95 | #endif |
---|
96 | |
---|
97 | free(inst); |
---|
98 | return 0; |
---|
99 | } |
---|
100 | |
---|
101 | int cf_vorbis_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
102 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
103 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
104 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
105 | ogg_packet header; |
---|
106 | ogg_packet header_comm; |
---|
107 | ogg_packet header_code; |
---|
108 | float ** encbuf; |
---|
109 | int i, c; |
---|
110 | int chans; |
---|
111 | int end; |
---|
112 | int16_t * data = (int16_t *) buf; |
---|
113 | |
---|
114 | if ( ! self->opened ) { |
---|
115 | vorbis_analysis_headerout(&(self->encoder.vd), &(self->encoder.vc), &header, &header_comm, &header_code); |
---|
116 | |
---|
117 | ogg_stream_packetin(&(self->encoder.os), &header); |
---|
118 | ogg_stream_packetin(&(self->encoder.os), &header_comm); |
---|
119 | ogg_stream_packetin(&(self->encoder.os), &header_code); |
---|
120 | |
---|
121 | while (ogg_stream_flush(&(self->encoder.os), &(self->encoder.og))) { |
---|
122 | if ( stream_vio_s_write(self->stream, self->encoder.og.header, self->encoder.og.header_len) |
---|
123 | != self->encoder.og.header_len || |
---|
124 | stream_vio_s_write(self->stream, self->encoder.og.body, self->encoder.og.body_len ) |
---|
125 | != self->encoder.og.body_len ) { |
---|
126 | free(self); // TODO: do we need addional cleanup? |
---|
127 | return -1; |
---|
128 | } |
---|
129 | } |
---|
130 | self->opened = 1; |
---|
131 | } else { |
---|
132 | encbuf = vorbis_analysis_buffer(&(self->encoder.vd), len /* TODO: need to lookup the menaing of this */); |
---|
133 | chans = s->info.channels; |
---|
134 | end = len/(2*chans); |
---|
135 | |
---|
136 | if ( chans == 1 ) { // use optimized code |
---|
137 | for (i = 0; i < end; i++) |
---|
138 | encbuf[0][i] = data[i]/32768.0; |
---|
139 | |
---|
140 | } else if ( chans == 2 ) { // use optimized code |
---|
141 | for (i = 0; i < end; i++) { |
---|
142 | encbuf[0][i] = data[2*i ]/32768.0; |
---|
143 | encbuf[1][i] = data[2*i+1]/32768.0; |
---|
144 | } |
---|
145 | } else { // use generic multi channel code |
---|
146 | for (i = 0; i < end; i++) { |
---|
147 | for (c = 0; c < chans; c++) { |
---|
148 | encbuf[c][i] = data[chans*i+c]/32768.0; |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | vorbis_analysis_wrote(&(self->encoder.vd), i); |
---|
154 | |
---|
155 | while ( vorbis_analysis_blockout(&(self->encoder.vd), &(self->encoder.vb)) == 1 ) { |
---|
156 | vorbis_analysis(&(self->encoder.vb), &(self->encoder.op)); |
---|
157 | vorbis_bitrate_addblock(&(self->encoder.vb)); |
---|
158 | |
---|
159 | while ( vorbis_bitrate_flushpacket(&(self->encoder.vd), &(self->encoder.op)) ) { |
---|
160 | ogg_stream_packetin(&(self->encoder.os), &(self->encoder.op)); |
---|
161 | |
---|
162 | while( ogg_stream_pageout(&(self->encoder.os), &(self->encoder.og)) ) { |
---|
163 | if ( |
---|
164 | stream_vio_s_write(self->stream, self->encoder.og.header, self->encoder.og.header_len) == -1 || |
---|
165 | stream_vio_s_write(self->stream, self->encoder.og.body, self->encoder.og.body_len ) == -1 ) { |
---|
166 | return -1; |
---|
167 | } |
---|
168 | } |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | return len; // we assume every thing was written (at least into our dsp anaylises buffer |
---|
174 | #else |
---|
175 | errno = ENOSYS; |
---|
176 | return -1; |
---|
177 | #endif |
---|
178 | } |
---|
179 | int cf_vorbis_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
180 | struct codecfilter_vorbis_inst * self = (struct codecfilter_vorbis_inst *) inst; |
---|
181 | long r; |
---|
182 | long todo = len; |
---|
183 | long done = 0; |
---|
184 | |
---|
185 | // printf("cf_vorbis_read(inst=%p, buf=%p, len=%i) = ?\n", inst, buf, len); |
---|
186 | |
---|
187 | self->opened++; |
---|
188 | if ( self->opened == 16 ) { |
---|
189 | //printf("cf_vorbis_read(*): opening...\n"); |
---|
190 | if ( ov_open(self->in, &(self->vf), NULL, 0) < 0 ) { |
---|
191 | // free((void*)self); |
---|
192 | return 0; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | if ( self->opened < 16 ) { |
---|
197 | errno = EAGAIN; |
---|
198 | return -1; |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | self->got_it_running = 1; |
---|
203 | |
---|
204 | while (todo) { |
---|
205 | r = ov_read(&(self->vf), buf+done, todo, 0, 2, 1, &(self->current_section)); |
---|
206 | if ( r < 1 ) { |
---|
207 | break; |
---|
208 | } else { |
---|
209 | if ( self->last_section != self->current_section ) |
---|
210 | if ( cf_vorbis_update_stream(self) == -1 ) |
---|
211 | return 0; |
---|
212 | |
---|
213 | self->last_section = self->current_section; |
---|
214 | todo -= r; |
---|
215 | done += r; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | //printf("ov_read(*) = %i\n", done); |
---|
220 | |
---|
221 | if ( done == 0 ) { |
---|
222 | // do some EOF handling... |
---|
223 | return 0; |
---|
224 | } else { |
---|
225 | return len; |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | int cf_vorbis_update_stream (struct codecfilter_vorbis_inst * self) { |
---|
230 | vorbis_info *vi = ov_info(&(self->vf), -1); |
---|
231 | char **ptr = ov_comment(&(self->vf), -1)->user_comments; |
---|
232 | char key[80] = {0}, value[80] = {0}; |
---|
233 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
234 | int type; |
---|
235 | int j, h = 0; |
---|
236 | float rpg_track = 0, rpg_album = 0; |
---|
237 | |
---|
238 | s->info.channels = vi->channels; |
---|
239 | s->info.rate = vi->rate; |
---|
240 | s->info.bits = 16; |
---|
241 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
242 | |
---|
243 | stream_meta_clear(s->id); |
---|
244 | |
---|
245 | while(*ptr){ |
---|
246 | for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) |
---|
247 | key[j] = (*ptr)[j]; |
---|
248 | key[j] = 0; |
---|
249 | |
---|
250 | for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) |
---|
251 | value[h++] = (*ptr)[j]; |
---|
252 | value[h] = 0; |
---|
253 | |
---|
254 | type = roar_meta_inttype(key); |
---|
255 | if ( type != -1 ) |
---|
256 | stream_meta_set(s->id, type, "", value); |
---|
257 | |
---|
258 | ROAR_DBG("cf_vorbis_update_stream(*): Meta %-16s: %s", key, value); |
---|
259 | |
---|
260 | if ( strcmp(key, "REPLAYGAIN_TRACK_PEAK") == 0 ) { |
---|
261 | rpg_track = 1/atof(value); |
---|
262 | /* |
---|
263 | } else if ( strcmp(key, "REPLAYGAIN_TRACK_GAIN") == 0 ) { |
---|
264 | rpg_track = powf(10, atof(value)/20); |
---|
265 | */ |
---|
266 | } else if ( strcmp(key, "REPLAYGAIN_ALBUM_PEAK") == 0 ) { |
---|
267 | rpg_album = 1/atof(value); |
---|
268 | /* |
---|
269 | } else if ( strcmp(key, "REPLAYGAIN_ALBUM_GAIN") == 0 ) { |
---|
270 | rpg_album = powf(10, atof(value)/20); |
---|
271 | */ |
---|
272 | } |
---|
273 | |
---|
274 | ++ptr; |
---|
275 | } |
---|
276 | |
---|
277 | if ( rpg_album ) { |
---|
278 | self->stream->mixer.rpg_div = 2718; // = int(exp(1)*1000) |
---|
279 | self->stream->mixer.rpg_mul = (float)rpg_album*2718; |
---|
280 | } else if ( rpg_track ) { |
---|
281 | self->stream->mixer.rpg_div = 2718; |
---|
282 | self->stream->mixer.rpg_mul = (float)rpg_track*2718; |
---|
283 | } |
---|
284 | |
---|
285 | //printf("RPG: mul=%i, div=%i\n", self->stream->mixer.rpg_mul, self->stream->mixer.rpg_div); |
---|
286 | return 0; |
---|
287 | } |
---|
288 | |
---|
289 | #endif |
---|
290 | |
---|
291 | //ll |
---|