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