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
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 <vlc/decoder.h>
36 #include "aout_internal.h"
37 #include "vlc_filter.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Create ( vlc_object_t * );
43 static void Destroy ( vlc_object_t * );
44 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
47 static int OpenFilter ( vlc_object_t * );
48 static void CloseFilter( vlc_object_t * );
49 static block_t *Convert( filter_t *, block_t * );
51 /*****************************************************************************
53 *****************************************************************************/
56 struct mad_stream mad_stream;
57 struct mad_frame mad_frame;
58 struct mad_synth mad_synth;
61 /*****************************************************************************
63 *****************************************************************************/
65 set_category( CAT_AUDIO );
66 set_subcategory( SUBCAT_AUDIO_MISC );
67 set_description( _("MPEG audio decoder") );
68 set_capability( "audio filter", 100 );
69 set_callbacks( Create, Destroy );
72 set_description( _("MPEG audio decoder") );
73 set_capability( "audio filter2", 100 );
74 set_callbacks( OpenFilter, CloseFilter );
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Create( vlc_object_t *p_this )
82 aout_filter_t *p_filter = (aout_filter_t *)p_this;
83 struct filter_sys_t *p_sys;
85 if ( (p_filter->input.i_format != VLC_FOURCC('m','p','g','a')
86 && p_filter->input.i_format != VLC_FOURCC('m','p','g','3'))
87 || (p_filter->output.i_format != VLC_FOURCC('f','l','3','2')
88 && p_filter->output.i_format != VLC_FOURCC('f','i','3','2')) )
93 if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
98 /* Allocate the memory needed to store the module's structure */
99 p_sys = malloc( sizeof(filter_sys_t) );
100 p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
103 msg_Err( p_filter, "out of memory" );
107 /* Initialize libmad */
108 mad_stream_init( &p_sys->mad_stream );
109 mad_frame_init( &p_sys->mad_frame );
110 mad_synth_init( &p_sys->mad_synth );
111 mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
113 p_filter->pf_do_work = DoWork;
114 p_filter->b_in_place = 0;
119 /*****************************************************************************
120 * DoWork: decode an MPEG audio frame.
121 *****************************************************************************/
122 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
123 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
125 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
127 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
128 p_out_buf->i_nb_bytes = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t) *
129 aout_FormatNbChannels( &p_filter->input );
131 /* Do the actual decoding now. */
132 mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
133 p_in_buf->i_nb_bytes );
134 if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
136 msg_Dbg( p_aout, "libmad error: %s",
137 mad_stream_errorstr( &p_sys->mad_stream ) );
138 if( p_filter->output.i_format == VLC_FOURCC('f','l','3','2') )
141 int i_size = p_out_buf->i_nb_bytes / sizeof(float);
143 float * a = (float *)p_out_buf->p_buffer;
144 for ( i = 0 ; i < i_size ; i++ )
149 memset( p_out_buf->p_buffer, 0, p_out_buf->i_nb_bytes );
154 mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
156 if ( p_filter->output.i_format == VLC_FOURCC('f','i','3','2') )
158 /* Interleave and keep buffers in mad_fixed_t format */
159 mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;
160 struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
161 unsigned int i_samples = p_pcm->length;
162 mad_fixed_t const * p_left = p_pcm->samples[0];
163 mad_fixed_t const * p_right = p_pcm->samples[1];
165 switch ( p_pcm->channels )
168 while ( i_samples-- )
170 *p_samples++ = *p_left++;
171 *p_samples++ = *p_right++;
176 p_filter->p_vlc->pf_memcpy( p_samples, p_left,
177 i_samples * sizeof(mad_fixed_t) );
181 msg_Err( p_filter, "cannot interleave %i channels",
188 float * p_samples = (float *)p_out_buf->p_buffer;
189 struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
190 unsigned int i_samples = p_pcm->length;
191 mad_fixed_t const * p_left = p_pcm->samples[0];
192 mad_fixed_t const * p_right = p_pcm->samples[1];
193 float f_temp = (float)FIXED32_ONE;
195 switch ( p_pcm->channels )
198 while ( i_samples-- )
200 *p_samples++ = (float)*p_left++ / f_temp;
201 *p_samples++ = (float)*p_right++ / f_temp;
206 while ( i_samples-- )
208 *p_samples++ = (float)*p_left++ / f_temp;
213 msg_Err( p_filter, "cannot interleave %i channels",
219 /*****************************************************************************
220 * Destroy : deallocate data structures
221 *****************************************************************************/
222 static void Destroy( vlc_object_t *p_this )
224 aout_filter_t *p_filter = (aout_filter_t *)p_this;
225 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
227 mad_synth_finish( &p_sys->mad_synth );
228 mad_frame_finish( &p_sys->mad_frame );
229 mad_stream_finish( &p_sys->mad_stream );
233 /*****************************************************************************
235 *****************************************************************************/
236 static int OpenFilter( vlc_object_t *p_this )
238 filter_t *p_filter = (filter_t *)p_this;
241 if( p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') &&
242 p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','3') )
247 /* Allocate the memory needed to store the module's structure */
248 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
251 msg_Err( p_filter, "out of memory" );
255 p_filter->pf_audio_filter = Convert;
257 /* Initialize libmad */
258 mad_stream_init( &p_sys->mad_stream );
259 mad_frame_init( &p_sys->mad_frame );
260 mad_synth_init( &p_sys->mad_synth );
261 mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
263 msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
264 (char *)&p_filter->fmt_in.i_codec,
265 (char *)&p_filter->fmt_out.i_codec,
266 p_filter->fmt_out.audio.i_bitspersample );
268 p_filter->fmt_out.i_codec =
269 p_filter->fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
270 p_filter->fmt_out.audio.i_bitspersample = sizeof(float);
275 /*****************************************************************************
276 * CloseFilter : deallocate data structures
277 *****************************************************************************/
278 static void CloseFilter( vlc_object_t *p_this )
280 filter_t *p_filter = (filter_t *)p_this;
281 filter_sys_t *p_sys = p_filter->p_sys;
283 mad_synth_finish( &p_sys->mad_synth );
284 mad_frame_finish( &p_sys->mad_frame );
285 mad_stream_finish( &p_sys->mad_stream );
289 static block_t *Convert( filter_t *p_filter, block_t *p_block )
291 aout_filter_t aout_filter;
292 aout_buffer_t in_buf, out_buf;
296 if( !p_block || !p_block->i_samples )
298 if( p_block ) p_block->pf_release( p_block );
302 i_out_size = p_block->i_samples *
303 p_filter->fmt_out.audio.i_bitspersample *
304 p_filter->fmt_out.audio.i_channels / 8;
306 p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
309 msg_Warn( p_filter, "can't get output buffer" );
310 p_block->pf_release( p_block );
314 p_out->i_samples = p_block->i_samples;
315 p_out->i_dts = p_block->i_dts;
316 p_out->i_pts = p_block->i_pts;
317 p_out->i_length = p_block->i_length;
319 aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
320 aout_filter.input = p_filter->fmt_in.audio;
321 aout_filter.input.i_format = p_filter->fmt_in.i_codec;
322 aout_filter.output = p_filter->fmt_out.audio;
323 aout_filter.output.i_format = p_filter->fmt_out.i_codec;
325 in_buf.p_buffer = p_block->p_buffer;
326 in_buf.i_nb_bytes = p_block->i_buffer;
327 in_buf.i_nb_samples = p_block->i_samples;
328 out_buf.p_buffer = p_out->p_buffer;
329 out_buf.i_nb_bytes = p_out->i_buffer;
330 out_buf.i_nb_samples = p_out->i_samples;
332 DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
334 p_block->pf_release( p_block );
336 p_out->i_buffer = out_buf.i_nb_bytes;
337 p_out->i_samples = out_buf.i_nb_samples;