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