Commit | Line | Data |
---|---|---|
f8f8039f RK |
1 | /* |
2 | * This file is part of DisOrder | |
5aff007d | 3 | * Copyright (C) 2007, 2008 Richard Kettlewell |
f8f8039f RK |
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 2 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, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * 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, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
20 | /** @file server/decode.c | |
21 | * @brief General-purpose decoder for use by speaker process | |
22 | */ | |
23 | ||
05b75f8d | 24 | #include "disorder-server.h" |
f8f8039f | 25 | |
f8f8039f | 26 | #include <mad.h> |
572c2899 | 27 | #include <vorbis/vorbisfile.h> |
762806f1 RK |
28 | |
29 | /* libFLAC has had an API change and stupidly taken away the old API */ | |
30 | #if HAVE_FLAC_FILE_DECODER_H | |
31 | # include <FLAC/file_decoder.h> | |
32 | #else | |
33 | # include <FLAC/stream_decoder.h> | |
34 | #define FLAC__FileDecoder FLAC__StreamDecoder | |
35 | #define FLAC__FileDecoderState FLAC__StreamDecoderState | |
36 | #endif | |
f8f8039f | 37 | |
ce6c36be | 38 | #include "wav.h" |
39492555 | 39 | #include "speaker-protocol.h" |
f8f8039f RK |
40 | |
41 | /** @brief Encoding lookup table type */ | |
42 | struct decoder { | |
43 | /** @brief Glob pattern matching file */ | |
44 | const char *pattern; | |
45 | /** @brief Decoder function */ | |
46 | void (*decode)(void); | |
47 | }; | |
48 | ||
49 | /** @brief Input file */ | |
50 | static int inputfd; | |
51 | ||
52 | /** @brief Output file */ | |
53 | static FILE *outputfp; | |
54 | ||
55 | /** @brief Filename */ | |
56 | static const char *path; | |
57 | ||
58 | /** @brief Input buffer */ | |
75db8354 | 59 | static char input_buffer[1048576]; |
f8f8039f | 60 | |
87b5259b | 61 | /** @brief Number of bytes read into buffer */ |
62 | static int input_count; | |
f8f8039f | 63 | |
75db8354 | 64 | /** @brief Write an 8-bit word */ |
65 | static inline void output_8(int n) { | |
66 | if(putc(n, outputfp) < 0) | |
67 | fatal(errno, "decoding %s: output error", path); | |
68 | } | |
69 | ||
f8f8039f RK |
70 | /** @brief Write a 16-bit word in bigendian format */ |
71 | static inline void output_16(uint16_t n) { | |
72 | if(putc(n >> 8, outputfp) < 0 | |
75db8354 | 73 | || putc(n, outputfp) < 0) |
74 | fatal(errno, "decoding %s: output error", path); | |
75 | } | |
76 | ||
77 | /** @brief Write a 24-bit word in bigendian format */ | |
78 | static inline void output_24(uint32_t n) { | |
79 | if(putc(n >> 16, outputfp) < 0 | |
80 | || putc(n >> 8, outputfp) < 0 | |
81 | || putc(n, outputfp) < 0) | |
f8f8039f RK |
82 | fatal(errno, "decoding %s: output error", path); |
83 | } | |
84 | ||
75db8354 | 85 | /** @brief Write a 32-bit word in bigendian format */ |
86 | static inline void output_32(uint32_t n) { | |
87 | if(putc(n >> 24, outputfp) < 0 | |
88 | || putc(n >> 16, outputfp) < 0 | |
89 | || putc(n >> 8, outputfp) < 0 | |
90 | || putc(n, outputfp) < 0) | |
91 | fatal(errno, "decoding %s: output error", path); | |
92 | } | |
93 | ||
94 | /** @brief Write a block header | |
95 | * @param rate Sample rate in Hz | |
96 | * @param channels Channel count (currently only 1 or 2 supported) | |
97 | * @param bits Bits per sample (must be a multiple of 8, no more than 64) | |
98 | * @param nbytes Total number of data bytes | |
99 | * @param endian @ref ENDIAN_BIG or @ref ENDIAN_LITTLE | |
100 | * | |
101 | * Checks that the sample format is a supported one (so other calls do not have | |
102 | * to) and calls fatal() on error. | |
f8f8039f RK |
103 | */ |
104 | static void output_header(int rate, | |
105 | int channels, | |
39492555 | 106 | int bits, |
ce6c36be | 107 | int nbytes, |
108 | int endian) { | |
39492555 | 109 | struct stream_header header; |
110 | ||
75db8354 | 111 | if(bits <= 0 || bits % 8 || bits > 64) |
112 | fatal(0, "decoding %s: unsupported sample size %d bits", path, bits); | |
113 | if(channels <= 0 || channels > 2) | |
114 | fatal(0, "decoding %s: unsupported channel count %d", path, channels); | |
115 | if(rate <= 0) | |
116 | fatal(0, "decoding %s: nonsensical sample rate %dHz", path, rate); | |
39492555 | 117 | header.rate = rate; |
118 | header.bits = bits; | |
119 | header.channels = channels; | |
ce6c36be | 120 | header.endian = endian; |
39492555 | 121 | header.nbytes = nbytes; |
122 | if(fwrite(&header, sizeof header, 1, outputfp) < 1) | |
123 | fatal(errno, "decoding %s: writing format header", path); | |
f8f8039f RK |
124 | } |
125 | ||
126 | /** @brief Dithering state | |
127 | * Filched from mpg321, which credits it to Robert Leslie */ | |
128 | struct audio_dither { | |
129 | mad_fixed_t error[3]; | |
130 | mad_fixed_t random; | |
131 | }; | |
132 | ||
133 | /** @brief 32-bit PRNG | |
134 | * Filched from mpg321, which credits it to Robert Leslie */ | |
135 | static inline unsigned long prng(unsigned long state) | |
136 | { | |
137 | return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL; | |
138 | } | |
139 | ||
140 | /** @brief Generic linear sample quantize and dither routine | |
141 | * Filched from mpg321, which credits it to Robert Leslie */ | |
f8f8039f RK |
142 | static long audio_linear_dither(mad_fixed_t sample, |
143 | struct audio_dither *dither) { | |
144 | unsigned int scalebits; | |
145 | mad_fixed_t output, mask, rnd; | |
87b5259b | 146 | const int bits = 16; |
f8f8039f RK |
147 | |
148 | enum { | |
149 | MIN = -MAD_F_ONE, | |
150 | MAX = MAD_F_ONE - 1 | |
151 | }; | |
152 | ||
153 | /* noise shape */ | |
154 | sample += dither->error[0] - dither->error[1] + dither->error[2]; | |
155 | ||
156 | dither->error[2] = dither->error[1]; | |
157 | dither->error[1] = dither->error[0] / 2; | |
158 | ||
159 | /* bias */ | |
160 | output = sample + (1L << (MAD_F_FRACBITS + 1 - bits - 1)); | |
161 | ||
162 | scalebits = MAD_F_FRACBITS + 1 - bits; | |
163 | mask = (1L << scalebits) - 1; | |
164 | ||
165 | /* dither */ | |
166 | rnd = prng(dither->random); | |
167 | output += (rnd & mask) - (dither->random & mask); | |
168 | ||
169 | dither->random = rnd; | |
170 | ||
171 | /* clip */ | |
172 | if (output > MAX) { | |
173 | output = MAX; | |
174 | ||
175 | if (sample > MAX) | |
176 | sample = MAX; | |
177 | } | |
178 | else if (output < MIN) { | |
179 | output = MIN; | |
180 | ||
181 | if (sample < MIN) | |
182 | sample = MIN; | |
183 | } | |
184 | ||
185 | /* quantize */ | |
186 | output &= ~mask; | |
187 | ||
188 | /* error feedback */ | |
189 | dither->error[0] = sample - output; | |
190 | ||
191 | /* scale */ | |
192 | return output >> scalebits; | |
193 | } | |
f8f8039f RK |
194 | |
195 | /** @brief MP3 output callback */ | |
196 | static enum mad_flow mp3_output(void attribute((unused)) *data, | |
197 | struct mad_header const *header, | |
198 | struct mad_pcm *pcm) { | |
199 | size_t n = pcm->length; | |
200 | const mad_fixed_t *l = pcm->samples[0], *r = pcm->samples[1]; | |
201 | static struct audio_dither ld[1], rd[1]; | |
202 | ||
203 | output_header(header->samplerate, | |
204 | pcm->channels, | |
39492555 | 205 | 16, |
ce6c36be | 206 | 2 * pcm->channels * pcm->length, |
207 | ENDIAN_BIG); | |
f8f8039f RK |
208 | switch(pcm->channels) { |
209 | case 1: | |
210 | while(n--) | |
211 | output_16(audio_linear_dither(*l++, ld)); | |
212 | break; | |
213 | case 2: | |
214 | while(n--) { | |
215 | output_16(audio_linear_dither(*l++, ld)); | |
216 | output_16(audio_linear_dither(*r++, rd)); | |
217 | } | |
218 | break; | |
f8f8039f RK |
219 | } |
220 | return MAD_FLOW_CONTINUE; | |
221 | } | |
222 | ||
223 | /** @brief MP3 input callback */ | |
224 | static enum mad_flow mp3_input(void attribute((unused)) *data, | |
225 | struct mad_stream *stream) { | |
87b5259b | 226 | int used, remain, n; |
227 | ||
228 | /* libmad requires its caller to do ALL the buffering work, including coping | |
229 | * with partial frames. Given that it appears to be completely undocumented | |
230 | * you could perhaps be forgiven for not discovering this... */ | |
231 | if(input_count) { | |
232 | /* Compute total number of bytes consumed */ | |
233 | used = (char *)stream->next_frame - input_buffer; | |
234 | /* Compute number of bytes left to consume */ | |
235 | remain = input_count - used; | |
236 | memmove(input_buffer, input_buffer + used, remain); | |
237 | } else { | |
238 | remain = 0; | |
239 | } | |
240 | /* Read new data */ | |
241 | n = read(inputfd, input_buffer + remain, (sizeof input_buffer) - remain); | |
242 | if(n < 0) | |
243 | fatal(errno, "reading from %s", path); | |
244 | /* Compute total number of bytes available */ | |
245 | input_count = remain + n; | |
246 | if(input_count) | |
247 | mad_stream_buffer(stream, (unsigned char *)input_buffer, input_count); | |
248 | if(n) | |
249 | return MAD_FLOW_CONTINUE; | |
250 | else | |
f8f8039f | 251 | return MAD_FLOW_STOP; |
f8f8039f RK |
252 | } |
253 | ||
f8f8039f RK |
254 | /** @brief MP3 error callback */ |
255 | static enum mad_flow mp3_error(void attribute((unused)) *data, | |
256 | struct mad_stream *stream, | |
257 | struct mad_frame attribute((unused)) *frame) { | |
39492555 | 258 | if(0) |
259 | /* Just generates pointless verbosity l-( */ | |
260 | error(0, "decoding %s: %s (%#04x)", | |
261 | path, mad_stream_errorstr(stream), stream->error); | |
f8f8039f RK |
262 | return MAD_FLOW_CONTINUE; |
263 | } | |
264 | ||
265 | /** @brief MP3 decoder */ | |
266 | static void decode_mp3(void) { | |
267 | struct mad_decoder mad[1]; | |
268 | ||
87b5259b | 269 | if((inputfd = open(path, O_RDONLY)) < 0) |
270 | fatal(errno, "opening %s", path); | |
39492555 | 271 | mad_decoder_init(mad, 0/*data*/, mp3_input, 0/*header*/, 0/*filter*/, |
f8f8039f RK |
272 | mp3_output, mp3_error, 0/*message*/); |
273 | if(mad_decoder_run(mad, MAD_DECODER_MODE_SYNC)) | |
274 | exit(1); | |
275 | mad_decoder_finish(mad); | |
276 | } | |
277 | ||
572c2899 | 278 | /** @brief OGG decoder */ |
279 | static void decode_ogg(void) { | |
280 | FILE *fp; | |
281 | OggVorbis_File vf[1]; | |
282 | int err; | |
283 | long n; | |
284 | int bitstream; | |
285 | vorbis_info *vi; | |
286 | ||
287 | if(!(fp = fopen(path, "rb"))) | |
288 | fatal(errno, "cannot open %s", path); | |
289 | /* There doesn't seem to be any standard function for mapping the error codes | |
290 | * to strings l-( */ | |
291 | if((err = ov_open(fp, vf, 0/*initial*/, 0/*ibytes*/))) | |
292 | fatal(0, "ov_fopen %s: %d", path, err); | |
293 | if(!(vi = ov_info(vf, 0/*link*/))) | |
294 | fatal(0, "ov_info %s: failed", path); | |
75db8354 | 295 | while((n = ov_read(vf, input_buffer, sizeof input_buffer, 1/*bigendianp*/, |
572c2899 | 296 | 2/*bytes/word*/, 1/*signed*/, &bitstream))) { |
297 | if(n < 0) | |
298 | fatal(0, "ov_read %s: %ld", path, n); | |
299 | if(bitstream > 0) | |
300 | fatal(0, "only single-bitstream ogg files are supported"); | |
ce6c36be | 301 | output_header(vi->rate, vi->channels, 16/*bits*/, n, ENDIAN_BIG); |
75db8354 | 302 | if(fwrite(input_buffer, 1, n, outputfp) < (size_t)n) |
572c2899 | 303 | fatal(errno, "decoding %s: writing sample data", path); |
304 | } | |
305 | } | |
306 | ||
ce6c36be | 307 | /** @brief Sample data callback used by decode_wav() */ |
308 | static int wav_write(struct wavfile attribute((unused)) *f, | |
309 | const char *data, | |
310 | size_t nbytes, | |
311 | void attribute((unused)) *u) { | |
312 | if(fwrite(data, 1, nbytes, outputfp) < nbytes) | |
313 | fatal(errno, "decoding %s: writing sample data", path); | |
314 | return 0; | |
315 | } | |
316 | ||
317 | /** @brief WAV file decoder */ | |
318 | static void decode_wav(void) { | |
319 | struct wavfile f[1]; | |
320 | int err; | |
321 | ||
322 | if((err = wav_init(f, path))) | |
323 | fatal(err, "opening %s", path); | |
ce6c36be | 324 | output_header(f->rate, f->channels, f->bits, f->datasize, ENDIAN_LITTLE); |
325 | if((err = wav_data(f, wav_write, 0))) | |
326 | fatal(err, "error decoding %s", path); | |
327 | } | |
328 | ||
75db8354 | 329 | /** @brief Metadata callback for FLAC decoder |
330 | * | |
331 | * This is a no-op here. | |
332 | */ | |
333 | static void flac_metadata(const FLAC__FileDecoder attribute((unused)) *decoder, | |
334 | const FLAC__StreamMetadata attribute((unused)) *metadata, | |
335 | void attribute((unused)) *client_data) { | |
336 | } | |
337 | ||
338 | /** @brief Error callback for FLAC decoder */ | |
339 | static void flac_error(const FLAC__FileDecoder attribute((unused)) *decoder, | |
340 | FLAC__StreamDecoderErrorStatus status, | |
341 | void attribute((unused)) *client_data) { | |
342 | fatal(0, "error decoding %s: %s", path, | |
343 | FLAC__StreamDecoderErrorStatusString[status]); | |
344 | } | |
345 | ||
346 | /** @brief Write callback for FLAC decoder */ | |
347 | static FLAC__StreamDecoderWriteStatus flac_write | |
348 | (const FLAC__FileDecoder attribute((unused)) *decoder, | |
349 | const FLAC__Frame *frame, | |
350 | const FLAC__int32 *const buffer[], | |
351 | void attribute((unused)) *client_data) { | |
352 | size_t n, c; | |
353 | ||
354 | output_header(frame->header.sample_rate, | |
355 | frame->header.channels, | |
356 | frame->header.bits_per_sample, | |
357 | (frame->header.channels * frame->header.blocksize | |
358 | * frame->header.bits_per_sample) / 8, | |
359 | ENDIAN_BIG); | |
360 | for(n = 0; n < frame->header.blocksize; ++n) { | |
361 | for(c = 0; c < frame->header.channels; ++c) { | |
362 | switch(frame->header.bits_per_sample) { | |
363 | case 8: output_8(buffer[c][n]); break; | |
364 | case 16: output_16(buffer[c][n]); break; | |
365 | case 24: output_24(buffer[c][n]); break; | |
366 | case 32: output_32(buffer[c][n]); break; | |
367 | } | |
368 | } | |
369 | } | |
370 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; | |
371 | } | |
372 | ||
373 | ||
374 | /** @brief FLAC file decoder */ | |
375 | static void decode_flac(void) { | |
762806f1 | 376 | #if HAVE_FLAC_FILE_DECODER_H |
75db8354 | 377 | FLAC__FileDecoder *fd = 0; |
378 | FLAC__FileDecoderState fs; | |
379 | ||
380 | if(!(fd = FLAC__file_decoder_new())) | |
381 | fatal(0, "FLAC__file_decoder_new failed"); | |
382 | if(!(FLAC__file_decoder_set_filename(fd, path))) | |
383 | fatal(0, "FLAC__file_set_filename failed"); | |
384 | FLAC__file_decoder_set_metadata_callback(fd, flac_metadata); | |
385 | FLAC__file_decoder_set_error_callback(fd, flac_error); | |
386 | FLAC__file_decoder_set_write_callback(fd, flac_write); | |
387 | if((fs = FLAC__file_decoder_init(fd))) | |
388 | fatal(0, "FLAC__file_decoder_init: %s", FLAC__FileDecoderStateString[fs]); | |
389 | FLAC__file_decoder_process_until_end_of_file(fd); | |
762806f1 RK |
390 | #else |
391 | FLAC__StreamDecoder *sd = 0; | |
392 | FLAC__StreamDecoderInitStatus is; | |
393 | ||
394 | if((is = FLAC__stream_decoder_init_file(sd, path, flac_write, flac_metadata, | |
395 | flac_error, 0))) | |
396 | fatal(0, "FLAC__stream_decoder_init_file %s: %s", | |
397 | path, FLAC__StreamDecoderInitStatusString[is]); | |
398 | #endif | |
75db8354 | 399 | } |
400 | ||
f8f8039f RK |
401 | /** @brief Lookup table of decoders */ |
402 | static const struct decoder decoders[] = { | |
403 | { "*.mp3", decode_mp3 }, | |
404 | { "*.MP3", decode_mp3 }, | |
572c2899 | 405 | { "*.ogg", decode_ogg }, |
406 | { "*.OGG", decode_ogg }, | |
75db8354 | 407 | { "*.flac", decode_flac }, |
408 | { "*.FLAC", decode_flac }, | |
ce6c36be | 409 | { "*.wav", decode_wav }, |
410 | { "*.WAV", decode_wav }, | |
f8f8039f RK |
411 | { 0, 0 } |
412 | }; | |
413 | ||
414 | static const struct option options[] = { | |
415 | { "help", no_argument, 0, 'h' }, | |
416 | { "version", no_argument, 0, 'V' }, | |
417 | { 0, 0, 0, 0 } | |
418 | }; | |
419 | ||
420 | /* Display usage message and terminate. */ | |
421 | static void help(void) { | |
422 | xprintf("Usage:\n" | |
423 | " disorder-decode [OPTIONS] PATH\n" | |
424 | "Options:\n" | |
425 | " --help, -h Display usage message\n" | |
426 | " --version, -V Display version number\n" | |
427 | "\n" | |
428 | "Audio decoder for DisOrder. Only intended to be used by speaker\n" | |
429 | "process, not for normal users.\n"); | |
430 | xfclose(stdout); | |
431 | exit(0); | |
432 | } | |
433 | ||
f8f8039f RK |
434 | int main(int argc, char **argv) { |
435 | int n; | |
436 | const char *e; | |
437 | ||
438 | set_progname(argv); | |
439 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "calling setlocale"); | |
440 | while((n = getopt_long(argc, argv, "hV", options, 0)) >= 0) { | |
441 | switch(n) { | |
442 | case 'h': help(); | |
3fbdc96d | 443 | case 'V': version("disorder-decode"); |
f8f8039f RK |
444 | default: fatal(0, "invalid option"); |
445 | } | |
446 | } | |
447 | if(optind >= argc) | |
448 | fatal(0, "missing filename"); | |
449 | if(optind + 1 < argc) | |
450 | fatal(0, "excess arguments"); | |
451 | if((e = getenv("DISORDER_RAW_FD"))) { | |
452 | if(!(outputfp = fdopen(atoi(e), "wb"))) | |
453 | fatal(errno, "fdopen"); | |
454 | } else | |
455 | outputfp = stdout; | |
456 | path = argv[optind]; | |
457 | for(n = 0; | |
458 | decoders[n].pattern | |
459 | && fnmatch(decoders[n].pattern, path, 0) != 0; | |
460 | ++n) | |
461 | ; | |
462 | if(!decoders[n].pattern) | |
463 | fatal(0, "cannot determine file type for %s", path); | |
464 | decoders[n].decode(); | |
465 | xfclose(outputfp); | |
466 | return 0; | |
467 | } | |
468 | ||
469 | /* | |
470 | Local Variables: | |
471 | c-basic-offset:2 | |
472 | comment-column:40 | |
473 | fill-column:79 | |
474 | indent-tabs-mode:nil | |
475 | End: | |
476 | */ |