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 /* Do the actual decoding now. */
90 mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
92 if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
94 msg_Err( p_filter, "libmad error: %s",
95 mad_stream_errorstr( &p_sys->mad_stream ) );
96 if( !MAD_RECOVERABLE( p_sys->mad_stream.error ) )
97 p_sys->i_reject_count = 3;
99 else if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
101 p_sys->i_reject_count = 3;
104 if( p_sys->i_reject_count > 0 )
107 memset( p_out_buf->p_buffer, 0, p_out_buf->i_buffer );
108 p_sys->i_reject_count--;
113 mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
115 struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
116 unsigned int i_samples = p_pcm->length;
117 mad_fixed_t const * p_left = p_pcm->samples[0];
118 mad_fixed_t const * p_right = p_pcm->samples[1];
119 float *p_samples = (float *)p_out_buf->p_buffer;
121 if (p_pcm->channels > p_filter->fmt_out.audio.i_channels)
123 msg_Err( p_filter, "wrong channels count (corrupt stream?): %u > %u",
124 p_pcm->channels, p_filter->fmt_out.audio.i_channels);
125 p_sys->i_reject_count = 3;
129 if( i_samples != p_out_buf->i_nb_samples )
131 msg_Err( p_filter, "unexpected samples count (corrupt stream?): "
132 "%u / %u", i_samples, p_out_buf->i_nb_samples );
133 p_sys->i_reject_count = 3;
137 /* Interleave and keep buffers in mad_fixed_t format */
138 if ( p_pcm->channels == 2 )
140 while ( i_samples-- )
142 //assert( *p_left < MAD_F_ONE );
143 //assert( *p_left >= -MAD_F_ONE );
144 //assert( *p_right < MAD_F_ONE );
145 //assert( *p_right >= -MAD_F_ONE );
146 *p_samples++ = (float)*p_left++ / (float)MAD_F_ONE;
147 *p_samples++ = (float)*p_right++ / (float)MAD_F_ONE;
152 assert( p_pcm->channels == 1 );
153 while ( i_samples-- )
155 //assert( *p_left < MAD_F_ONE );
156 //assert( *p_left >= -MAD_F_ONE );
157 *p_samples++ = (float)*p_left++ / (float)MAD_F_ONE;
162 /*****************************************************************************
164 *****************************************************************************/
165 static int OpenFilter( vlc_object_t *p_this )
167 filter_t *p_filter = (filter_t *)p_this;
170 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_MPGA &&
171 p_filter->fmt_in.audio.i_format != VLC_CODEC_MP3 &&
172 p_filter->fmt_in.audio.i_format != VLC_FOURCC('m','p','g','3') )
175 if( p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
178 if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
181 /* Allocate the memory needed to store the module's structure */
182 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
185 p_sys->i_reject_count = 0;
188 /* Initialize libmad */
189 mad_stream_init( &p_sys->mad_stream );
190 mad_frame_init( &p_sys->mad_frame );
191 mad_synth_init( &p_sys->mad_synth );
192 mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
194 msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
195 (char *)&p_filter->fmt_in.audio.i_format,
196 (char *)&p_filter->fmt_out.audio.i_format,
197 p_filter->fmt_out.audio.i_bitspersample );
199 p_filter->pf_audio_filter = Convert;
204 /*****************************************************************************
205 * CloseFilter : deallocate data structures
206 *****************************************************************************/
207 static void CloseFilter( vlc_object_t *p_this )
209 filter_t *p_filter = (filter_t *)p_this;
210 filter_sys_t *p_sys = p_filter->p_sys;
212 mad_synth_finish( &p_sys->mad_synth );
213 mad_frame_finish( &p_sys->mad_frame );
214 mad_stream_finish( &p_sys->mad_stream );
218 static block_t *Convert( filter_t *p_filter, block_t *p_block )
220 if( !p_block || !p_block->i_nb_samples )
223 block_Release( p_block );
227 size_t i_out_size = p_block->i_nb_samples *
228 p_filter->fmt_out.audio.i_bitspersample *
229 p_filter->fmt_out.audio.i_channels / 8;
231 block_t *p_out = block_Alloc( i_out_size );
232 if( unlikely( !p_out ) )
234 msg_Warn( p_filter, "can't get output buffer" );
235 block_Release( p_block );
239 p_out->i_nb_samples = p_block->i_nb_samples;
240 p_out->i_dts = p_block->i_dts;
241 p_out->i_pts = p_block->i_pts;
242 p_out->i_length = p_block->i_length;
244 DoWork( p_filter, p_block, p_out );
246 block_Release( p_block );