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(vlc_fixed_t) *
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 mad_fixed_t * p_samples = (mad_fixed_t *)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 *p_samples++ = *p_left++;
130 *p_samples++ = *p_right++;
135 assert( p_pcm->channels == 1 );
136 memcpy( p_samples, p_left, i_samples * sizeof(mad_fixed_t) );
140 /*****************************************************************************
142 *****************************************************************************/
143 static int OpenFilter( vlc_object_t *p_this )
145 filter_t *p_filter = (filter_t *)p_this;
148 if( p_filter->fmt_in.audio.i_format != VLC_CODEC_MPGA &&
149 p_filter->fmt_in.audio.i_format != VLC_FOURCC('m','p','g','3') )
152 if( p_filter->fmt_out.audio.i_format != VLC_CODEC_FI32 )
155 if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
158 /* Allocate the memory needed to store the module's structure */
159 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
162 p_sys->i_reject_count = 0;
164 p_filter->pf_audio_filter = Convert;
166 /* Initialize libmad */
167 mad_stream_init( &p_sys->mad_stream );
168 mad_frame_init( &p_sys->mad_frame );
169 mad_synth_init( &p_sys->mad_synth );
170 mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
172 msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
173 (char *)&p_filter->fmt_in.audio.i_format,
174 (char *)&p_filter->fmt_out.audio.i_format,
175 p_filter->fmt_out.audio.i_bitspersample );
180 /*****************************************************************************
181 * CloseFilter : deallocate data structures
182 *****************************************************************************/
183 static void CloseFilter( vlc_object_t *p_this )
185 filter_t *p_filter = (filter_t *)p_this;
186 filter_sys_t *p_sys = p_filter->p_sys;
188 mad_synth_finish( &p_sys->mad_synth );
189 mad_frame_finish( &p_sys->mad_frame );
190 mad_stream_finish( &p_sys->mad_stream );
194 static block_t *Convert( filter_t *p_filter, block_t *p_block )
196 if( !p_block || !p_block->i_nb_samples )
199 block_Release( p_block );
203 size_t i_out_size = p_block->i_nb_samples *
204 p_filter->fmt_out.audio.i_bitspersample *
205 p_filter->fmt_out.audio.i_channels / 8;
207 block_t *p_out = block_Alloc( i_out_size );
210 msg_Warn( p_filter, "can't get output buffer" );
211 block_Release( p_block );
215 p_out->i_nb_samples = p_block->i_nb_samples;
216 p_out->i_dts = p_block->i_dts;
217 p_out->i_pts = p_block->i_pts;
218 p_out->i_length = p_block->i_length;
220 DoWork( p_filter, p_block, p_out );
222 block_Release( p_block );