2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
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,
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.
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 USA
24 # include <sys/stat.h>
25 # include <sys/mman.h>
30 * This is perhaps the simplest example use of the MAD high-level API.
31 * Standard input is mapped into memory via mmap(), then the high-level API
32 * is invoked with three callbacks: input, output, and error. The output
33 * callback converts MAD's high-resolution PCM samples to 16 bits, then
34 * writes them to standard output in little-endian, stereo-interleaved
38 static int decode(unsigned char const *, unsigned long);
40 int main(int argc
, char *argv
[])
48 if (fstat(STDIN_FILENO
, &stat
) == -1 ||
52 fdm
= mmap(0, stat
.st_size
, PROT_READ
, MAP_SHARED
, STDIN_FILENO
, 0);
53 if (fdm
== MAP_FAILED
)
56 decode(fdm
, stat
.st_size
);
58 if (munmap(fdm
, stat
.st_size
) == -1)
65 * This is a private message structure. A generic pointer to this structure
66 * is passed to each of the callback functions. Put here any data you need
67 * to access from within the callbacks.
71 unsigned char const *start
;
76 * This is the input callback. The purpose of this callback is to (re)fill
77 * the stream buffer which is to be decoded. In this example, an entire file
78 * has been mapped into memory, so we just call mad_stream_buffer() with the
79 * address and length of the mapping. When this callback is called a second
80 * time, we are finished decoding.
84 enum mad_flow
input(void *data
,
85 struct mad_stream
*stream
)
87 struct buffer
*buffer
= data
;
92 mad_stream_buffer(stream
, buffer
->start
, buffer
->length
);
96 return MAD_FLOW_CONTINUE
;
100 * The following utility routine performs simple rounding, clipping, and
101 * scaling of MAD's high-resolution samples down to 16 bits. It does not
102 * perform any dithering or noise shaping, which would be recommended to
103 * obtain any exceptional audio quality. It is therefore not recommended to
104 * use this routine if high-quality output is desired.
108 signed int scale(mad_fixed_t sample
)
111 sample
+= (1L << (MAD_F_FRACBITS
- 16));
114 if (sample
>= MAD_F_ONE
)
115 sample
= MAD_F_ONE
- 1;
116 else if (sample
< -MAD_F_ONE
)
120 return sample
>> (MAD_F_FRACBITS
+ 1 - 16);
124 * This is the output callback function. It is called after each frame of
125 * MPEG audio data has been completely decoded. The purpose of this callback
126 * is to output (or play) the decoded PCM audio.
130 enum mad_flow
output(void *data
,
131 struct mad_header
const *header
,
134 unsigned int nchannels
, nsamples
;
135 mad_fixed_t
const *left_ch
, *right_ch
;
137 /* pcm->samplerate contains the sampling frequency */
139 nchannels
= pcm
->channels
;
140 nsamples
= pcm
->length
;
141 left_ch
= pcm
->samples
[0];
142 right_ch
= pcm
->samples
[1];
147 /* output sample(s) in 16-bit signed little-endian PCM */
149 sample
= scale(*left_ch
++);
150 putchar((sample
>> 0) & 0xff);
151 putchar((sample
>> 8) & 0xff);
153 if (nchannels
== 2) {
154 sample
= scale(*right_ch
++);
155 putchar((sample
>> 0) & 0xff);
156 putchar((sample
>> 8) & 0xff);
160 return MAD_FLOW_CONTINUE
;
164 * This is the error callback function. It is called whenever a decoding
165 * error occurs. The error is indicated by stream->error; the list of
166 * possible MAD_ERROR_* errors can be found in the mad.h (or stream.h)
171 enum mad_flow
error(void *data
,
172 struct mad_stream
*stream
,
173 struct mad_frame
*frame
)
175 struct buffer
*buffer
= data
;
177 fprintf(stderr
, "decoding error 0x%04x (%s) at byte offset %u\n",
178 stream
->error
, mad_stream_errorstr(stream
),
179 stream
->this_frame
- buffer
->start
);
181 /* return MAD_FLOW_BREAK here to stop decoding (and propagate an error) */
183 return MAD_FLOW_CONTINUE
;
187 * This is the function called by main() above to perform all the decoding.
188 * It instantiates a decoder object and configures it with the input,
189 * output, and error callback functions above. A single call to
190 * mad_decoder_run() continues until a callback function returns
191 * MAD_FLOW_STOP (to stop decoding) or MAD_FLOW_BREAK (to stop decoding and
196 int decode(unsigned char const *start
, unsigned long length
)
198 struct buffer buffer
;
199 struct mad_decoder decoder
;
202 /* initialize our private message structure */
204 buffer
.start
= start
;
205 buffer
.length
= length
;
207 /* configure input, output, and error functions */
209 mad_decoder_init(&decoder
, &buffer
,
210 input
, 0 /* header */, 0 /* filter */, output
,
211 error
, 0 /* message */);
215 result
= mad_decoder_run(&decoder
, MAD_DECODER_MODE_SYNC
);
217 /* release the decoder */
219 mad_decoder_finish(&decoder
);