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"
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Create ( vlc_object_t * );
46 static void Destroy ( vlc_object_t * );
47 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
50 static int OpenFilter ( vlc_object_t * );
51 static void CloseFilter( vlc_object_t * );
52 static block_t *Convert( filter_t *, block_t * );
54 /*****************************************************************************
56 *****************************************************************************/
59 struct mad_stream mad_stream;
60 struct mad_frame mad_frame;
61 struct mad_synth mad_synth;
66 /*****************************************************************************
68 *****************************************************************************/
70 set_category( CAT_INPUT )
71 set_subcategory( SUBCAT_INPUT_ACODEC )
72 set_description( N_("MPEG audio decoder") )
73 set_capability( "audio filter", 100 )
74 set_callbacks( Create, Destroy )
77 set_description( N_("MPEG audio decoder") )
78 set_capability( "audio filter2", 100 )
79 set_callbacks( OpenFilter, CloseFilter )
82 /*****************************************************************************
84 *****************************************************************************/
85 static int Create( vlc_object_t *p_this )
87 aout_filter_t *p_filter = (aout_filter_t *)p_this;
88 struct filter_sys_t *p_sys;
90 if ( (p_filter->input.i_format != VLC_CODEC_MPGA
91 && p_filter->input.i_format != VLC_FOURCC('m','p','g','3'))
92 || (p_filter->output.i_format != VLC_CODEC_FL32
93 && p_filter->output.i_format != VLC_CODEC_FI32) )
98 if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
103 /* Allocate the memory needed to store the module's structure */
104 p_sys = malloc( sizeof(filter_sys_t) );
105 p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
109 /* Initialize libmad */
110 mad_stream_init( &p_sys->mad_stream );
111 mad_frame_init( &p_sys->mad_frame );
112 mad_synth_init( &p_sys->mad_synth );
113 mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
114 p_sys->i_reject_count = 0;
116 p_filter->pf_do_work = DoWork;
117 p_filter->b_in_place = 0;
122 /*****************************************************************************
123 * DoWork: decode an MPEG audio frame.
124 *****************************************************************************/
125 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
126 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
128 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
130 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
131 p_out_buf->i_nb_bytes = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t) *
132 aout_FormatNbChannels( &p_filter->output );
134 /* Do the actual decoding now. */
135 mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
136 p_in_buf->i_nb_bytes );
137 if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
139 msg_Dbg( p_aout, "libmad error: %s",
140 mad_stream_errorstr( &p_sys->mad_stream ) );
141 p_sys->i_reject_count = 3;
143 else if( p_in_buf->b_discontinuity )
145 p_sys->i_reject_count = 3;
148 if( p_sys->i_reject_count > 0 )
150 if( p_filter->output.i_format == VLC_CODEC_FL32 )
153 int i_size = p_out_buf->i_nb_bytes / sizeof(float);
155 float * a = (float *)p_out_buf->p_buffer;
156 for ( i = 0 ; i < i_size ; i++ )
161 memset( p_out_buf->p_buffer, 0, p_out_buf->i_nb_bytes );
163 p_sys->i_reject_count--;
168 mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
170 if ( p_filter->output.i_format == VLC_CODEC_FI32 )
172 /* Interleave and keep buffers in mad_fixed_t format */
173 mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;
174 struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
175 unsigned int i_samples = p_pcm->length;
176 mad_fixed_t const * p_left = p_pcm->samples[0];
177 mad_fixed_t const * p_right = p_pcm->samples[1];
179 switch ( p_pcm->channels )
182 if ( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
184 while ( i_samples-- )
186 *p_samples++ = (*p_left++ >> 1) + (*p_right++ >> 1);
189 else if ( p_filter->output.i_original_channels == AOUT_CHAN_LEFT )
191 while ( i_samples-- )
193 *p_samples++ = *p_left;
194 *p_samples++ = *p_left++;
197 else if ( p_filter->output.i_original_channels == AOUT_CHAN_RIGHT )
199 while ( i_samples-- )
201 *p_samples++ = *p_right;
202 *p_samples++ = *p_right++;
207 while ( i_samples-- )
209 *p_samples++ = *p_left++;
210 *p_samples++ = *p_right++;
216 vlc_memcpy( p_samples, p_left, i_samples * sizeof(mad_fixed_t) );
220 msg_Err( p_aout, "cannot interleave %i channels",
227 float * p_samples = (float *)p_out_buf->p_buffer;
228 struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
229 unsigned int i_samples = p_pcm->length;
230 mad_fixed_t const * p_left = p_pcm->samples[0];
231 mad_fixed_t const * p_right = p_pcm->samples[1];
232 float f_temp = (float)FIXED32_ONE;
234 switch ( p_pcm->channels )
237 if ( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
239 while ( i_samples-- )
241 *p_samples++ = (float)*p_left++ / f_temp / 2 +
242 (float)*p_right++ / f_temp / 2;
245 else if ( p_filter->output.i_original_channels == AOUT_CHAN_LEFT )
247 while ( i_samples-- )
249 *p_samples++ = (float)*p_left / f_temp;
250 *p_samples++ = (float)*p_left++ / f_temp;
253 else if ( p_filter->output.i_original_channels == AOUT_CHAN_RIGHT )
255 while ( i_samples-- )
257 *p_samples++ = (float)*p_right / f_temp;
258 *p_samples++ = (float)*p_right++ / f_temp;
263 while ( i_samples-- )
265 *p_samples++ = (float)*p_left++ / f_temp;
266 *p_samples++ = (float)*p_right++ / f_temp;
272 while ( i_samples-- )
274 *p_samples++ = (float)*p_left++ / f_temp;
279 msg_Err( p_aout, "cannot interleave %i channels",
285 /*****************************************************************************
286 * Destroy : deallocate data structures
287 *****************************************************************************/
288 static void Destroy( vlc_object_t *p_this )
290 aout_filter_t *p_filter = (aout_filter_t *)p_this;
291 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
293 mad_synth_finish( &p_sys->mad_synth );
294 mad_frame_finish( &p_sys->mad_frame );
295 mad_stream_finish( &p_sys->mad_stream );
299 /*****************************************************************************
301 *****************************************************************************/
302 static int OpenFilter( vlc_object_t *p_this )
304 filter_t *p_filter = (filter_t *)p_this;
307 if( p_filter->fmt_in.i_codec != VLC_CODEC_MPGA &&
308 p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','3') )
313 /* Allocate the memory needed to store the module's structure */
314 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
317 p_sys->i_reject_count = 0;
319 p_filter->pf_audio_filter = Convert;
321 /* Initialize libmad */
322 mad_stream_init( &p_sys->mad_stream );
323 mad_frame_init( &p_sys->mad_frame );
324 mad_synth_init( &p_sys->mad_synth );
325 mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
327 if( vlc_CPU() & CPU_CAPABILITY_FPU )
328 p_filter->fmt_out.i_codec = VLC_CODEC_FL32;
330 p_filter->fmt_out.i_codec = VLC_CODEC_FI32;
331 p_filter->fmt_out.audio.i_format = p_filter->fmt_out.i_codec;
332 p_filter->fmt_out.audio.i_bitspersample =
333 aout_BitsPerSample( p_filter->fmt_out.i_codec );
335 p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
337 msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
338 (char *)&p_filter->fmt_in.i_codec,
339 (char *)&p_filter->fmt_out.i_codec,
340 p_filter->fmt_in.audio.i_bitspersample );
345 /*****************************************************************************
346 * CloseFilter : deallocate data structures
347 *****************************************************************************/
348 static void CloseFilter( vlc_object_t *p_this )
350 filter_t *p_filter = (filter_t *)p_this;
351 filter_sys_t *p_sys = p_filter->p_sys;
353 mad_synth_finish( &p_sys->mad_synth );
354 mad_frame_finish( &p_sys->mad_frame );
355 mad_stream_finish( &p_sys->mad_stream );
359 static block_t *Convert( filter_t *p_filter, block_t *p_block )
361 aout_filter_t aout_filter;
362 aout_buffer_t in_buf, out_buf;
366 if( !p_block || !p_block->i_samples )
369 block_Release( p_block );
373 i_out_size = p_block->i_samples *
374 p_filter->fmt_out.audio.i_bitspersample *
375 p_filter->fmt_out.audio.i_channels / 8;
377 p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
380 msg_Warn( p_filter, "can't get output buffer" );
381 block_Release( p_block );
385 p_out->i_samples = p_block->i_samples;
386 p_out->i_dts = p_block->i_dts;
387 p_out->i_pts = p_block->i_pts;
388 p_out->i_length = p_block->i_length;
390 aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
391 aout_filter.input = p_filter->fmt_in.audio;
392 aout_filter.input.i_format = p_filter->fmt_in.i_codec;
393 aout_filter.output = p_filter->fmt_out.audio;
394 aout_filter.output.i_format = p_filter->fmt_out.i_codec;
396 in_buf.p_buffer = p_block->p_buffer;
397 in_buf.b_discontinuity = false;
398 in_buf.i_nb_bytes = p_block->i_buffer;
399 in_buf.i_nb_samples = p_block->i_samples;
400 out_buf.p_buffer = p_out->p_buffer;
401 out_buf.i_nb_bytes = p_out->i_buffer;
402 out_buf.i_nb_samples = p_out->i_samples;
404 DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
406 block_Release( p_block );
408 p_out->i_buffer = out_buf.i_nb_bytes;
409 p_out->i_samples = out_buf.i_nb_samples;