Commit | Line | Data |
---|---|---|
7207a21b MW |
1 | /* |
2 | * This file is part of DisOrder | |
3 | * Copyright (C) 2013 Mark Wooding | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | /** @file server/gstdecode.c | |
19 | * @brief Decode compressed audio files, and apply ReplayGain. | |
20 | */ | |
21 | ||
22 | #include "disorder-server.h" | |
23 | ||
24 | #include "speaker-protocol.h" | |
25 | ||
26 | /* Ugh. It turns out that libxml tries to define a function called | |
27 | * `attribute', and it's included by GStreamer for some unimaginable reason. | |
28 | * So undefine it here. We'll want GCC attributes for special effects, but | |
29 | * can take care of ourselves. | |
30 | */ | |
31 | #undef attribute | |
32 | ||
33 | #include <glib.h> | |
34 | #include <gst/gst.h> | |
35 | #include <gst/app/gstappsink.h> | |
36 | #include <gst/audio/audio.h> | |
37 | ||
16bf32dc | 38 | /* The only applications we have for `attribute' is declaring function |
7207a21b | 39 | * arguments as being unused, because we have a lot of callback functions |
16bf32dc MW |
40 | * which are meant to comply with an externally defined interface; and |
41 | * marking `help' as not returning. | |
7207a21b MW |
42 | */ |
43 | #ifdef __GNUC__ | |
16bf32dc | 44 | # define NORETURN __attribute__((noreturn)) |
7207a21b MW |
45 | # define UNUSED __attribute__((unused)) |
46 | #endif | |
47 | ||
48 | #define END ((void *)0) | |
49 | #define N(v) (sizeof(v)/sizeof(*(v))) | |
50 | ||
51 | static FILE *fp; | |
52 | static const char *file; | |
53 | static GstAppSink *appsink; | |
54 | static GstElement *pipeline; | |
55 | static GMainLoop *loop; | |
ff56519f MW |
56 | static unsigned flags = 0; |
57 | #define f_stream 1u | |
7207a21b MW |
58 | |
59 | #define MODES(_) _("off", OFF) _("track", TRACK) _("album", ALBUM) | |
60 | enum { | |
61 | #define DEFENUM(name, tag) tag, | |
62 | MODES(DEFENUM) | |
63 | #undef DEFENUM | |
64 | NMODES | |
65 | }; | |
66 | static const char *const modes[] = { | |
67 | #define DEFNAME(name, tag) name, | |
68 | MODES(DEFNAME) | |
69 | #undef DEFNAME | |
70 | 0 | |
71 | }; | |
f5ebedb0 MW |
72 | |
73 | static const char *const dithers[] = { | |
74 | "none", "rpdf", "tpdf", "tpdf-hf", 0 | |
75 | }; | |
76 | ||
77 | static const char *const shapes[] = { | |
78 | "none", "error-feedback", "simple", "medium", "high", 0 | |
79 | }; | |
80 | ||
81 | static int dither = -1; | |
7207a21b | 82 | static int mode = ALBUM; |
c915f0d4 | 83 | static int quality = -1; |
f5ebedb0 MW |
84 | static int shape = -1; |
85 | static gdouble fallback = 0.0; | |
7207a21b MW |
86 | |
87 | static struct stream_header hdr; | |
88 | ||
89 | /* Report the pads of an element ELT, as iterated by IT; WHAT is an adjective | |
90 | * phrase describing the pads for use in the output. | |
91 | */ | |
92 | static void report_element_pads(const char *what, GstElement *elt, | |
93 | GstIterator *it) | |
94 | { | |
95 | gchar *cs; | |
c076146f | 96 | #ifdef HAVE_GSTREAMER_0_10 |
7207a21b | 97 | gpointer pad; |
c076146f MW |
98 | #else |
99 | GValue gv; | |
100 | GstPad *pad; | |
101 | GstCaps *caps; | |
102 | #endif | |
7207a21b MW |
103 | |
104 | for(;;) { | |
c076146f | 105 | #ifdef HAVE_GSTREAMER_0_10 |
7207a21b | 106 | switch(gst_iterator_next(it, &pad)) { |
c076146f MW |
107 | #else |
108 | switch(gst_iterator_next(it, &gv)) { | |
109 | #endif | |
7207a21b MW |
110 | case GST_ITERATOR_DONE: |
111 | goto done; | |
112 | case GST_ITERATOR_OK: | |
c076146f | 113 | #ifdef HAVE_GSTREAMER_0_10 |
6c9b376a | 114 | cs = gst_caps_to_string(GST_PAD_CAPS(pad)); |
c076146f MW |
115 | #else |
116 | assert(G_VALUE_HOLDS(&gv, GST_TYPE_PAD)); | |
117 | pad = g_value_get_object(&gv); | |
118 | caps = gst_pad_query_caps(pad, 0); | |
119 | cs = gst_caps_to_string(caps); | |
3d9c9d54 | 120 | gst_caps_unref(caps); |
c076146f | 121 | #endif |
7207a21b MW |
122 | disorder_error(0, " `%s' %s pad: %s", GST_OBJECT_NAME(elt), what, cs); |
123 | g_free(cs); | |
3d9c9d54 | 124 | gst_object_unref(pad); |
7207a21b MW |
125 | break; |
126 | case GST_ITERATOR_RESYNC: | |
127 | gst_iterator_resync(it); | |
128 | break; | |
129 | case GST_ITERATOR_ERROR: | |
130 | disorder_error(0, "<failed to enumerate `%s' %s pads>", | |
131 | GST_OBJECT_NAME(elt), what); | |
132 | goto done; | |
133 | } | |
134 | } | |
135 | ||
136 | done: | |
137 | gst_iterator_free(it); | |
138 | } | |
139 | ||
140 | /* Link together two elements; fail with an approximately useful error | |
141 | * message if it didn't work. | |
142 | */ | |
143 | static void link_elements(GstElement *left, GstElement *right) | |
144 | { | |
145 | /* Try to link things together. */ | |
146 | if(gst_element_link(left, right)) return; | |
147 | ||
148 | /* If this didn't work, it's probably for some really hairy reason, so | |
149 | * provide a bunch of debugging information. | |
150 | */ | |
151 | disorder_error(0, "failed to link GStreamer elements `%s' and `%s'", | |
152 | GST_OBJECT_NAME(left), GST_OBJECT_NAME(right)); | |
153 | report_element_pads("source", left, gst_element_iterate_src_pads(left)); | |
f931733b | 154 | report_element_pads("dest", right, gst_element_iterate_sink_pads(right)); |
7207a21b MW |
155 | disorder_fatal(0, "can't decode `%s'", file); |
156 | } | |
157 | ||
158 | /* The `decoderbin' element (DECODE) has deigned to announce a new PAD. | |
159 | * Maybe we should attach the tag end of our pipeline (starting with the | |
160 | * element U) to it. | |
161 | */ | |
162 | static void decoder_pad_arrived(GstElement *decode, GstPad *pad, gpointer u) | |
163 | { | |
164 | GstElement *tail = u; | |
c076146f | 165 | #ifdef HAVE_GSTREAMER_0_10 |
7207a21b | 166 | GstCaps *caps = gst_pad_get_caps(pad); |
c076146f MW |
167 | #else |
168 | GstCaps *caps = gst_pad_get_current_caps(pad); | |
169 | #endif | |
7207a21b MW |
170 | GstStructure *s; |
171 | guint i, n; | |
172 | const gchar *name; | |
173 | ||
174 | /* The input file could be more or less anything, so this could be any kind | |
175 | * of pad. We're only interested if it's audio, so let's go check. | |
176 | */ | |
177 | for(i = 0, n = gst_caps_get_size(caps); i < n; i++) { | |
178 | s = gst_caps_get_structure(caps, i); | |
179 | name = gst_structure_get_name(s); | |
c076146f MW |
180 | #ifdef HAVE_GSTREAMER_0_10 |
181 | if(strncmp(name, "audio/x-raw-", 12) == 0) | |
182 | #else | |
183 | if(strcmp(name, "audio/x-raw") == 0) | |
184 | #endif | |
185 | goto match; | |
7207a21b | 186 | } |
3d9c9d54 | 187 | goto end; |
7207a21b MW |
188 | |
189 | match: | |
190 | /* Yes, it's audio. Link the two elements together. */ | |
191 | link_elements(decode, tail); | |
192 | ||
193 | /* If requested using the environemnt variable `GST_DEBUG_DUMP_DOT_DIR', | |
194 | * write a dump of the now-completed pipeline. | |
195 | */ | |
196 | GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(pipeline), | |
197 | GST_DEBUG_GRAPH_SHOW_ALL, | |
198 | "disorder-gstdecode"); | |
3d9c9d54 MW |
199 | |
200 | end: | |
201 | gst_caps_unref(caps); | |
7207a21b MW |
202 | } |
203 | ||
204 | /* Prepare the GStreamer pipeline, ready to decode the given FILE. This sets | |
205 | * up the variables `appsink' and `pipeline'. | |
206 | */ | |
207 | static void prepare_pipeline(void) | |
208 | { | |
209 | GstElement *source = gst_element_factory_make("filesrc", "file"); | |
210 | GstElement *decode = gst_element_factory_make("decodebin", "decode"); | |
c915f0d4 MW |
211 | GstElement *resample = gst_element_factory_make("audioresample", |
212 | "resample"); | |
7207a21b MW |
213 | GstElement *convert = gst_element_factory_make("audioconvert", "convert"); |
214 | GstElement *sink = gst_element_factory_make("appsink", "sink"); | |
215 | GstElement *tail = sink; | |
216 | GstElement *gain; | |
c915f0d4 MW |
217 | GstCaps *caps; |
218 | const struct stream_header *fmt = &config->sample_format; | |
7207a21b | 219 | |
c54fec77 MW |
220 | if(!source || !decode || !resample || !convert || !sink) |
221 | disorder_fatal(0, "failed to create GStreamer elements: " | |
222 | "need base and good plugins"); | |
223 | ||
c076146f MW |
224 | #ifndef HAVE_GSTREAMER_0_10 |
225 | static const struct fmttab { | |
226 | const char *fmt; | |
227 | unsigned bits; | |
228 | unsigned endian; | |
229 | } fmttab[] = { | |
230 | { "S8", 8, ENDIAN_BIG }, | |
231 | { "S8", 8, ENDIAN_LITTLE }, | |
232 | { "S16BE", 16, ENDIAN_BIG }, | |
233 | { "S16LE", 16, ENDIAN_LITTLE }, | |
234 | { 0 } | |
235 | }; | |
236 | const struct fmttab *ft; | |
237 | #endif | |
238 | ||
7207a21b MW |
239 | /* Set up the global variables. */ |
240 | pipeline = gst_pipeline_new("pipe"); | |
241 | appsink = GST_APP_SINK(sink); | |
242 | ||
243 | /* Configure the various simple elements. */ | |
244 | g_object_set(source, "location", file, END); | |
245 | g_object_set(sink, "sync", FALSE, END); | |
246 | ||
c915f0d4 MW |
247 | /* Configure the resampler and converter. Leave things as their defaults |
248 | * if the user hasn't made an explicit request. | |
f5ebedb0 | 249 | */ |
c915f0d4 | 250 | if(quality >= 0) g_object_set(resample, "quality", quality, END); |
f5ebedb0 MW |
251 | if(dither >= 0) g_object_set(convert, "dithering", dither, END); |
252 | if(shape >= 0) g_object_set(convert, "noise-shaping", shape, END); | |
253 | ||
c915f0d4 | 254 | /* Set up the sink's capabilities from the configuration. */ |
c076146f | 255 | #ifdef HAVE_GSTREAMER_0_10 |
c915f0d4 MW |
256 | caps = gst_caps_new_simple("audio/x-raw-int", |
257 | "width", G_TYPE_INT, fmt->bits, | |
258 | "depth", G_TYPE_INT, fmt->bits, | |
259 | "channels", G_TYPE_INT, fmt->channels, | |
260 | "signed", G_TYPE_BOOLEAN, TRUE, | |
261 | "rate", G_TYPE_INT, fmt->rate, | |
262 | "endianness", G_TYPE_INT, | |
263 | fmt->endian == ENDIAN_BIG ? | |
264 | G_BIG_ENDIAN : G_LITTLE_ENDIAN, | |
265 | END); | |
c076146f MW |
266 | #else |
267 | for (ft = fmttab; ft->fmt; ft++) | |
268 | if (ft->bits == fmt->bits && ft->endian == fmt->endian) break; | |
269 | if(!ft->fmt) { | |
270 | disorder_fatal(0, "unsupported sample format: bits=%"PRIu32", endian=%u", | |
271 | fmt->bits, fmt->endian); | |
272 | } | |
273 | caps = gst_caps_new_simple("audio/x-raw", | |
274 | "format", G_TYPE_STRING, ft->fmt, | |
275 | "channels", G_TYPE_INT, fmt->channels, | |
276 | "rate", G_TYPE_INT, fmt->rate, | |
277 | END); | |
278 | #endif | |
7207a21b | 279 | gst_app_sink_set_caps(appsink, caps); |
3d9c9d54 | 280 | gst_caps_unref(caps); |
7207a21b MW |
281 | |
282 | /* Add the various elements into the pipeline. We'll stitch them together | |
283 | * in pieces, because the pipeline is somewhat dynamic. | |
284 | */ | |
c915f0d4 MW |
285 | gst_bin_add_many(GST_BIN(pipeline), |
286 | source, decode, | |
287 | resample, convert, sink, END); | |
7207a21b | 288 | |
c915f0d4 | 289 | /* Link audio conversion stages onto the front. The rest of DisOrder |
7207a21b MW |
290 | * doesn't handle much of the full panoply of exciting audio formats. |
291 | */ | |
292 | link_elements(convert, tail); tail = convert; | |
c915f0d4 | 293 | link_elements(resample, tail); tail = resample; |
7207a21b MW |
294 | |
295 | /* If we're meant to do ReplayGain then insert it into the pipeline before | |
296 | * the converter. | |
297 | */ | |
298 | if(mode != OFF) { | |
299 | gain = gst_element_factory_make("rgvolume", "gain"); | |
c54fec77 MW |
300 | if(!gain) |
301 | disorder_fatal(0, "failed to create GStreamer elements: " | |
302 | "need base and good plugins"); | |
f5ebedb0 MW |
303 | g_object_set(gain, |
304 | "album-mode", mode == ALBUM, | |
305 | "fallback-gain", fallback, | |
306 | END); | |
7207a21b MW |
307 | gst_bin_add(GST_BIN(pipeline), gain); |
308 | link_elements(gain, tail); tail = gain; | |
309 | } | |
310 | ||
311 | /* Link the source and the decoder together. The `decodebin' is annoying | |
312 | * and doesn't have any source pads yet, so the best we can do is make two | |
313 | * halves of the chain, and add a hook to stitch them together later. | |
314 | */ | |
315 | link_elements(source, decode); | |
316 | g_signal_connect(decode, "pad-added", | |
317 | G_CALLBACK(decoder_pad_arrived), tail); | |
318 | } | |
319 | ||
320 | /* Respond to a message from the BUS. The only thing we need worry about | |
321 | * here is errors from the pipeline. | |
322 | */ | |
323 | static void bus_message(GstBus UNUSED *bus, GstMessage *msg, | |
324 | gpointer UNUSED u) | |
325 | { | |
0b47d516 | 326 | switch(GST_MESSAGE_TYPE(msg)) { |
7207a21b | 327 | case GST_MESSAGE_ERROR: |
c076146f MW |
328 | disorder_fatal(0, "%s", |
329 | gst_structure_get_string(gst_message_get_structure(msg), | |
330 | "debug")); | |
7207a21b MW |
331 | default: |
332 | break; | |
333 | } | |
334 | } | |
335 | ||
336 | /* End of stream. Stop polling the main loop. */ | |
337 | static void cb_eos(GstAppSink UNUSED *sink, gpointer UNUSED u) | |
338 | { g_main_loop_quit(loop); } | |
339 | ||
340 | /* Preroll buffers are prepared when the pipeline moves to the `paused' | |
341 | * state, so that they're ready for immediate playback. Conveniently, they | |
342 | * also carry format information, which is what we want here. Stash the | |
343 | * sample format information in the `stream_header' structure ready for | |
344 | * actual buffers of interesting data. | |
345 | */ | |
346 | static GstFlowReturn cb_preroll(GstAppSink *sink, gpointer UNUSED u) | |
347 | { | |
c076146f | 348 | #ifdef HAVE_GSTREAMER_0_10 |
7207a21b MW |
349 | GstBuffer *buf = gst_app_sink_pull_preroll(sink); |
350 | GstCaps *caps = GST_BUFFER_CAPS(buf); | |
c076146f MW |
351 | #else |
352 | GstSample *samp = gst_app_sink_pull_preroll(sink); | |
353 | GstCaps *caps = gst_sample_get_caps(samp); | |
354 | #endif | |
7207a21b MW |
355 | |
356 | #ifdef HAVE_GST_AUDIO_INFO_FROM_CAPS | |
357 | ||
358 | /* Parse the audio format information out of the caps. There's a handy | |
359 | * function to do this in later versions of gst-plugins-base, so use that | |
360 | * if it's available. Once we no longer care about supporting such old | |
361 | * versions we can delete the version which does the job the hard way. | |
362 | */ | |
363 | ||
364 | GstAudioInfo ai; | |
365 | ||
366 | if(!gst_audio_info_from_caps(&ai, caps)) | |
367 | disorder_fatal(0, "can't decode `%s': failed to parse audio info", file); | |
368 | hdr.rate = ai.rate; | |
369 | hdr.channels = ai.channels; | |
370 | hdr.bits = ai.finfo->width; | |
371 | hdr.endian = ai.finfo->endianness == G_BIG_ENDIAN ? | |
372 | ENDIAN_BIG : ENDIAN_LITTLE; | |
373 | ||
374 | #else | |
375 | ||
376 | GstStructure *s; | |
377 | const char *ty; | |
378 | gint rate, channels, bits, endian; | |
379 | gboolean signedp; | |
380 | ||
381 | /* Make sure that the caps is basically the right shape. */ | |
382 | if(!GST_CAPS_IS_SIMPLE(caps)) disorder_fatal(0, "expected simple caps"); | |
383 | s = gst_caps_get_structure(caps, 0); | |
384 | ty = gst_structure_get_name(s); | |
385 | if(strcmp(ty, "audio/x-raw-int") != 0) | |
386 | disorder_fatal(0, "unexpected content type `%s'", ty); | |
387 | ||
388 | /* Extract fields from the structure. */ | |
389 | if(!gst_structure_get(s, | |
390 | "rate", G_TYPE_INT, &rate, | |
391 | "channels", G_TYPE_INT, &channels, | |
392 | "width", G_TYPE_INT, &bits, | |
393 | "endianness", G_TYPE_INT, &endian, | |
394 | "signed", G_TYPE_BOOLEAN, &signedp, | |
395 | END)) | |
396 | disorder_fatal(0, "can't decode `%s': failed to parse audio caps", file); | |
397 | hdr.rate = rate; hdr.channels = channels; hdr.bits = bits; | |
398 | hdr.endian = endian == G_BIG_ENDIAN ? ENDIAN_BIG : ENDIAN_LITTLE; | |
399 | ||
400 | #endif | |
401 | ||
c076146f | 402 | #ifdef HAVE_GSTREAMER_0_10 |
7207a21b | 403 | gst_buffer_unref(buf); |
c076146f MW |
404 | #else |
405 | gst_sample_unref(samp); | |
406 | #endif | |
7207a21b MW |
407 | return GST_FLOW_OK; |
408 | } | |
409 | ||
410 | /* A new buffer of sample data has arrived, so we should pass it on with | |
411 | * appropriate framing. | |
412 | */ | |
413 | static GstFlowReturn cb_buffer(GstAppSink *sink, gpointer UNUSED u) | |
414 | { | |
c076146f | 415 | #ifdef HAVE_GSTREAMER_0_10 |
7207a21b | 416 | GstBuffer *buf = gst_app_sink_pull_buffer(sink); |
c076146f MW |
417 | #else |
418 | GstSample *samp = gst_app_sink_pull_sample(sink); | |
419 | GstBuffer *buf = gst_sample_get_buffer(samp); | |
420 | GstMemory *mem; | |
421 | GstMapInfo map; | |
422 | gint i, n; | |
423 | #endif | |
7207a21b MW |
424 | |
425 | /* Make sure we actually have a grip on the sample format here. */ | |
426 | if(!hdr.rate) disorder_fatal(0, "format unset"); | |
427 | ||
428 | /* Write out a frame of audio data. */ | |
c076146f | 429 | #ifdef HAVE_GSTREAMER_0_10 |
7207a21b | 430 | hdr.nbytes = GST_BUFFER_SIZE(buf); |
ff56519f | 431 | if((!(flags&f_stream) && fwrite(&hdr, sizeof(hdr), 1, fp) != 1) || |
7207a21b MW |
432 | fwrite(GST_BUFFER_DATA(buf), 1, hdr.nbytes, fp) != hdr.nbytes) |
433 | disorder_fatal(errno, "output"); | |
c076146f MW |
434 | #else |
435 | for(i = 0, n = gst_buffer_n_memory(buf); i < n; i++) { | |
436 | mem = gst_buffer_peek_memory(buf, i); | |
437 | if(!gst_memory_map(mem, &map, GST_MAP_READ)) | |
438 | disorder_fatal(0, "failed to map sample buffer"); | |
439 | hdr.nbytes = map.size; | |
440 | if((!(flags&f_stream) && fwrite(&hdr, sizeof(hdr), 1, fp) != 1) || | |
441 | fwrite(map.data, 1, map.size, fp) != map.size) | |
442 | disorder_fatal(errno, "output"); | |
443 | gst_memory_unmap(mem, &map); | |
444 | } | |
445 | #endif | |
7207a21b MW |
446 | |
447 | /* And we're done. */ | |
c076146f | 448 | #ifdef HAVE_GSTREAMER_0_10 |
7207a21b | 449 | gst_buffer_unref(buf); |
c076146f MW |
450 | #else |
451 | gst_sample_unref(samp); | |
452 | #endif | |
7207a21b MW |
453 | return GST_FLOW_OK; |
454 | } | |
455 | ||
456 | static GstAppSinkCallbacks callbacks = { | |
457 | .eos = cb_eos, | |
458 | .new_preroll = cb_preroll, | |
c076146f | 459 | #ifdef HAVE_GSTREAMER_0_10 |
7207a21b | 460 | .new_buffer = cb_buffer |
c076146f MW |
461 | #else |
462 | .new_sample = cb_buffer | |
463 | #endif | |
7207a21b MW |
464 | }; |
465 | ||
466 | /* Decode the audio file. We're already set up for everything. */ | |
467 | static void decode(void) | |
468 | { | |
469 | GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); | |
470 | ||
471 | /* Set up the message bus and main loop. */ | |
472 | gst_bus_add_signal_watch(bus); | |
473 | loop = g_main_loop_new(0, FALSE); | |
474 | g_signal_connect(bus, "message", G_CALLBACK(bus_message), 0); | |
475 | ||
476 | /* Tell the sink to call us when interesting things happen. */ | |
01eab3ca MW |
477 | gst_app_sink_set_max_buffers(appsink, 16); |
478 | gst_app_sink_set_drop(appsink, FALSE); | |
7207a21b MW |
479 | gst_app_sink_set_callbacks(appsink, &callbacks, 0, 0); |
480 | ||
481 | /* Set the ball rolling. */ | |
be398aad | 482 | gst_element_set_state(pipeline, GST_STATE_PLAYING); |
7207a21b MW |
483 | |
484 | /* And wait for the miracle to come. */ | |
485 | g_main_loop_run(loop); | |
486 | ||
487 | /* Shut down the pipeline. This isn't strictly necessary, since we're | |
488 | * about to exit very soon, but it's kind of polite. | |
489 | */ | |
be398aad | 490 | gst_element_set_state(pipeline, GST_STATE_NULL); |
7207a21b MW |
491 | } |
492 | ||
493 | static int getenum(const char *what, const char *s, const char *const *tags) | |
494 | { | |
495 | int i; | |
496 | ||
497 | for(i = 0; tags[i]; i++) | |
498 | if(strcmp(s, tags[i]) == 0) return i; | |
499 | disorder_fatal(0, "unknown %s `%s'", what, s); | |
500 | } | |
501 | ||
f5ebedb0 MW |
502 | static double getfloat(const char *what, const char *s) |
503 | { | |
504 | double d; | |
505 | char *q; | |
506 | ||
507 | errno = 0; | |
508 | d = strtod(s, &q); | |
509 | if(*q || errno) disorder_fatal(0, "invalid %s `%s'", what, s); | |
510 | return d; | |
511 | } | |
512 | ||
c915f0d4 MW |
513 | static int getint(const char *what, const char *s, int min, int max) |
514 | { | |
515 | long i; | |
516 | char *q; | |
517 | ||
518 | errno = 0; | |
519 | i = strtol(s, &q, 10); | |
520 | if(*q || errno || min > i || i > max) | |
521 | disorder_fatal(0, "invalid %s `%s'", what, s); | |
522 | return (int)i; | |
523 | } | |
524 | ||
7207a21b MW |
525 | static const struct option options[] = { |
526 | { "help", no_argument, 0, 'h' }, | |
527 | { "version", no_argument, 0, 'V' }, | |
c915f0d4 | 528 | { "config", required_argument, 0, 'c' }, |
f5ebedb0 MW |
529 | { "dither", required_argument, 0, 'd' }, |
530 | { "fallback-gain", required_argument, 0, 'f' }, | |
531 | { "noise-shape", required_argument, 0, 'n' }, | |
c915f0d4 | 532 | { "quality", required_argument, 0, 'q' }, |
7207a21b | 533 | { "replay-gain", required_argument, 0, 'r' }, |
ff56519f | 534 | { "stream", no_argument, 0, 's' }, |
7207a21b MW |
535 | { 0, 0, 0, 0 } |
536 | }; | |
537 | ||
16bf32dc | 538 | static void NORETURN help(void) |
7207a21b MW |
539 | { |
540 | xprintf("Usage:\n" | |
541 | " disorder-gstdecode [OPTIONS] PATH\n" | |
542 | "Options:\n" | |
543 | " --help, -h Display usage message\n" | |
544 | " --version, -V Display version number\n" | |
c915f0d4 | 545 | " --config PATH, -c PATH Set configuration file\n" |
f5ebedb0 MW |
546 | " --dither TYPE, -d TYPE TYPE is `none', `rpdf', `tpdf', or " |
547 | "`tpdf-hf'\n" | |
548 | " --fallback-gain DB, -f DB For tracks without ReplayGain data\n" | |
549 | " --noise-shape TYPE, -n TYPE TYPE is `none', `error-feedback',\n" | |
550 | " `simple', `medium' or `high'\n" | |
c915f0d4 | 551 | " --quality QUAL, -q QUAL Resampling quality: 0 poor, 10 good\n" |
7207a21b | 552 | " --replay-gain MODE, -r MODE MODE is `off', `track' or `album'\n" |
ff56519f | 553 | " --stream, -s Output raw samples, without framing\n" |
7207a21b MW |
554 | "\n" |
555 | "Alternative audio decoder for DisOrder. Only intended to be\n" | |
556 | "used by speaker process, not for normal users.\n"); | |
557 | xfclose(stdout); | |
558 | exit(0); | |
559 | } | |
560 | ||
561 | /* Main program. */ | |
562 | int main(int argc, char *argv[]) | |
563 | { | |
564 | int n; | |
565 | const char *e; | |
566 | ||
567 | /* Initial setup. */ | |
568 | set_progname(argv); | |
569 | if(!setlocale(LC_CTYPE, "")) disorder_fatal(errno, "calling setlocale"); | |
570 | ||
571 | /* Parse command line. */ | |
ff56519f | 572 | while((n = getopt_long(argc, argv, "hVc:d:f:n:q:r:s", options, 0)) >= 0) { |
7207a21b MW |
573 | switch(n) { |
574 | case 'h': help(); | |
575 | case 'V': version("disorder-gstdecode"); | |
c915f0d4 | 576 | case 'c': configfile = optarg; break; |
f5ebedb0 MW |
577 | case 'd': dither = getenum("dither type", optarg, dithers); break; |
578 | case 'f': fallback = getfloat("fallback gain", optarg); break; | |
579 | case 'n': shape = getenum("noise-shaping type", optarg, shapes); break; | |
c915f0d4 | 580 | case 'q': quality = getint("resample quality", optarg, 0, 10); break; |
7207a21b | 581 | case 'r': mode = getenum("ReplayGain mode", optarg, modes); break; |
ff56519f | 582 | case 's': flags |= f_stream; break; |
7207a21b MW |
583 | default: disorder_fatal(0, "invalid option"); |
584 | } | |
585 | } | |
586 | if(optind >= argc) disorder_fatal(0, "missing filename"); | |
587 | file = argv[optind++]; | |
588 | if(optind < argc) disorder_fatal(0, "excess arguments"); | |
c915f0d4 | 589 | if(config_read(1, 0)) disorder_fatal(0, "cannot read configuration"); |
7207a21b MW |
590 | |
591 | /* Set up the GStreamer machinery. */ | |
592 | gst_init(0, 0); | |
593 | prepare_pipeline(); | |
594 | ||
595 | /* Set up the output file. */ | |
596 | if((e = getenv("DISORDER_RAW_FD")) != 0) { | |
597 | if((fp = fdopen(atoi(e), "wb")) == 0) disorder_fatal(errno, "fdopen"); | |
598 | } else | |
599 | fp = stdout; | |
600 | ||
601 | /* Let's go. */ | |
602 | decode(); | |
603 | ||
604 | /* And now we're done. */ | |
605 | xfclose(fp); | |
606 | return (0); | |
607 | } | |
608 | ||
609 | /* | |
610 | Local Variables: | |
611 | c-basic-offset:2 | |
612 | comment-column:40 | |
613 | fill-column:77 | |
614 | indent-tabs-mode:nil | |
615 | End: | |
616 | */ |