c63ca2020738290b86cdda7070fdb8aa2a1c46e9
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
40 #include "speaker-protocol.h"
42 /** @brief Encoding lookup table type */
44 /** @brief Glob pattern matching file */
46 /** @brief Decoder function */
50 /** @brief Input file */
53 /** @brief Output file */
54 static FILE *outputfp
;
56 /** @brief Filename */
57 static const char *path
;
59 /** @brief Input buffer */
60 static unsigned char buffer
[1048576];
62 /** @brief Open the input file */
63 static void open_input(void) {
64 if((inputfd
= open(path
, O_RDONLY
)) < 0)
65 fatal(errno
, "opening %s", path
);
68 /** @brief Fill the buffer
69 * @return Number of bytes read
71 static size_t fill(void) {
72 int n
= read(inputfd
, buffer
, sizeof buffer
);
75 fatal(errno
, "reading from %s", path
);
79 /** @brief Write a 16-bit word in bigendian format */
80 static inline void output_16(uint16_t n
) {
81 if(putc(n
>> 8, outputfp
) < 0
82 || putc(n
& 0xFF, outputfp
) < 0)
83 fatal(errno
, "decoding %s: output error", path
);
86 /** @brief Write the header
87 * If called more than once, either does nothing (if you kept the same
88 * output encoding) or fails (if you changed it).
90 static void output_header(int rate
,
94 struct stream_header header
;
98 header
.channels
= channels
;
99 header
.endian
= ENDIAN_BIG
;
100 header
.nbytes
= nbytes
;
101 if(fwrite(&header
, sizeof header
, 1, outputfp
) < 1)
102 fatal(errno
, "decoding %s: writing format header", path
);
105 /** @brief Dithering state
106 * Filched from mpg321, which credits it to Robert Leslie */
107 struct audio_dither
{
108 mad_fixed_t error
[3];
112 /** @brief 32-bit PRNG
113 * Filched from mpg321, which credits it to Robert Leslie */
114 static inline unsigned long prng(unsigned long state
)
116 return (state
* 0x0019660dL
+ 0x3c6ef35fL
) & 0xffffffffL
;
119 /** @brief Generic linear sample quantize and dither routine
120 * Filched from mpg321, which credits it to Robert Leslie */
122 static long audio_linear_dither(mad_fixed_t sample
,
123 struct audio_dither
*dither
) {
124 unsigned int scalebits
;
125 mad_fixed_t output
, mask
, rnd
;
133 sample
+= dither
->error
[0] - dither
->error
[1] + dither
->error
[2];
135 dither
->error
[2] = dither
->error
[1];
136 dither
->error
[1] = dither
->error
[0] / 2;
139 output
= sample
+ (1L << (MAD_F_FRACBITS
+ 1 - bits
- 1));
141 scalebits
= MAD_F_FRACBITS
+ 1 - bits
;
142 mask
= (1L << scalebits
) - 1;
145 rnd
= prng(dither
->random
);
146 output
+= (rnd
& mask
) - (dither
->random
& mask
);
148 dither
->random
= rnd
;
157 else if (output
< MIN
) {
168 dither
->error
[0] = sample
- output
;
171 return output
>> scalebits
;
175 /** @brief MP3 output callback */
176 static enum mad_flow
mp3_output(void attribute((unused
)) *data
,
177 struct mad_header
const *header
,
178 struct mad_pcm
*pcm
) {
179 size_t n
= pcm
->length
;
180 const mad_fixed_t
*l
= pcm
->samples
[0], *r
= pcm
->samples
[1];
181 static struct audio_dither ld
[1], rd
[1];
183 output_header(header
->samplerate
,
186 2 * pcm
->channels
* pcm
->length
);
187 switch(pcm
->channels
) {
190 output_16(audio_linear_dither(*l
++, ld
));
194 output_16(audio_linear_dither(*l
++, ld
));
195 output_16(audio_linear_dither(*r
++, rd
));
199 fatal(0, "decoding %s: unsupported channel count %d", path
, pcm
->channels
);
201 return MAD_FLOW_CONTINUE
;
204 /** @brief MP3 input callback */
205 static enum mad_flow
mp3_input(void attribute((unused
)) *data
,
206 struct mad_stream
*stream
) {
207 const size_t n
= fill();
208 fprintf(stderr
, "n=%zu\n", n
);
210 return MAD_FLOW_STOP
;
211 mad_stream_buffer(stream
, buffer
, n
);
212 return MAD_FLOW_CONTINUE
;
216 /** @brief MP3 error callback */
217 static enum mad_flow
mp3_error(void attribute((unused
)) *data
,
218 struct mad_stream
*stream
,
219 struct mad_frame
attribute((unused
)) *frame
) {
221 /* Just generates pointless verbosity l-( */
222 error(0, "decoding %s: %s (%#04x)",
223 path
, mad_stream_errorstr(stream
), stream
->error
);
224 return MAD_FLOW_CONTINUE
;
227 /** @brief MP3 decoder */
228 static void decode_mp3(void) {
229 struct mad_decoder mad
[1];
232 mad_decoder_init(mad
, 0/*data*/, mp3_input
, 0/*header*/, 0/*filter*/,
233 mp3_output
, mp3_error
, 0/*message*/);
234 if(mad_decoder_run(mad
, MAD_DECODER_MODE_SYNC
))
236 mad_decoder_finish(mad
);
239 /** @brief Lookup table of decoders */
240 static const struct decoder decoders
[] = {
241 { "*.mp3", decode_mp3
},
242 { "*.MP3", decode_mp3
},
246 static const struct option options
[] = {
247 { "help", no_argument
, 0, 'h' },
248 { "version", no_argument
, 0, 'V' },
252 /* Display usage message and terminate. */
253 static void help(void) {
255 " disorder-decode [OPTIONS] PATH\n"
257 " --help, -h Display usage message\n"
258 " --version, -V Display version number\n"
260 "Audio decoder for DisOrder. Only intended to be used by speaker\n"
261 "process, not for normal users.\n");
266 /* Display version number and terminate. */
267 static void version(void) {
268 xprintf("disorder-decode version %s\n", disorder_version_string
);
273 int main(int argc
, char **argv
) {
278 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "calling setlocale");
279 while((n
= getopt_long(argc
, argv
, "hV", options
, 0)) >= 0) {
283 default: fatal(0, "invalid option");
287 fatal(0, "missing filename");
288 if(optind
+ 1 < argc
)
289 fatal(0, "excess arguments");
290 if((e
= getenv("DISORDER_RAW_FD"))) {
291 if(!(outputfp
= fdopen(atoi(e
), "wb")))
292 fatal(errno
, "fdopen");
298 && fnmatch(decoders
[n
].pattern
, path
, 0) != 0;
301 if(!decoders
[n
].pattern
)
302 fatal(0, "cannot determine file type for %s", path
);
303 decoders
[n
].decode();