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 *****************************************************************************/
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
40 #include <vlc_block.h>
41 #include <vlc_filter.h>
44 /*****************************************************************************
46 *****************************************************************************/
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;
63 /*****************************************************************************
65 *****************************************************************************/
67 set_category( CAT_INPUT )
68 set_subcategory( SUBCAT_INPUT_ACODEC )
69 set_description( N_("MPEG audio decoder") )
70 set_capability( "audio filter", 100 )
71 set_callbacks( OpenFilter, CloseFilter )
74 /*****************************************************************************
75 * DoWork: decode an MPEG audio frame.
76 *****************************************************************************/
77 static void DoWork( filter_t * p_filter,
78 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
80 filter_sys_t *p_sys = p_filter->p_sys;
82 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
83 p_out_buf->i_buffer = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t) *
84 aout_FormatNbChannels( &p_filter->fmt_out.audio );
86 /* Do the actual decoding now. */
87 mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
89 if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
91 msg_Dbg( p_filter, "libmad error: %s",
92 mad_stream_errorstr( &p_sys->mad_stream ) );
93 p_sys->i_reject_count = 3;
95 else if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
97 p_sys->i_reject_count = 3;
100 if( p_sys->i_reject_count > 0 )
102 if( p_filter->fmt_out.audio.i_format == VLC_CODEC_FL32 )
105 int i_size = p_out_buf->i_buffer / sizeof(float);
107 float * a = (float *)p_out_buf->p_buffer;
108 for ( i = 0 ; i < i_size ; i++ )
113 memset( p_out_buf->p_buffer, 0, p_out_buf->i_buffer );
115 p_sys->i_reject_count--;
120 mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
122 struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
123 unsigned int i_samples = p_pcm->length;
124 mad_fixed_t const * p_left = p_pcm->samples[0];
125 mad_fixed_t const * p_right = p_pcm->samples[1];
127 assert( i_samples == p_out_buf->i_nb_samples );
128 if ( p_filter->fmt_out.audio.i_format == VLC_CODEC_FI32 )
130 /* Interleave and keep buffers in mad_fixed_t format */
131 mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;
133 if ( p_pcm->channels == 2 )
135 while ( i_samples-- )
137 *p_samples++ = *p_left++;
138 *p_samples++ = *p_right++;
143 assert( p_pcm->channels == 1 );
144 vlc_memcpy( p_samples, p_left, i_samples * sizeof(mad_fixed_t) );
150 float * p_samples = (float *)p_out_buf->p_buffer;
151 const float f_temp = (float)FIXED32_ONE;
153 if ( p_pcm->channels == 2 )
155 while ( i_samples-- )
157 *p_samples++ = (float)*p_left++ / f_temp;
158 *p_samples++ = (float)*p_right++ / f_temp;
163 assert( p_pcm->channels == 1 );
164 while ( i_samples-- )
166 *p_samples++ = (float)*p_left++ / f_temp;
172 /*****************************************************************************
174 *****************************************************************************/
175 static int OpenFilter( vlc_object_t *p_this )
177 filter_t *p_filter = (filter_t *)p_this;
180 if( p_filter->fmt_in.i_codec != VLC_CODEC_MPGA &&
181 p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','3') )
183 if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
186 /* Allocate the memory needed to store the module's structure */
187 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
190 p_sys->i_reject_count = 0;
192 p_filter->pf_audio_filter = Convert;
194 /* Initialize libmad */
195 mad_stream_init( &p_sys->mad_stream );
196 mad_frame_init( &p_sys->mad_frame );
197 mad_synth_init( &p_sys->mad_synth );
198 mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
200 p_filter->fmt_out.i_codec = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_FI32;
201 p_filter->fmt_out.audio.i_format = p_filter->fmt_out.i_codec;
202 p_filter->fmt_out.audio.i_bitspersample =
203 aout_BitsPerSample( p_filter->fmt_out.i_codec );
205 msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
206 (char *)&p_filter->fmt_in.i_codec,
207 (char *)&p_filter->fmt_out.i_codec,
208 p_filter->fmt_in.audio.i_bitspersample );
213 /*****************************************************************************
214 * CloseFilter : deallocate data structures
215 *****************************************************************************/
216 static void CloseFilter( vlc_object_t *p_this )
218 filter_t *p_filter = (filter_t *)p_this;
219 filter_sys_t *p_sys = p_filter->p_sys;
221 mad_synth_finish( &p_sys->mad_synth );
222 mad_frame_finish( &p_sys->mad_frame );
223 mad_stream_finish( &p_sys->mad_stream );
227 static block_t *Convert( filter_t *p_filter, block_t *p_block )
229 if( !p_block || !p_block->i_nb_samples )
232 block_Release( p_block );
236 size_t i_out_size = p_block->i_nb_samples *
237 p_filter->fmt_out.audio.i_bitspersample *
238 p_filter->fmt_out.audio.i_channels / 8;
240 block_t *p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
243 msg_Warn( p_filter, "can't get output buffer" );
244 block_Release( p_block );
248 p_out->i_nb_samples = p_block->i_nb_samples;
249 p_out->i_dts = p_block->i_dts;
250 p_out->i_pts = p_block->i_pts;
251 p_out->i_length = p_block->i_length;
253 DoWork( p_filter, p_block, p_out );
255 block_Release( p_block );