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