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