]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/mpgatofixed32.c
Remove stdlib.h
[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 #include <string.h>                                              /* strdup() */
30
31 #include <mad.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc_aout.h>
35 #include <vlc_block.h>
36 #include "vlc_filter.h"
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int  Create    ( vlc_object_t * );
42 static void Destroy   ( vlc_object_t * );
43 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,  
44                         aout_buffer_t * );
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( _("MPEG audio decoder") );
69     set_capability( "audio filter", 100 );
70     set_callbacks( Create, Destroy );
71
72     add_submodule();
73     set_description( _("MPEG audio decoder") );
74     set_capability( "audio filter2", 100 );
75     set_callbacks( OpenFilter, CloseFilter );
76 vlc_module_end();
77
78 /*****************************************************************************
79  * Create: 
80  *****************************************************************************/
81 static int Create( vlc_object_t *p_this )
82 {
83     aout_filter_t *p_filter = (aout_filter_t *)p_this;
84     struct filter_sys_t *p_sys;
85
86     if ( (p_filter->input.i_format != VLC_FOURCC('m','p','g','a')
87            && p_filter->input.i_format != VLC_FOURCC('m','p','g','3'))
88             || (p_filter->output.i_format != VLC_FOURCC('f','l','3','2')
89                  && p_filter->output.i_format != VLC_FOURCC('f','i','3','2')) )
90     {
91         return -1;
92     }
93
94     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
95     {
96         return -1;
97     }
98
99     /* Allocate the memory needed to store the module's structure */
100     p_sys = malloc( sizeof(filter_sys_t) );
101     p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
102     if( p_sys == NULL )
103     {
104         msg_Err( p_filter, "out of memory" );
105         return -1;
106     }
107
108     /* Initialize libmad */
109     mad_stream_init( &p_sys->mad_stream );
110     mad_frame_init( &p_sys->mad_frame );
111     mad_synth_init( &p_sys->mad_synth );
112     mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
113     p_sys->i_reject_count = 0;
114
115     p_filter->pf_do_work = DoWork;
116     p_filter->b_in_place = 0;
117
118     return 0;
119 }
120
121 /*****************************************************************************
122  * DoWork: decode an MPEG audio frame.
123  *****************************************************************************/
124 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
125                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
126 {
127     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
128
129     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
130     p_out_buf->i_nb_bytes = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t) * 
131                                aout_FormatNbChannels( &p_filter->output );
132
133     /* Do the actual decoding now. */
134     mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
135                        p_in_buf->i_nb_bytes );
136     if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
137     {
138         msg_Dbg( p_aout, "libmad error: %s",
139                   mad_stream_errorstr( &p_sys->mad_stream ) );
140         p_sys->i_reject_count = 3;
141     }
142     else if( p_in_buf->b_discontinuity )
143     {
144         p_sys->i_reject_count = 3;
145     }
146
147     if( p_sys->i_reject_count > 0 )
148     {
149         if( p_filter->output.i_format == VLC_FOURCC('f','l','3','2') )
150         {
151             int i;
152             int i_size = p_out_buf->i_nb_bytes / sizeof(float);
153
154             float * a = (float *)p_out_buf->p_buffer;
155             for ( i = 0 ; i < i_size ; i++ )
156                 *a++ = 0.0;
157         }
158         else
159         {
160             memset( p_out_buf->p_buffer, 0, p_out_buf->i_nb_bytes );
161         }
162         p_sys->i_reject_count--;
163         return;
164     }
165
166
167     mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
168
169     if ( p_filter->output.i_format == VLC_FOURCC('f','i','3','2') )
170     {
171         /* Interleave and keep buffers in mad_fixed_t format */
172         mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;
173         struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
174         unsigned int i_samples = p_pcm->length;
175         mad_fixed_t const * p_left = p_pcm->samples[0];
176         mad_fixed_t const * p_right = p_pcm->samples[1];
177
178         switch ( p_pcm->channels )
179         {
180         case 2:
181             if ( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
182             {
183                 while ( i_samples-- )
184                 {
185                     *p_samples++ = (*p_left++ >> 1) + (*p_right++ >> 1);
186                 }
187             }
188             else if ( p_filter->output.i_original_channels == AOUT_CHAN_LEFT )
189             {
190                 while ( i_samples-- )
191                 {
192                     *p_samples++ = *p_left;
193                     *p_samples++ = *p_left++;
194                 }
195             }
196             else if ( p_filter->output.i_original_channels == AOUT_CHAN_RIGHT )
197             {
198                 while ( i_samples-- )
199                 {
200                     *p_samples++ = *p_right;
201                     *p_samples++ = *p_right++;
202                 }
203             }
204             else
205             {
206                 while ( i_samples-- )
207                 {
208                     *p_samples++ = *p_left++;
209                     *p_samples++ = *p_right++;
210                 }
211             }
212             break;
213
214         case 1:
215             p_filter->p_libvlc->pf_memcpy( p_samples, p_left,
216                                         i_samples * sizeof(mad_fixed_t) );
217             break;
218
219         default:
220             msg_Err( p_aout, "cannot interleave %i channels",
221                      p_pcm->channels );
222         }
223     }
224     else
225     {
226         /* float32 */
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;
233
234         switch ( p_pcm->channels )
235         {
236         case 2:
237             if ( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
238             {
239                 while ( i_samples-- )
240                 {
241                     *p_samples++ = (float)*p_left++ / f_temp / 2 +
242                                    (float)*p_right++ / f_temp / 2;
243                 }
244             }
245             else if ( p_filter->output.i_original_channels == AOUT_CHAN_LEFT )
246             {
247                 while ( i_samples-- )
248                 {
249                     *p_samples++ = (float)*p_left / f_temp;
250                     *p_samples++ = (float)*p_left++ / f_temp;
251                 }
252             }
253             else if ( p_filter->output.i_original_channels == AOUT_CHAN_RIGHT )
254             {
255                 while ( i_samples-- )
256                 {
257                     *p_samples++ = (float)*p_right / f_temp;
258                     *p_samples++ = (float)*p_right++ / f_temp;
259                 }
260             }
261             else
262             {
263                 while ( i_samples-- )
264                 {
265                     *p_samples++ = (float)*p_left++ / f_temp;
266                     *p_samples++ = (float)*p_right++ / f_temp;
267                 }
268             }
269             break;
270
271         case 1:
272             while ( i_samples-- )
273             {
274                 *p_samples++ = (float)*p_left++ / f_temp;
275             }
276             break;
277
278         default:
279             msg_Err( p_aout, "cannot interleave %i channels",
280                      p_pcm->channels );
281         }
282     }
283 }
284
285 /*****************************************************************************
286  * Destroy : deallocate data structures
287  *****************************************************************************/
288 static void Destroy( vlc_object_t *p_this )
289 {
290     aout_filter_t *p_filter = (aout_filter_t *)p_this;
291     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
292
293     mad_synth_finish( &p_sys->mad_synth );
294     mad_frame_finish( &p_sys->mad_frame );
295     mad_stream_finish( &p_sys->mad_stream );
296     free( p_sys );
297 }
298
299 /*****************************************************************************
300  * OpenFilter: 
301  *****************************************************************************/
302 static int OpenFilter( vlc_object_t *p_this )
303 {
304     filter_t *p_filter = (filter_t *)p_this;
305     filter_sys_t *p_sys;
306
307     if( p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') &&
308         p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','3') )
309     {
310         return VLC_EGENERIC;
311     }
312
313     /* Allocate the memory needed to store the module's structure */
314     p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
315     if( p_sys == NULL )
316     {
317         msg_Err( p_filter, "out of memory" );
318         return -1;
319     }
320     p_sys->i_reject_count = 0;
321
322     p_filter->pf_audio_filter = Convert;
323
324     /* Initialize libmad */
325     mad_stream_init( &p_sys->mad_stream );
326     mad_frame_init( &p_sys->mad_frame );
327     mad_synth_init( &p_sys->mad_synth );
328     mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
329
330     if( vlc_CPU() & CPU_CAPABILITY_FPU )
331         p_filter->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
332     else
333         p_filter->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
334     p_filter->fmt_out.audio.i_format = p_filter->fmt_out.i_codec;
335
336     p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
337
338     msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
339              (char *)&p_filter->fmt_in.i_codec,
340              (char *)&p_filter->fmt_out.i_codec,
341              p_filter->fmt_in.audio.i_bitspersample );
342
343     return 0;
344 }
345
346 /*****************************************************************************
347  * CloseFilter : deallocate data structures
348  *****************************************************************************/
349 static void CloseFilter( vlc_object_t *p_this )
350 {
351     filter_t *p_filter = (filter_t *)p_this;
352     filter_sys_t *p_sys = p_filter->p_sys;
353
354     mad_synth_finish( &p_sys->mad_synth );
355     mad_frame_finish( &p_sys->mad_frame );
356     mad_stream_finish( &p_sys->mad_stream );
357     free( p_sys );
358 }
359
360 static block_t *Convert( filter_t *p_filter, block_t *p_block )
361 {
362     aout_filter_t aout_filter;
363     aout_buffer_t in_buf, out_buf;
364     block_t *p_out;
365     int i_out_size;
366
367     if( !p_block || !p_block->i_samples )
368     {
369         if( p_block ) p_block->pf_release( p_block );
370         return NULL;
371     }
372
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;
376
377     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
378     if( !p_out )
379     {
380         msg_Warn( p_filter, "can't get output buffer" );
381         p_block->pf_release( p_block );
382         return NULL;
383     }
384
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;
389
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;
395
396     in_buf.p_buffer = p_block->p_buffer;
397     in_buf.b_discontinuity = VLC_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;
403
404     DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
405
406     p_block->pf_release( p_block );
407
408     p_out->i_buffer = out_buf.i_nb_bytes;
409     p_out->i_samples = out_buf.i_nb_samples;
410
411     return p_out;
412 }