]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/mpgatofixed32.c
74c64a12017b9056baaf381b66e2d56f8c05187b
[vlc] / modules / audio_filter / converter / mpgatofixed32.c
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
6  * $Id$
7  *
8  * Authors: Christophe Massiot <massiot@via.ecp.fr>
9  *          Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #include <mad.h>
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_aout.h>
39 #include <vlc_block.h>
40 #include <vlc_filter.h>
41 #include <vlc_cpu.h>
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int  OpenFilter ( vlc_object_t * );
47 static void CloseFilter( vlc_object_t * );
48 static block_t *Convert( filter_t *, block_t * );
49
50 /*****************************************************************************
51  * Local structures
52  *****************************************************************************/
53 struct filter_sys_t
54 {
55     struct mad_stream mad_stream;
56     struct mad_frame  mad_frame;
57     struct mad_synth  mad_synth;
58
59     int               i_reject_count;
60 };
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin ()
66     set_category( CAT_INPUT )
67     set_subcategory( SUBCAT_INPUT_ACODEC )
68     set_description( N_("MPEG audio decoder") )
69     set_capability( "audio filter2", 100 )
70     set_callbacks( OpenFilter, CloseFilter )
71 vlc_module_end ()
72
73 /*****************************************************************************
74  * DoWork: decode an MPEG audio frame.
75  *****************************************************************************/
76 static void DoWork( filter_t * p_filter,
77                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
78 {
79     filter_sys_t *p_sys = p_filter->p_sys;
80
81     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
82     p_out_buf->i_buffer = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t) *
83                                aout_FormatNbChannels( &p_filter->fmt_out.audio );
84
85     /* Do the actual decoding now. */
86     mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
87                        p_in_buf->i_buffer );
88     if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
89     {
90         msg_Dbg( p_filter, "libmad error: %s",
91                   mad_stream_errorstr( &p_sys->mad_stream ) );
92         p_sys->i_reject_count = 3;
93     }
94     else if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
95     {
96         p_sys->i_reject_count = 3;
97     }
98
99     if( p_sys->i_reject_count > 0 )
100     {
101         if( p_filter->fmt_out.audio.i_format == VLC_CODEC_FL32 )
102         {
103             int i;
104             int i_size = p_out_buf->i_buffer / sizeof(float);
105
106             float * a = (float *)p_out_buf->p_buffer;
107             for ( i = 0 ; i < i_size ; i++ )
108                 *a++ = 0.0;
109         }
110         else
111         {
112             memset( p_out_buf->p_buffer, 0, p_out_buf->i_buffer );
113         }
114         p_sys->i_reject_count--;
115         return;
116     }
117
118
119     mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
120
121     if ( p_filter->fmt_out.audio.i_format == VLC_CODEC_FI32 )
122     {
123         /* Interleave and keep buffers in mad_fixed_t format */
124         mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;
125         struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
126         unsigned int i_samples = p_pcm->length;
127         mad_fixed_t const * p_left = p_pcm->samples[0];
128         mad_fixed_t const * p_right = p_pcm->samples[1];
129
130         switch ( p_pcm->channels )
131         {
132         case 2:
133             if ( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
134             {
135                 while ( i_samples-- )
136                 {
137                     *p_samples++ = (*p_left++ >> 1) + (*p_right++ >> 1);
138                 }
139             }
140             else if ( p_filter->fmt_out.audio.i_original_channels == AOUT_CHAN_LEFT )
141             {
142                 while ( i_samples-- )
143                 {
144                     *p_samples++ = *p_left;
145                     *p_samples++ = *p_left++;
146                 }
147             }
148             else if ( p_filter->fmt_out.audio.i_original_channels == AOUT_CHAN_RIGHT )
149             {
150                 while ( i_samples-- )
151                 {
152                     *p_samples++ = *p_right;
153                     *p_samples++ = *p_right++;
154                 }
155             }
156             else
157             {
158                 while ( i_samples-- )
159                 {
160                     *p_samples++ = *p_left++;
161                     *p_samples++ = *p_right++;
162                 }
163             }
164             break;
165
166         case 1:
167             vlc_memcpy( p_samples, p_left, i_samples * sizeof(mad_fixed_t) );
168             break;
169
170         default:
171             msg_Err( p_filter, "cannot interleave %i channels",
172                      p_pcm->channels );
173         }
174     }
175     else
176     {
177         /* float32 */
178         float * p_samples = (float *)p_out_buf->p_buffer;
179         struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
180         unsigned int i_samples = p_pcm->length;
181         mad_fixed_t const * p_left = p_pcm->samples[0];
182         mad_fixed_t const * p_right = p_pcm->samples[1];
183         float f_temp = (float)FIXED32_ONE;
184
185         switch ( p_pcm->channels )
186         {
187         case 2:
188             if ( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
189             {
190                 while ( i_samples-- )
191                 {
192                     *p_samples++ = (float)*p_left++ / f_temp / 2 +
193                                    (float)*p_right++ / f_temp / 2;
194                 }
195             }
196             else if ( p_filter->fmt_out.audio.i_original_channels == AOUT_CHAN_LEFT )
197             {
198                 while ( i_samples-- )
199                 {
200                     *p_samples++ = (float)*p_left / f_temp;
201                     *p_samples++ = (float)*p_left++ / f_temp;
202                 }
203             }
204             else if ( p_filter->fmt_out.audio.i_original_channels == AOUT_CHAN_RIGHT )
205             {
206                 while ( i_samples-- )
207                 {
208                     *p_samples++ = (float)*p_right / f_temp;
209                     *p_samples++ = (float)*p_right++ / f_temp;
210                 }
211             }
212             else
213             {
214                 while ( i_samples-- )
215                 {
216                     *p_samples++ = (float)*p_left++ / f_temp;
217                     *p_samples++ = (float)*p_right++ / f_temp;
218                 }
219             }
220             break;
221
222         case 1:
223             while ( i_samples-- )
224             {
225                 *p_samples++ = (float)*p_left++ / f_temp;
226             }
227             break;
228
229         default:
230             msg_Err( p_filter, "cannot interleave %i channels",
231                      p_pcm->channels );
232         }
233     }
234 }
235
236 /*****************************************************************************
237  * OpenFilter:
238  *****************************************************************************/
239 static int OpenFilter( vlc_object_t *p_this )
240 {
241     filter_t *p_filter = (filter_t *)p_this;
242     filter_sys_t *p_sys;
243
244     if( p_filter->fmt_in.i_codec != VLC_CODEC_MPGA &&
245         p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','3') )
246     {
247         return VLC_EGENERIC;
248     }
249
250     /* Allocate the memory needed to store the module's structure */
251     p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
252     if( p_sys == NULL )
253         return -1;
254     p_sys->i_reject_count = 0;
255
256     p_filter->pf_audio_filter = Convert;
257
258     /* Initialize libmad */
259     mad_stream_init( &p_sys->mad_stream );
260     mad_frame_init( &p_sys->mad_frame );
261     mad_synth_init( &p_sys->mad_synth );
262     mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
263
264     if( vlc_CPU() & CPU_CAPABILITY_FPU )
265         p_filter->fmt_out.i_codec = VLC_CODEC_FL32;
266     else
267         p_filter->fmt_out.i_codec = VLC_CODEC_FI32;
268     p_filter->fmt_out.audio.i_format = p_filter->fmt_out.i_codec;
269     p_filter->fmt_out.audio.i_bitspersample =
270         aout_BitsPerSample( p_filter->fmt_out.i_codec );
271
272     p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
273
274     msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
275              (char *)&p_filter->fmt_in.i_codec,
276              (char *)&p_filter->fmt_out.i_codec,
277              p_filter->fmt_in.audio.i_bitspersample );
278
279     return 0;
280 }
281
282 /*****************************************************************************
283  * CloseFilter : deallocate data structures
284  *****************************************************************************/
285 static void CloseFilter( vlc_object_t *p_this )
286 {
287     filter_t *p_filter = (filter_t *)p_this;
288     filter_sys_t *p_sys = p_filter->p_sys;
289
290     mad_synth_finish( &p_sys->mad_synth );
291     mad_frame_finish( &p_sys->mad_frame );
292     mad_stream_finish( &p_sys->mad_stream );
293     free( p_sys );
294 }
295
296 static block_t *Convert( filter_t *p_filter, block_t *p_block )
297 {
298     if( !p_block || !p_block->i_nb_samples )
299     {
300         if( p_block )
301             block_Release( p_block );
302         return NULL;
303     }
304
305     size_t i_out_size = p_block->i_nb_samples *
306       p_filter->fmt_out.audio.i_bitspersample *
307         p_filter->fmt_out.audio.i_channels / 8;
308
309     block_t *p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
310     if( !p_out )
311     {
312         msg_Warn( p_filter, "can't get output buffer" );
313         block_Release( p_block );
314         return NULL;
315     }
316
317     p_out->i_nb_samples = p_block->i_nb_samples;
318     p_out->i_dts = p_block->i_dts;
319     p_out->i_pts = p_block->i_pts;
320     p_out->i_length = p_block->i_length;
321
322     DoWork( p_filter, p_block, p_out );
323
324     block_Release( p_block );
325
326     return p_out;
327 }