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