Remove residual tabs in codec directory.
[kugel-rb.git] / apps / codecs / libmad / minimad.c
blob7a23b346c210746f3dd8b848d8442be33b7024fb
1 /*
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
19 * $Id$
22 # include <stdio.h>
23 # include <unistd.h>
24 # include <sys/stat.h>
25 # include <sys/mman.h>
27 # include "mad.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
35 * format.
38 static int decode(unsigned char const *, unsigned long);
40 int main(int argc, char *argv[])
42 struct stat stat;
43 void *fdm;
45 if (argc != 1)
46 return 1;
48 if (fstat(STDIN_FILENO, &stat) == -1 ||
49 stat.st_size == 0)
50 return 2;
52 fdm = mmap(0, stat.st_size, PROT_READ, MAP_SHARED, STDIN_FILENO, 0);
53 if (fdm == MAP_FAILED)
54 return 3;
56 decode(fdm, stat.st_size);
58 if (munmap(fdm, stat.st_size) == -1)
59 return 4;
61 return 0;
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.
70 struct buffer {
71 unsigned char const *start;
72 unsigned long length;
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.
83 static
84 enum mad_flow input(void *data,
85 struct mad_stream *stream)
87 struct buffer *buffer = data;
89 if (!buffer->length)
90 return MAD_FLOW_STOP;
92 mad_stream_buffer(stream, buffer->start, buffer->length);
94 buffer->length = 0;
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.
107 static inline
108 signed int scale(mad_fixed_t sample)
110 /* round */
111 sample += (1L << (MAD_F_FRACBITS - 16));
113 /* clip */
114 if (sample >= MAD_F_ONE)
115 sample = MAD_F_ONE - 1;
116 else if (sample < -MAD_F_ONE)
117 sample = -MAD_F_ONE;
119 /* quantize */
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.
129 static
130 enum mad_flow output(void *data,
131 struct mad_header const *header,
132 struct mad_pcm *pcm)
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];
144 while (nsamples--) {
145 signed int sample;
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)
167 * header file.
170 static
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
192 * signal an error).
195 static
196 int decode(unsigned char const *start, unsigned long length)
198 struct buffer buffer;
199 struct mad_decoder decoder;
200 int result;
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 */);
213 /* start decoding */
215 result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
217 /* release the decoder */
219 mad_decoder_finish(&decoder);
221 return result;