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-2005 the VideoLAN team
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
39 #include <vlc_block.h>
40 #include <vlc_filter.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int OpenFilter ( vlc_object_t * );
47 static void CloseFilter( vlc_object_t * );
48 static block_t *Convert( filter_t *, block_t * );
50 /*****************************************************************************
52 *****************************************************************************/
55 struct mad_stream mad_stream;
56 struct mad_frame mad_frame;
57 struct mad_synth mad_synth;
62 /*****************************************************************************
64 *****************************************************************************/
66 set_category( CAT_INPUT )
67 set_subcategory( SUBCAT_INPUT_ACODEC )
68 set_description( N_("MPEG audio decoder") )
69 set_capability( "audio filter2", 100 )
70 set_callbacks( OpenFilter, CloseFilter )
73 /*****************************************************************************
74 * DoWork: decode an MPEG audio frame.
75 *****************************************************************************/
76 static void DoWork( filter_t * p_filter,
77 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
79 filter_sys_t *p_sys = p_filter->p_sys;
81 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
82 p_out_buf->i_buffer = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t) *
83 aout_FormatNbChannels( &p_filter->fmt_out.audio );
85 /* Do the actual decoding now. */
86 mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
88 if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
90 msg_Dbg( p_filter, "libmad error: %s",
91 mad_stream_errorstr( &p_sys->mad_stream ) );
92 p_sys->i_reject_count = 3;
94 else if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
96 p_sys->i_reject_count = 3;
99 if( p_sys->i_reject_count > 0 )
101 if( p_filter->fmt_out.audio.i_format == VLC_CODEC_FL32 )
104 int i_size = p_out_buf->i_buffer / sizeof(float);
106 float * a = (float *)p_out_buf->p_buffer;
107 for ( i = 0 ; i < i_size ; i++ )
112 memset( p_out_buf->p_buffer, 0, p_out_buf->i_buffer );
114 p_sys->i_reject_count--;
119 mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
121 if ( p_filter->fmt_out.audio.i_format == VLC_CODEC_FI32 )
123 /* Interleave and keep buffers in mad_fixed_t format */
124 mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;
125 struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
126 unsigned int i_samples = p_pcm->length;
127 mad_fixed_t const * p_left = p_pcm->samples[0];
128 mad_fixed_t const * p_right = p_pcm->samples[1];
130 switch ( p_pcm->channels )
133 if ( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
135 while ( i_samples-- )
137 *p_samples++ = (*p_left++ >> 1) + (*p_right++ >> 1);
140 else if ( p_filter->fmt_out.audio.i_original_channels == AOUT_CHAN_LEFT )
142 while ( i_samples-- )
144 *p_samples++ = *p_left;
145 *p_samples++ = *p_left++;
148 else if ( p_filter->fmt_out.audio.i_original_channels == AOUT_CHAN_RIGHT )
150 while ( i_samples-- )
152 *p_samples++ = *p_right;
153 *p_samples++ = *p_right++;
158 while ( i_samples-- )
160 *p_samples++ = *p_left++;
161 *p_samples++ = *p_right++;
167 vlc_memcpy( p_samples, p_left, i_samples * sizeof(mad_fixed_t) );
171 msg_Err( p_filter, "cannot interleave %i channels",
178 float * p_samples = (float *)p_out_buf->p_buffer;
179 struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
180 unsigned int i_samples = p_pcm->length;
181 mad_fixed_t const * p_left = p_pcm->samples[0];
182 mad_fixed_t const * p_right = p_pcm->samples[1];
183 float f_temp = (float)FIXED32_ONE;
185 switch ( p_pcm->channels )
188 if ( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
190 while ( i_samples-- )
192 *p_samples++ = (float)*p_left++ / f_temp / 2 +
193 (float)*p_right++ / f_temp / 2;
196 else if ( p_filter->fmt_out.audio.i_original_channels == AOUT_CHAN_LEFT )
198 while ( i_samples-- )
200 *p_samples++ = (float)*p_left / f_temp;
201 *p_samples++ = (float)*p_left++ / f_temp;
204 else if ( p_filter->fmt_out.audio.i_original_channels == AOUT_CHAN_RIGHT )
206 while ( i_samples-- )
208 *p_samples++ = (float)*p_right / f_temp;
209 *p_samples++ = (float)*p_right++ / f_temp;
214 while ( i_samples-- )
216 *p_samples++ = (float)*p_left++ / f_temp;
217 *p_samples++ = (float)*p_right++ / f_temp;
223 while ( i_samples-- )
225 *p_samples++ = (float)*p_left++ / f_temp;
230 msg_Err( p_filter, "cannot interleave %i channels",
236 /*****************************************************************************
238 *****************************************************************************/
239 static int OpenFilter( vlc_object_t *p_this )
241 filter_t *p_filter = (filter_t *)p_this;
244 if( p_filter->fmt_in.i_codec != VLC_CODEC_MPGA &&
245 p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','3') )
250 /* Allocate the memory needed to store the module's structure */
251 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
254 p_sys->i_reject_count = 0;
256 p_filter->pf_audio_filter = Convert;
258 /* Initialize libmad */
259 mad_stream_init( &p_sys->mad_stream );
260 mad_frame_init( &p_sys->mad_frame );
261 mad_synth_init( &p_sys->mad_synth );
262 mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
264 p_filter->fmt_out.i_codec = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_FI32;
265 p_filter->fmt_out.audio.i_format = p_filter->fmt_out.i_codec;
266 p_filter->fmt_out.audio.i_bitspersample =
267 aout_BitsPerSample( p_filter->fmt_out.i_codec );
269 p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
271 msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
272 (char *)&p_filter->fmt_in.i_codec,
273 (char *)&p_filter->fmt_out.i_codec,
274 p_filter->fmt_in.audio.i_bitspersample );
279 /*****************************************************************************
280 * CloseFilter : deallocate data structures
281 *****************************************************************************/
282 static void CloseFilter( vlc_object_t *p_this )
284 filter_t *p_filter = (filter_t *)p_this;
285 filter_sys_t *p_sys = p_filter->p_sys;
287 mad_synth_finish( &p_sys->mad_synth );
288 mad_frame_finish( &p_sys->mad_frame );
289 mad_stream_finish( &p_sys->mad_stream );
293 static block_t *Convert( filter_t *p_filter, block_t *p_block )
295 if( !p_block || !p_block->i_nb_samples )
298 block_Release( p_block );
302 size_t i_out_size = p_block->i_nb_samples *
303 p_filter->fmt_out.audio.i_bitspersample *
304 p_filter->fmt_out.audio.i_channels / 8;
306 block_t *p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
309 msg_Warn( p_filter, "can't get output buffer" );
310 block_Release( p_block );
314 p_out->i_nb_samples = p_block->i_nb_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 DoWork( p_filter, p_block, p_out );
321 block_Release( p_block );