xref: /OK3568_Linux_fs/external/rktoolkit/minimad.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * libmad - MPEG audio decoder library
3*4882a593Smuzhiyun  * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
6*4882a593Smuzhiyun  * it under the terms of the GNU General Public License as published by
7*4882a593Smuzhiyun  * the Free Software Foundation; either version 2 of the License, or
8*4882a593Smuzhiyun  * (at your option) any later version.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful,
11*4882a593Smuzhiyun  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*4882a593Smuzhiyun  * GNU General Public License for more details.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
16*4882a593Smuzhiyun  * along with this program; if not, write to the Free Software
17*4882a593Smuzhiyun  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * $Id: minimad.c,v 1.4 2004/01/23 09:41:32 rob Exp $
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun # include <stdio.h>
23*4882a593Smuzhiyun # include <unistd.h>
24*4882a593Smuzhiyun # include <sys/stat.h>
25*4882a593Smuzhiyun # include <sys/mman.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun # include "mad.h"
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun  * This is perhaps the simplest example use of the MAD high-level API.
31*4882a593Smuzhiyun  * Standard input is mapped into memory via mmap(), then the high-level API
32*4882a593Smuzhiyun  * is invoked with three callbacks: input, output, and error. The output
33*4882a593Smuzhiyun  * callback converts MAD's high-resolution PCM samples to 16 bits, then
34*4882a593Smuzhiyun  * writes them to standard output in little-endian, stereo-interleaved
35*4882a593Smuzhiyun  * format.
36*4882a593Smuzhiyun  */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun static int decode(unsigned char const *, unsigned long);
39*4882a593Smuzhiyun 
main(int argc,char * argv[])40*4882a593Smuzhiyun int main(int argc, char *argv[])
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun   struct stat stat;
43*4882a593Smuzhiyun   void *fdm;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun   if (argc != 1)
46*4882a593Smuzhiyun     return 1;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun   if (fstat(STDIN_FILENO, &stat) == -1 ||
49*4882a593Smuzhiyun       stat.st_size == 0)
50*4882a593Smuzhiyun     return 2;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun   fdm = mmap(0, stat.st_size, PROT_READ, MAP_SHARED, STDIN_FILENO, 0);
53*4882a593Smuzhiyun   if (fdm == MAP_FAILED)
54*4882a593Smuzhiyun     return 3;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun   decode(fdm, stat.st_size);
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun   if (munmap(fdm, stat.st_size) == -1)
59*4882a593Smuzhiyun     return 4;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun   return 0;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun  * This is a private message structure. A generic pointer to this structure
66*4882a593Smuzhiyun  * is passed to each of the callback functions. Put here any data you need
67*4882a593Smuzhiyun  * to access from within the callbacks.
68*4882a593Smuzhiyun  */
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun struct buffer {
71*4882a593Smuzhiyun   unsigned char const *start;
72*4882a593Smuzhiyun   unsigned long length;
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /*
76*4882a593Smuzhiyun  * This is the input callback. The purpose of this callback is to (re)fill
77*4882a593Smuzhiyun  * the stream buffer which is to be decoded. In this example, an entire file
78*4882a593Smuzhiyun  * has been mapped into memory, so we just call mad_stream_buffer() with the
79*4882a593Smuzhiyun  * address and length of the mapping. When this callback is called a second
80*4882a593Smuzhiyun  * time, we are finished decoding.
81*4882a593Smuzhiyun  */
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun static
input(void * data,struct mad_stream * stream)84*4882a593Smuzhiyun enum mad_flow input(void *data,
85*4882a593Smuzhiyun 		    struct mad_stream *stream)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun   struct buffer *buffer = data;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun   if (!buffer->length)
90*4882a593Smuzhiyun     return MAD_FLOW_STOP;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun   mad_stream_buffer(stream, buffer->start, buffer->length);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun   buffer->length = 0;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun   return MAD_FLOW_CONTINUE;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun  * The following utility routine performs simple rounding, clipping, and
101*4882a593Smuzhiyun  * scaling of MAD's high-resolution samples down to 16 bits. It does not
102*4882a593Smuzhiyun  * perform any dithering or noise shaping, which would be recommended to
103*4882a593Smuzhiyun  * obtain any exceptional audio quality. It is therefore not recommended to
104*4882a593Smuzhiyun  * use this routine if high-quality output is desired.
105*4882a593Smuzhiyun  */
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun static inline
scale(mad_fixed_t sample)108*4882a593Smuzhiyun signed int scale(mad_fixed_t sample)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun   /* round */
111*4882a593Smuzhiyun   sample += (1L << (MAD_F_FRACBITS - 16));
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun   /* clip */
114*4882a593Smuzhiyun   if (sample >= MAD_F_ONE)
115*4882a593Smuzhiyun     sample = MAD_F_ONE - 1;
116*4882a593Smuzhiyun   else if (sample < -MAD_F_ONE)
117*4882a593Smuzhiyun     sample = -MAD_F_ONE;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun   /* quantize */
120*4882a593Smuzhiyun   return sample >> (MAD_F_FRACBITS + 1 - 16);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun /*
124*4882a593Smuzhiyun  * This is the output callback function. It is called after each frame of
125*4882a593Smuzhiyun  * MPEG audio data has been completely decoded. The purpose of this callback
126*4882a593Smuzhiyun  * is to output (or play) the decoded PCM audio.
127*4882a593Smuzhiyun  */
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun static
output(void * data,struct mad_header const * header,struct mad_pcm * pcm)130*4882a593Smuzhiyun enum mad_flow output(void *data,
131*4882a593Smuzhiyun 		     struct mad_header const *header,
132*4882a593Smuzhiyun 		     struct mad_pcm *pcm)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun   unsigned int nchannels, nsamples;
135*4882a593Smuzhiyun   mad_fixed_t const *left_ch, *right_ch;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun   /* pcm->samplerate contains the sampling frequency */
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun   nchannels = pcm->channels;
140*4882a593Smuzhiyun   nsamples  = pcm->length;
141*4882a593Smuzhiyun   left_ch   = pcm->samples[0];
142*4882a593Smuzhiyun   right_ch  = pcm->samples[1];
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun   while (nsamples--) {
145*4882a593Smuzhiyun     signed int sample;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun     /* output sample(s) in 16-bit signed little-endian PCM */
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun     sample = scale(*left_ch++);
150*4882a593Smuzhiyun     putchar((sample >> 0) & 0xff);
151*4882a593Smuzhiyun     putchar((sample >> 8) & 0xff);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun     if (nchannels == 2) {
154*4882a593Smuzhiyun       sample = scale(*right_ch++);
155*4882a593Smuzhiyun       putchar((sample >> 0) & 0xff);
156*4882a593Smuzhiyun       putchar((sample >> 8) & 0xff);
157*4882a593Smuzhiyun     }
158*4882a593Smuzhiyun   }
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun   return MAD_FLOW_CONTINUE;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun  * This is the error callback function. It is called whenever a decoding
165*4882a593Smuzhiyun  * error occurs. The error is indicated by stream->error; the list of
166*4882a593Smuzhiyun  * possible MAD_ERROR_* errors can be found in the mad.h (or stream.h)
167*4882a593Smuzhiyun  * header file.
168*4882a593Smuzhiyun  */
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun static
error(void * data,struct mad_stream * stream,struct mad_frame * frame)171*4882a593Smuzhiyun enum mad_flow error(void *data,
172*4882a593Smuzhiyun 		    struct mad_stream *stream,
173*4882a593Smuzhiyun 		    struct mad_frame *frame)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun   struct buffer *buffer = data;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun   fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %u\n",
178*4882a593Smuzhiyun 	  stream->error, mad_stream_errorstr(stream),
179*4882a593Smuzhiyun 	  stream->this_frame - buffer->start);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun   /* return MAD_FLOW_BREAK here to stop decoding (and propagate an error) */
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun   return MAD_FLOW_CONTINUE;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun  * This is the function called by main() above to perform all the decoding.
188*4882a593Smuzhiyun  * It instantiates a decoder object and configures it with the input,
189*4882a593Smuzhiyun  * output, and error callback functions above. A single call to
190*4882a593Smuzhiyun  * mad_decoder_run() continues until a callback function returns
191*4882a593Smuzhiyun  * MAD_FLOW_STOP (to stop decoding) or MAD_FLOW_BREAK (to stop decoding and
192*4882a593Smuzhiyun  * signal an error).
193*4882a593Smuzhiyun  */
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun static
decode(unsigned char const * start,unsigned long length)196*4882a593Smuzhiyun int decode(unsigned char const *start, unsigned long length)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun   struct buffer buffer;
199*4882a593Smuzhiyun   struct mad_decoder decoder;
200*4882a593Smuzhiyun   int result;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun   /* initialize our private message structure */
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun   buffer.start  = start;
205*4882a593Smuzhiyun   buffer.length = length;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun   /* configure input, output, and error functions */
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun   mad_decoder_init(&decoder, &buffer,
210*4882a593Smuzhiyun 		   input, 0 /* header */, 0 /* filter */, output,
211*4882a593Smuzhiyun 		   error, 0 /* message */);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun   /* start decoding */
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun   result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun   /* release the decoder */
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun   mad_decoder_finish(&decoder);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun   return result;
222*4882a593Smuzhiyun }
223