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