1 /*****************************************************************************
2 * mpgatofixed32.c: MPEG-1 & 2 audio layer I, II, III + MPEG 2.5 decoder,
3 * using MAD (MPEG Audio Decoder)
4 *****************************************************************************
5 * Copyright (C) 2001 by Jean-Paul Saman
6 * $Id: mpgatofixed32.c,v 1.1 2003/01/15 10:58:47 massiot Exp $
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * Jean-Paul Saman <jpsaman@wxs.nl>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <string.h> /* strdup() */
35 #include "audio_output.h"
36 #include "aout_internal.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Create ( vlc_object_t * );
42 static void Destroy ( vlc_object_t * );
43 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
46 /*****************************************************************************
48 *****************************************************************************/
49 struct aout_filter_sys_t
51 struct mad_stream mad_stream;
52 struct mad_frame mad_frame;
53 struct mad_synth mad_synth;
56 /*****************************************************************************
58 *****************************************************************************/
60 add_category_hint( N_("Miscellaneous"), NULL );
61 set_description( _("MPEG audio decoder module") );
62 set_capability( "audio filter", 100 );
63 set_callbacks( Create, Destroy );
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Create( vlc_object_t * _p_filter )
71 aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
72 struct aout_filter_sys_t * p_sys;
74 if ( (p_filter->input.i_format != VLC_FOURCC('m','p','g','a')
75 && p_filter->input.i_format != VLC_FOURCC('m','p','g','3'))
76 || (p_filter->output.i_format != VLC_FOURCC('f','l','3','2')
77 && p_filter->output.i_format != VLC_FOURCC('f','i','3','2')) )
82 if ( p_filter->input.i_rate != p_filter->output.i_rate )
87 if ( p_filter->input.i_original_channels !=
88 p_filter->output.i_original_channels
89 && p_filter->input.i_physical_channels !=
90 p_filter->output.i_physical_channels )
95 /* Allocate the memory needed to store the module's structure */
96 p_sys = p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
99 msg_Err( p_filter, "out of memory" );
103 /* Initialize libmad */
104 mad_stream_init( &p_sys->mad_stream );
105 mad_frame_init( &p_sys->mad_frame );
106 mad_synth_init( &p_sys->mad_synth );
107 mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
109 p_filter->pf_do_work = DoWork;
110 p_filter->b_in_place = 0;
115 /*****************************************************************************
116 * DoWork: decode an MPEG audio frame.
117 *****************************************************************************/
118 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
119 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
121 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
124 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
125 p_out_buf->i_nb_bytes = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t);
127 /* Do the actual decoding now. */
128 mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
129 p_in_buf->i_nb_bytes );
130 if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
132 msg_Warn( p_aout, "libmad error: %s",
133 mad_stream_errorstr( &p_sys->mad_stream ) );
134 memset( p_out_buf->p_buffer, 0, p_out_buf->i_nb_bytes );
137 mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
139 if ( p_filter->output.i_format == VLC_FOURCC('f','i','3','2') )
141 /* Interleave and keep buffers in mad_fixed_t format */
142 mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;
143 struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
144 unsigned int i_samples = p_pcm->length;
145 mad_fixed_t const * p_left = p_pcm->samples[0];
146 mad_fixed_t const * p_right = p_pcm->samples[1];
148 switch ( p_pcm->channels )
151 while ( i_samples-- )
153 *p_samples++ = *p_left++;
154 *p_samples++ = *p_right++;
159 p_filter->p_vlc->pf_memcpy( p_samples, p_left,
160 i_samples * sizeof(mad_fixed_t) );
164 msg_Err( p_filter, "cannot interleave %i channels",
171 float * p_samples = (float *)p_out_buf->p_buffer;
172 struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
173 unsigned int i_samples = p_pcm->length;
174 mad_fixed_t const * p_left = p_pcm->samples[0];
175 mad_fixed_t const * p_right = p_pcm->samples[1];
177 switch ( p_pcm->channels )
180 while ( i_samples-- )
182 *p_samples++ = (float)*p_left++ / (float)FIXED32_ONE;
183 *p_samples++ = (float)*p_right++ / (float)FIXED32_ONE;
188 while ( i_samples-- )
190 *p_samples++ = (float)*p_left++ / (float)FIXED32_ONE;
195 msg_Err( p_filter, "cannot interleave %i channels",
201 /*****************************************************************************
202 * Destroy : deallocate data structures
203 *****************************************************************************/
204 static void Destroy( vlc_object_t * _p_filter )
206 aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
207 struct aout_filter_sys_t * p_sys = p_filter->p_sys;
209 mad_synth_finish( &p_sys->mad_synth );
210 mad_frame_finish( &p_sys->mad_frame );
211 mad_stream_finish( &p_sys->mad_stream );