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 VLC authors and VideoLAN
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 it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * NOTA BENE: this module requires the linking against a library which is
28 * known to require licensing under the GNU General Public License version 2
29 * (or later). Therefore, the result of compiling this module will normally
30 * be subject to the terms of that later license.
31 *****************************************************************************/
34 /*****************************************************************************
36 *****************************************************************************/
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
48 #include <vlc_block.h>
49 #include <vlc_filter.h>
51 /*****************************************************************************
53 *****************************************************************************/
54 static int OpenFilter ( vlc_object_t * );
55 static void CloseFilter( vlc_object_t * );
56 static block_t *Convert( filter_t *, block_t * );
58 /*****************************************************************************
60 *****************************************************************************/
63 struct mad_stream mad_stream;
64 struct mad_frame mad_frame;
65 struct mad_synth mad_synth;
70 /*****************************************************************************
72 *****************************************************************************/
74 set_category( CAT_INPUT )
75 set_subcategory( SUBCAT_INPUT_ACODEC )
76 set_description( N_("MPEG audio decoder") )
77 set_capability( "audio converter", 100 )
78 set_callbacks( OpenFilter, CloseFilter )
81 /*****************************************************************************
82 * DoWork: decode an MPEG audio frame.
83 *****************************************************************************/
84 static void DoWork( filter_t * p_filter,
85 block_t * p_in_buf, block_t * p_out_buf )
87 filter_sys_t *p_sys = p_filter->p_sys;
89 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
90 p_out_buf->i_buffer = p_in_buf->i_nb_samples * sizeof(float) *
91 aout_FormatNbChannels( &p_filter->fmt_out.audio );
93 /* Do the actual decoding now. */
94 mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
96 if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
98 msg_Dbg( p_filter, "libmad error: %s",
99 mad_stream_errorstr( &p_sys->mad_stream ) );
100 p_sys->i_reject_count = 3;
102 else if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
104 p_sys->i_reject_count = 3;
107 if( p_sys->i_reject_count > 0 )
109 memset( p_out_buf->p_buffer, 0, p_out_buf->i_buffer );
110 p_sys->i_reject_count--;
115 mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
117 struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
118 unsigned int i_samples = p_pcm->length;
119 mad_fixed_t const * p_left = p_pcm->samples[0];
120 mad_fixed_t const * p_right = p_pcm->samples[1];
121 float *p_samples = (float *)p_out_buf->p_buffer;
123 assert( i_samples == p_out_buf->i_nb_samples );
124 /* Interleave and keep buffers in mad_fixed_t format */
125 if ( p_pcm->channels == 2 )
127 while ( i_samples-- )
129 //assert( *p_left < MAD_F_ONE );
130 //assert( *p_left >= -MAD_F_ONE );
131 //assert( *p_right < MAD_F_ONE );
132 //assert( *p_right >= -MAD_F_ONE );
133 *p_samples++ = (float)*p_left++ / (float)MAD_F_ONE;
134 *p_samples++ = (float)*p_right++ / (float)MAD_F_ONE;
139 assert( p_pcm->channels == 1 );
140 while ( i_samples-- )
142 //assert( *p_left < MAD_F_ONE );
143 //assert( *p_left >= -MAD_F_ONE );
144 *p_samples++ = (float)*p_left++ / (float)MAD_F_ONE;
149 /*****************************************************************************
151 *****************************************************************************/
152 static int OpenFilter( vlc_object_t *p_this )
154 filter_t *p_filter = (filter_t *)p_this;
157 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_MPGA &&
158 p_filter->fmt_in.audio.i_format != VLC_FOURCC('m','p','g','3') )
161 if( p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
164 if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
167 /* Allocate the memory needed to store the module's structure */
168 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
171 p_sys->i_reject_count = 0;
173 p_filter->pf_audio_filter = Convert;
175 /* Initialize libmad */
176 mad_stream_init( &p_sys->mad_stream );
177 mad_frame_init( &p_sys->mad_frame );
178 mad_synth_init( &p_sys->mad_synth );
179 mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
181 msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
182 (char *)&p_filter->fmt_in.audio.i_format,
183 (char *)&p_filter->fmt_out.audio.i_format,
184 p_filter->fmt_out.audio.i_bitspersample );
189 /*****************************************************************************
190 * CloseFilter : deallocate data structures
191 *****************************************************************************/
192 static void CloseFilter( vlc_object_t *p_this )
194 filter_t *p_filter = (filter_t *)p_this;
195 filter_sys_t *p_sys = p_filter->p_sys;
197 mad_synth_finish( &p_sys->mad_synth );
198 mad_frame_finish( &p_sys->mad_frame );
199 mad_stream_finish( &p_sys->mad_stream );
203 static block_t *Convert( filter_t *p_filter, block_t *p_block )
205 if( !p_block || !p_block->i_nb_samples )
208 block_Release( p_block );
212 size_t i_out_size = p_block->i_nb_samples *
213 p_filter->fmt_out.audio.i_bitspersample *
214 p_filter->fmt_out.audio.i_channels / 8;
216 block_t *p_out = block_Alloc( i_out_size );
219 msg_Warn( p_filter, "can't get output buffer" );
220 block_Release( p_block );
224 p_out->i_nb_samples = p_block->i_nb_samples;
225 p_out->i_dts = p_block->i_dts;
226 p_out->i_pts = p_block->i_pts;
227 p_out->i_length = p_block->i_length;
229 DoWork( p_filter, p_block, p_out );
231 block_Release( p_block );