2 * This file is part of DisOrder
3 * Copyright (C) 2007 Richard Kettlewell
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.
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.
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
20 /** @file server/decode.c
21 * @brief General-purpose decoder for use by speaker process
36 #include <vorbis/vorbisfile.h>
41 #include "speaker-protocol.h"
43 /** @brief Encoding lookup table type */
45 /** @brief Glob pattern matching file */
47 /** @brief Decoder function */
51 /** @brief Input file */
54 /** @brief Output file */
55 static FILE *outputfp
;
57 /** @brief Filename */
58 static const char *path
;
60 /** @brief Input buffer */
61 static char buffer
[1048576];
63 /** @brief Open the input file */
64 static void open_input(void) {
65 if((inputfd
= open(path
, O_RDONLY
)) < 0)
66 fatal(errno
, "opening %s", path
);
69 /** @brief Fill the buffer
70 * @return Number of bytes read
72 static size_t fill(void) {
73 int n
= read(inputfd
, buffer
, sizeof buffer
);
76 fatal(errno
, "reading from %s", path
);
80 /** @brief Write a 16-bit word in bigendian format */
81 static inline void output_16(uint16_t n
) {
82 if(putc(n
>> 8, outputfp
) < 0
83 || putc(n
& 0xFF, outputfp
) < 0)
84 fatal(errno
, "decoding %s: output error", path
);
87 /** @brief Write the header
88 * If called more than once, either does nothing (if you kept the same
89 * output encoding) or fails (if you changed it).
91 static void output_header(int rate
,
95 struct stream_header header
;
99 header
.channels
= channels
;
100 header
.endian
= ENDIAN_BIG
;
101 header
.nbytes
= nbytes
;
102 if(fwrite(&header
, sizeof header
, 1, outputfp
) < 1)
103 fatal(errno
, "decoding %s: writing format header", path
);
106 /** @brief Dithering state
107 * Filched from mpg321, which credits it to Robert Leslie */
108 struct audio_dither
{
109 mad_fixed_t error
[3];
113 /** @brief 32-bit PRNG
114 * Filched from mpg321, which credits it to Robert Leslie */
115 static inline unsigned long prng(unsigned long state
)
117 return (state
* 0x0019660dL
+ 0x3c6ef35fL
) & 0xffffffffL
;
120 /** @brief Generic linear sample quantize and dither routine
121 * Filched from mpg321, which credits it to Robert Leslie */
123 static long audio_linear_dither(mad_fixed_t sample
,
124 struct audio_dither
*dither
) {
125 unsigned int scalebits
;
126 mad_fixed_t output
, mask
, rnd
;
134 sample
+= dither
->error
[0] - dither
->error
[1] + dither
->error
[2];
136 dither
->error
[2] = dither
->error
[1];
137 dither
->error
[1] = dither
->error
[0] / 2;
140 output
= sample
+ (1L << (MAD_F_FRACBITS
+ 1 - bits
- 1));
142 scalebits
= MAD_F_FRACBITS
+ 1 - bits
;
143 mask
= (1L << scalebits
) - 1;
146 rnd
= prng(dither
->random
);
147 output
+= (rnd
& mask
) - (dither
->random
& mask
);
149 dither
->random
= rnd
;
158 else if (output
< MIN
) {
169 dither
->error
[0] = sample
- output
;
172 return output
>> scalebits
;
176 /** @brief MP3 output callback */
177 static enum mad_flow
mp3_output(void attribute((unused
)) *data
,
178 struct mad_header
const *header
,
179 struct mad_pcm
*pcm
) {
180 size_t n
= pcm
->length
;
181 const mad_fixed_t
*l
= pcm
->samples
[0], *r
= pcm
->samples
[1];
182 static struct audio_dither ld
[1], rd
[1];
184 output_header(header
->samplerate
,
187 2 * pcm
->channels
* pcm
->length
);
188 switch(pcm
->channels
) {
191 output_16(audio_linear_dither(*l
++, ld
));
195 output_16(audio_linear_dither(*l
++, ld
));
196 output_16(audio_linear_dither(*r
++, rd
));
200 fatal(0, "decoding %s: unsupported channel count %d", path
, pcm
->channels
);
202 return MAD_FLOW_CONTINUE
;
205 /** @brief MP3 input callback */
206 static enum mad_flow
mp3_input(void attribute((unused
)) *data
,
207 struct mad_stream
*stream
) {
208 const size_t n
= fill();
211 return MAD_FLOW_STOP
;
212 mad_stream_buffer(stream
, (unsigned char *)buffer
, n
);
213 return MAD_FLOW_CONTINUE
;
217 /** @brief MP3 error callback */
218 static enum mad_flow
mp3_error(void attribute((unused
)) *data
,
219 struct mad_stream
*stream
,
220 struct mad_frame
attribute((unused
)) *frame
) {
222 /* Just generates pointless verbosity l-( */
223 error(0, "decoding %s: %s (%#04x)",
224 path
, mad_stream_errorstr(stream
), stream
->error
);
225 return MAD_FLOW_CONTINUE
;
228 /** @brief MP3 decoder */
229 static void decode_mp3(void) {
230 struct mad_decoder mad
[1];
233 mad_decoder_init(mad
, 0/*data*/, mp3_input
, 0/*header*/, 0/*filter*/,
234 mp3_output
, mp3_error
, 0/*message*/);
235 if(mad_decoder_run(mad
, MAD_DECODER_MODE_SYNC
))
237 mad_decoder_finish(mad
);
240 /** @brief OGG decoder */
241 static void decode_ogg(void) {
243 OggVorbis_File vf
[1];
249 if(!(fp
= fopen(path
, "rb")))
250 fatal(errno
, "cannot open %s", path
);
251 /* There doesn't seem to be any standard function for mapping the error codes
253 if((err
= ov_open(fp
, vf
, 0/*initial*/, 0/*ibytes*/)))
254 fatal(0, "ov_fopen %s: %d", path
, err
);
255 if(!(vi
= ov_info(vf
, 0/*link*/)))
256 fatal(0, "ov_info %s: failed", path
);
257 while((n
= ov_read(vf
, buffer
, sizeof buffer
, 1/*bigendianp*/,
258 2/*bytes/word*/, 1/*signed*/, &bitstream
))) {
260 fatal(0, "ov_read %s: %ld", path
, n
);
262 fatal(0, "only single-bitstream ogg files are supported");
263 output_header(vi
->rate
, vi
->channels
, 16/*bits*/, n
);
264 if(fwrite(buffer
, 1, n
, outputfp
) < (size_t)n
)
265 fatal(errno
, "decoding %s: writing sample data", path
);
269 /** @brief Lookup table of decoders */
270 static const struct decoder decoders
[] = {
271 { "*.mp3", decode_mp3
},
272 { "*.MP3", decode_mp3
},
273 { "*.ogg", decode_ogg
},
274 { "*.OGG", decode_ogg
},
278 static const struct option options
[] = {
279 { "help", no_argument
, 0, 'h' },
280 { "version", no_argument
, 0, 'V' },
284 /* Display usage message and terminate. */
285 static void help(void) {
287 " disorder-decode [OPTIONS] PATH\n"
289 " --help, -h Display usage message\n"
290 " --version, -V Display version number\n"
292 "Audio decoder for DisOrder. Only intended to be used by speaker\n"
293 "process, not for normal users.\n");
298 /* Display version number and terminate. */
299 static void version(void) {
300 xprintf("disorder-decode version %s\n", disorder_version_string
);
305 int main(int argc
, char **argv
) {
310 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "calling setlocale");
311 while((n
= getopt_long(argc
, argv
, "hV", options
, 0)) >= 0) {
315 default: fatal(0, "invalid option");
319 fatal(0, "missing filename");
320 if(optind
+ 1 < argc
)
321 fatal(0, "excess arguments");
322 if((e
= getenv("DISORDER_RAW_FD"))) {
323 if(!(outputfp
= fdopen(atoi(e
), "wb")))
324 fatal(errno
, "fdopen");
330 && fnmatch(decoders
[n
].pattern
, path
, 0) != 0;
333 if(!decoders
[n
].pattern
)
334 fatal(0, "cannot determine file type for %s", path
);
335 decoders
[n
].decode();