1 | //codecfilter.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 | struct roar_codecfilter g_codecfilter[] = { |
---|
30 | {-1, "null", "null codec filter", NULL, ROAR_CODECFILTER_NONE, |
---|
31 | NULL, NULL, NULL, NULL, NULL, NULL, NULL}, |
---|
32 | |
---|
33 | /* |
---|
34 | #ifdef ROAR_HAVE_LIBSNDFILE |
---|
35 | {ROAR_CODEC_RIFF_WAVE, "sndfile", "libsndfile codec filter", NULL, ROAR_CODECFILTER_READ|ROAR_CODECFILTER_WRITE, |
---|
36 | cf_sndfile_open, cf_sndfile_close, NULL, cf_sndfile_write, cf_sndfile_read, NULL}, |
---|
37 | #else |
---|
38 | */ |
---|
39 | {ROAR_CODEC_RIFF_WAVE, "RIFF/WAVE", "RIFF/WAVE", NULL, ROAR_CODECFILTER_READ|ROAR_CODECFILTER_WRITE, |
---|
40 | cf_wave_open, cf_wave_close, NULL, cf_wave_write, cf_wave_read, NULL, NULL}, |
---|
41 | //#endif |
---|
42 | |
---|
43 | #ifdef ROAR_SUPPORT_ALAW |
---|
44 | {ROAR_CODEC_ALAW, "alaw", "A-Law", NULL, |
---|
45 | #ifdef ROAR_SUPPORT_ALAW_RW |
---|
46 | ROAR_CODECFILTER_READ|ROAR_CODECFILTER_WRITE, |
---|
47 | #else |
---|
48 | ROAR_CODECFILTER_READ, |
---|
49 | #endif |
---|
50 | cf_alaw_open, cf_alaw_close, NULL, |
---|
51 | #ifdef ROAR_SUPPORT_ALAW_RW |
---|
52 | cf_alaw_write, |
---|
53 | #else |
---|
54 | NULL, |
---|
55 | #endif |
---|
56 | cf_alaw_read, NULL, cf_alaw_delay}, |
---|
57 | #endif |
---|
58 | |
---|
59 | #ifdef ROAR_SUPPORT_MULAW |
---|
60 | {ROAR_CODEC_MULAW, "mulaw", "mu-Law", NULL, |
---|
61 | #ifdef ROAR_SUPPORT_MULAW_RW |
---|
62 | ROAR_CODECFILTER_READ|ROAR_CODECFILTER_WRITE, |
---|
63 | #else |
---|
64 | ROAR_CODECFILTER_READ, |
---|
65 | #endif |
---|
66 | cf_mulaw_open, cf_mulaw_close, NULL, |
---|
67 | #ifdef ROAR_SUPPORT_MULAW_RW |
---|
68 | cf_mulaw_write, |
---|
69 | #else |
---|
70 | NULL, |
---|
71 | #endif |
---|
72 | cf_mulaw_read, NULL, cf_alaw_delay}, |
---|
73 | #endif |
---|
74 | |
---|
75 | #ifndef ROAR_WITHOUT_CF_CMD |
---|
76 | #ifdef ROAR_HAVE_BIN_OGG123 |
---|
77 | {ROAR_CODEC_OGG_GENERAL, "cmd", "ogg123", |
---|
78 | ROAR_HAVE_BIN_OGG123 " -q -d raw -f - -", ROAR_CODECFILTER_READ, |
---|
79 | cf_cmd_open, NULL, NULL, NULL, NULL, NULL, codecfilter_delay_fulldyn}, |
---|
80 | #endif |
---|
81 | #endif |
---|
82 | |
---|
83 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
84 | {ROAR_CODEC_OGG_VORBIS, "oggvorbis", "Ogg Vorbis decoder", NULL, |
---|
85 | #ifdef ROAR_HAVE_LIBVORBISENC |
---|
86 | ROAR_CODECFILTER_READ|ROAR_CODECFILTER_WRITE, |
---|
87 | #else |
---|
88 | ROAR_CODECFILTER_READ, |
---|
89 | #endif |
---|
90 | cf_vorbis_open, cf_vorbis_close, NULL, cf_vorbis_write, cf_vorbis_read, NULL, codecfilter_delay_fulldyn}, |
---|
91 | #else |
---|
92 | #ifndef ROAR_WITHOUT_CF_CMD |
---|
93 | #ifdef ROAR_HAVE_BIN_OGG123 |
---|
94 | {ROAR_CODEC_OGG_VORBIS, "cmd", "ogg123", |
---|
95 | ROAR_HAVE_BIN_OGG123 " -q -d raw -f - -", ROAR_CODECFILTER_READ, |
---|
96 | cf_cmd_open, NULL, NULL, NULL, NULL, NULL, codecfilter_delay_fulldyn}, |
---|
97 | #endif |
---|
98 | #endif |
---|
99 | #endif |
---|
100 | |
---|
101 | #ifdef ROAR_HAVE_LIBFISHSOUND |
---|
102 | {ROAR_CODEC_OGG_SPEEX, "fishsound", "libfishsound Xiph Codec library", |
---|
103 | NULL, ROAR_CODECFILTER_READ, |
---|
104 | cf_fishsound_open, cf_fishsound_close, NULL, NULL, cf_fishsound_read, NULL, codecfilter_delay_fulldyn}, |
---|
105 | |
---|
106 | {ROAR_CODEC_OGG_FLAC, "fishsound", "libfishsound Xiph Codec library", |
---|
107 | NULL, ROAR_CODECFILTER_READ, |
---|
108 | cf_fishsound_open, cf_fishsound_close, NULL, NULL, cf_fishsound_read, NULL, codecfilter_delay_fulldyn}, |
---|
109 | #endif |
---|
110 | |
---|
111 | #ifndef ROAR_WITHOUT_CF_CMD |
---|
112 | #ifdef ROAR_HAVE_BIN_TIMIDITY |
---|
113 | {ROAR_CODEC_MIDI_FILE, "MIDIFILE", "timidity MIDI synth", |
---|
114 | ROAR_HAVE_BIN_TIMIDITY " -Or1sl -s %R -o - -", ROAR_CODECFILTER_READ, |
---|
115 | cf_cmd_open, NULL, NULL, NULL, NULL, NULL, codecfilter_delay_fulldyn}, |
---|
116 | #endif |
---|
117 | #endif |
---|
118 | |
---|
119 | #ifdef ROAR_HAVE_LIBCELT |
---|
120 | {ROAR_CODEC_ROAR_CELT, "RoarCELT", "RoarAudio CELT", NULL, ROAR_CODECFILTER_READ|ROAR_CODECFILTER_WRITE, |
---|
121 | cf_celt_open, cf_celt_close, NULL, cf_celt_write, cf_celt_read, NULL, cf_celt_delay}, |
---|
122 | #endif |
---|
123 | |
---|
124 | #ifdef ROAR_HAVE_LIBSPEEX |
---|
125 | {ROAR_CODEC_ROAR_SPEEX, "RoarSpeex", "RoarAudio Speex", NULL, ROAR_CODECFILTER_READ|ROAR_CODECFILTER_WRITE, |
---|
126 | cf_speex_open, cf_speex_close, NULL, cf_speex_write, cf_speex_read, NULL, NULL}, |
---|
127 | #endif |
---|
128 | |
---|
129 | #ifndef ROAR_WITHOUT_CF_CMD |
---|
130 | #ifdef ROAR_HAVE_BIN_FLAC |
---|
131 | {ROAR_CODEC_FLAC, "cmd", "flac", |
---|
132 | #if BYTE_ORDER == BIG_ENDIAN |
---|
133 | ROAR_HAVE_BIN_FLAC " --silent --force-raw-format --sign=signed --endian=big -d - -o -", |
---|
134 | #elif BYTE_ORDER == LITTLE_ENDIAN |
---|
135 | ROAR_HAVE_BIN_FLAC " --silent --force-raw-format --sign=signed --endian=little -d - -o -", |
---|
136 | #else |
---|
137 | "false", |
---|
138 | #endif |
---|
139 | ROAR_CODECFILTER_READ, |
---|
140 | cf_cmd_open, NULL, NULL, NULL, NULL, NULL, codecfilter_delay_fulldyn}, |
---|
141 | #endif |
---|
142 | #endif |
---|
143 | |
---|
144 | {-1, NULL, NULL, NULL, ROAR_CODECFILTER_NONE, NULL, NULL, NULL, NULL, NULL, NULL, NULL} // end of list |
---|
145 | }; |
---|
146 | |
---|
147 | void print_codecfilterlist (void) { |
---|
148 | int i; |
---|
149 | int flags; |
---|
150 | char mode[5]; |
---|
151 | char delay[6]; |
---|
152 | uint_least32_t d; |
---|
153 | |
---|
154 | printf(" Codec Filtername Mode Delay - Description\n"); |
---|
155 | printf("------------------------------------------------------\n"); |
---|
156 | |
---|
157 | for (i = 0; g_codecfilter[i].name != NULL; i++) { |
---|
158 | flags = g_codecfilter[i].flags; |
---|
159 | |
---|
160 | if ( flags == ROAR_CODECFILTER_NONE ) { |
---|
161 | strcpy(mode, "none"); |
---|
162 | } else { |
---|
163 | strcpy(mode, " "); |
---|
164 | if ( flags & ROAR_CODECFILTER_READ ) |
---|
165 | mode[0] = 'r'; |
---|
166 | if ( flags & ROAR_CODECFILTER_WRITE ) |
---|
167 | mode[1] = 'w'; |
---|
168 | } |
---|
169 | |
---|
170 | *delay = 0; |
---|
171 | if ( g_codecfilter[i].codec == -1 ) { // null codec filter |
---|
172 | strcpy(delay, "0ms"); |
---|
173 | } else if ( g_codecfilter[i].delay == NULL ) { |
---|
174 | strcpy(delay, "?"); |
---|
175 | } else { |
---|
176 | if ( codecfilter_delay(NULL, i, &d) == -1 ) { |
---|
177 | strcpy(delay, "dyn"); |
---|
178 | } else { |
---|
179 | snprintf(delay, 5, "%ims", d/1000); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | printf(" %-12s %-12s %-4s %-5s - %s\n", |
---|
184 | roar_codec2str(g_codecfilter[i].codec), |
---|
185 | g_codecfilter[i].name, |
---|
186 | mode, |
---|
187 | delay, |
---|
188 | g_codecfilter[i].desc |
---|
189 | ); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | int codecfilter_open (CODECFILTER_USERDATA_T * inst, |
---|
194 | int * codecfilter_id, char * codecfilter /* NOTE: this is not part of struct roar_codecfilter's def! */, |
---|
195 | int codec, struct roar_stream_server * info) { |
---|
196 | int i; |
---|
197 | struct roar_codecfilter * filter = NULL; |
---|
198 | |
---|
199 | *codecfilter_id = -1; |
---|
200 | |
---|
201 | ROAR_DBG("codecfilter_open(*): codecfilter='%s', info->id=%i", codecfilter, ROAR_STREAM(info)->id); |
---|
202 | |
---|
203 | for (i = 0; g_codecfilter[i].name != NULL; i++) { |
---|
204 | if ( g_codecfilter[i].codec == codec ) { |
---|
205 | if ( !codecfilter || strcmp(codecfilter, g_codecfilter[i].name) == 0 ) { |
---|
206 | *codecfilter_id = i; |
---|
207 | filter = &g_codecfilter[i]; |
---|
208 | break; |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | info->codecfilter = *codecfilter_id; |
---|
214 | |
---|
215 | if (*codecfilter_id != -1) { |
---|
216 | if ( filter->open ) { |
---|
217 | if ( (i = filter->open(inst, codec, info, filter)) == -1 ) { |
---|
218 | info->codecfilter = *codecfilter_id = -1; |
---|
219 | } |
---|
220 | return i; |
---|
221 | } |
---|
222 | return 0; |
---|
223 | } |
---|
224 | |
---|
225 | return 0; // we found no filter -> ok |
---|
226 | } |
---|
227 | |
---|
228 | int codecfilter_close(CODECFILTER_USERDATA_T inst, int codecfilter) { |
---|
229 | ROAR_DBG("codecfilter_close(inst=%p, codecfilter=%i) = ?", inst, codecfilter); |
---|
230 | |
---|
231 | if ( codecfilter == -1 ) |
---|
232 | return -1; |
---|
233 | |
---|
234 | if ( g_codecfilter[codecfilter].close ) |
---|
235 | return g_codecfilter[codecfilter].close(inst); |
---|
236 | |
---|
237 | return 0; |
---|
238 | } |
---|
239 | |
---|
240 | int codecfilter_pause(CODECFILTER_USERDATA_T inst, int codecfilter, int newstate) { |
---|
241 | if ( codecfilter == -1 ) |
---|
242 | return -1; |
---|
243 | |
---|
244 | if ( g_codecfilter[codecfilter].pause ) |
---|
245 | return g_codecfilter[codecfilter].pause(inst, newstate); |
---|
246 | |
---|
247 | return 0; |
---|
248 | } |
---|
249 | |
---|
250 | int codecfilter_write(CODECFILTER_USERDATA_T inst, int codecfilter, char * buf, int len) { |
---|
251 | if ( codecfilter == -1 ) |
---|
252 | return -1; |
---|
253 | |
---|
254 | if ( g_codecfilter[codecfilter].write ) |
---|
255 | return g_codecfilter[codecfilter].write(inst, buf, len); |
---|
256 | |
---|
257 | return 0; |
---|
258 | } |
---|
259 | |
---|
260 | int codecfilter_read (CODECFILTER_USERDATA_T inst, int codecfilter, char * buf, int len) { |
---|
261 | if ( codecfilter == -1 ) |
---|
262 | return -1; |
---|
263 | |
---|
264 | errno = 0; |
---|
265 | |
---|
266 | if ( g_codecfilter[codecfilter].read ) |
---|
267 | return g_codecfilter[codecfilter].read(inst, buf, len); |
---|
268 | |
---|
269 | return 0; |
---|
270 | } |
---|
271 | |
---|
272 | int codecfilter_flush(CODECFILTER_USERDATA_T inst, int codecfilter) { |
---|
273 | if ( codecfilter == -1 ) |
---|
274 | return -1; |
---|
275 | |
---|
276 | if ( g_codecfilter[codecfilter].flush ) |
---|
277 | return g_codecfilter[codecfilter].flush(inst); |
---|
278 | |
---|
279 | return 0; |
---|
280 | } |
---|
281 | |
---|
282 | int codecfilter_delay(CODECFILTER_USERDATA_T inst, int codecfilter, uint_least32_t * delay) { |
---|
283 | ROAR_DBG("codecfilter_delay(inst=%p, codecfilter=%i, *delay=?) = ?", inst, codecfilter); |
---|
284 | |
---|
285 | if ( codecfilter == -1 ) |
---|
286 | return -1; |
---|
287 | |
---|
288 | if ( g_codecfilter[codecfilter].delay ) |
---|
289 | return g_codecfilter[codecfilter].delay(inst, delay); |
---|
290 | |
---|
291 | return -1; |
---|
292 | } |
---|
293 | |
---|
294 | int codecfilter_delay_fulldyn(CODECFILTER_USERDATA_T inst, uint_least32_t * delay) { |
---|
295 | *delay = 0; // just to be sure |
---|
296 | return -1; |
---|
297 | } |
---|
298 | |
---|
299 | //ll |
---|