]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/mpgatofixed32.c
33a9a34ab774b14ddd109b4b72893d0a3c0543d2
[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 by Jean-Paul Saman
6  * $Id$
7  *
8  * Authors: Christophe Massiot <massiot@via.ecp.fr>
9  *          Jean-Paul Saman <jpsaman@wxs.nl>
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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/decoder.h>
36 #include "aout_internal.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_description( _("MPEG audio decoder") );
66     set_capability( "audio filter", 100 );
67     set_callbacks( Create, Destroy );
68
69     add_submodule();
70     set_description( _("MPEG audio decoder") );
71     set_capability( "audio filter2", 100 );
72     set_callbacks( OpenFilter, CloseFilter );
73 vlc_module_end();
74
75 /*****************************************************************************
76  * Create: 
77  *****************************************************************************/
78 static int Create( vlc_object_t *p_this )
79 {
80     aout_filter_t *p_filter = (aout_filter_t *)p_this;
81     struct filter_sys_t *p_sys;
82
83     if ( (p_filter->input.i_format != VLC_FOURCC('m','p','g','a')
84            && p_filter->input.i_format != VLC_FOURCC('m','p','g','3'))
85             || (p_filter->output.i_format != VLC_FOURCC('f','l','3','2')
86                  && p_filter->output.i_format != VLC_FOURCC('f','i','3','2')) )
87     {
88         return -1;
89     }
90
91     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
92     {
93         return -1;
94     }
95
96     /* Allocate the memory needed to store the module's structure */
97     p_sys = malloc( sizeof(filter_sys_t) );
98     p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
99     if( p_sys == NULL )
100     {
101         msg_Err( p_filter, "out of memory" );
102         return -1;
103     }
104
105     /* Initialize libmad */
106     mad_stream_init( &p_sys->mad_stream );
107     mad_frame_init( &p_sys->mad_frame );
108     mad_synth_init( &p_sys->mad_synth );
109     mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
110
111     p_filter->pf_do_work = DoWork;
112     p_filter->b_in_place = 0;
113
114     return 0;
115 }
116
117 /*****************************************************************************
118  * DoWork: decode an MPEG audio frame.
119  *****************************************************************************/
120 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
121                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
122 {
123     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
124
125     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
126     p_out_buf->i_nb_bytes = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t) * 
127                                aout_FormatNbChannels( &p_filter->input );
128
129     /* Do the actual decoding now. */
130     mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
131                        p_in_buf->i_nb_bytes );
132     if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
133     {
134         msg_Dbg( p_aout, "libmad error: %s",
135                   mad_stream_errorstr( &p_sys->mad_stream ) );
136         if( p_filter->output.i_format == VLC_FOURCC('f','l','3','2') )
137         {
138             int i;
139             int i_size = p_out_buf->i_nb_bytes / sizeof(float);
140             
141             float * a = (float *)p_out_buf->p_buffer;
142             for ( i = 0 ; i < i_size ; i++ )
143                 *a++ = 0.0;
144         }
145         else
146         {
147             memset( p_out_buf->p_buffer, 0, p_out_buf->i_nb_bytes );
148         }
149         return;
150     }
151
152     mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
153
154     if ( p_filter->output.i_format == VLC_FOURCC('f','i','3','2') )
155     {
156         /* Interleave and keep buffers in mad_fixed_t format */
157         mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;
158         struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
159         unsigned int i_samples = p_pcm->length;
160         mad_fixed_t const * p_left = p_pcm->samples[0];
161         mad_fixed_t const * p_right = p_pcm->samples[1];
162
163         switch ( p_pcm->channels )
164         {
165         case 2:
166             while ( i_samples-- )
167             {
168                 *p_samples++ = *p_left++;
169                 *p_samples++ = *p_right++;
170             }
171             break;
172
173         case 1:
174             p_filter->p_vlc->pf_memcpy( p_samples, p_left,
175                                         i_samples * sizeof(mad_fixed_t) );
176             break;
177
178         default:
179             msg_Err( p_filter, "cannot interleave %i channels",
180                      p_pcm->channels );
181         }
182     }
183     else
184     {
185         /* float32 */
186         float * p_samples = (float *)p_out_buf->p_buffer;
187         struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
188         unsigned int i_samples = p_pcm->length;
189         mad_fixed_t const * p_left = p_pcm->samples[0];
190         mad_fixed_t const * p_right = p_pcm->samples[1];
191         float f_temp = (float)FIXED32_ONE;
192         
193         switch ( p_pcm->channels )
194         {
195         case 2:
196             while ( i_samples-- )
197             {
198                 *p_samples++ = (float)*p_left++ / f_temp;
199                 *p_samples++ = (float)*p_right++ / f_temp;
200             }
201             break;
202
203         case 1:
204             while ( i_samples-- )
205             {
206                 *p_samples++ = (float)*p_left++ / f_temp;
207             }
208             break;
209
210         default:
211             msg_Err( p_filter, "cannot interleave %i channels",
212                      p_pcm->channels );
213         }
214     }
215 }
216
217 /*****************************************************************************
218  * Destroy : deallocate data structures
219  *****************************************************************************/
220 static void Destroy( vlc_object_t *p_this )
221 {
222     aout_filter_t *p_filter = (aout_filter_t *)p_this;
223     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
224
225     mad_synth_finish( &p_sys->mad_synth );
226     mad_frame_finish( &p_sys->mad_frame );
227     mad_stream_finish( &p_sys->mad_stream );
228     free( p_sys );
229 }
230
231 /*****************************************************************************
232  * OpenFilter: 
233  *****************************************************************************/
234 static int OpenFilter( vlc_object_t *p_this )
235 {
236     filter_t *p_filter = (filter_t *)p_this;
237     filter_sys_t *p_sys;
238
239     if( p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') &&
240         p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','3') )
241     {
242         return VLC_EGENERIC;
243     }
244
245     /* Allocate the memory needed to store the module's structure */
246     p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
247     if( p_sys == NULL )
248     {
249         msg_Err( p_filter, "out of memory" );
250         return -1;
251     }
252
253     p_filter->pf_audio_filter = Convert;
254
255     /* Initialize libmad */
256     mad_stream_init( &p_sys->mad_stream );
257     mad_frame_init( &p_sys->mad_frame );
258     mad_synth_init( &p_sys->mad_synth );
259     mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
260
261     msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
262              (char *)&p_filter->fmt_in.i_codec,
263              (char *)&p_filter->fmt_out.i_codec,
264              p_filter->fmt_out.audio.i_bitspersample );
265
266     p_filter->fmt_out.i_codec =
267         p_filter->fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
268     p_filter->fmt_out.audio.i_bitspersample = sizeof(float);
269
270     return 0;
271 }
272
273 /*****************************************************************************
274  * CloseFilter : deallocate data structures
275  *****************************************************************************/
276 static void CloseFilter( vlc_object_t *p_this )
277 {
278     filter_t *p_filter = (filter_t *)p_this;
279     filter_sys_t *p_sys = p_filter->p_sys;
280
281     mad_synth_finish( &p_sys->mad_synth );
282     mad_frame_finish( &p_sys->mad_frame );
283     mad_stream_finish( &p_sys->mad_stream );
284     free( p_sys );
285 }
286
287 static block_t *Convert( filter_t *p_filter, block_t *p_block )
288 {
289     aout_filter_t aout_filter;
290     aout_buffer_t in_buf, out_buf;
291     block_t *p_out;
292     int i_out_size;
293
294     if( !p_block || !p_block->i_samples )
295     {
296         if( p_block ) p_block->pf_release( p_block );
297         return NULL;
298     }
299
300     i_out_size = p_block->i_samples *
301       p_filter->fmt_out.audio.i_bitspersample *
302         p_filter->fmt_out.audio.i_channels / 8;
303
304     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
305     if( !p_out )
306     {
307         msg_Warn( p_filter, "can't get output buffer" );
308         p_block->pf_release( p_block );
309         return NULL;
310     }
311
312     p_out->i_samples = p_block->i_samples;
313     p_out->i_dts = p_block->i_dts;
314     p_out->i_pts = p_block->i_pts;
315     p_out->i_length = p_block->i_length;
316
317     aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
318     aout_filter.input = p_filter->fmt_in.audio;
319     aout_filter.input.i_format = p_filter->fmt_in.i_codec;
320     aout_filter.output = p_filter->fmt_out.audio;
321     aout_filter.output.i_format = p_filter->fmt_out.i_codec;
322
323     in_buf.p_buffer = p_block->p_buffer;
324     in_buf.i_nb_bytes = p_block->i_buffer;
325     in_buf.i_nb_samples = p_block->i_samples;
326     out_buf.p_buffer = p_out->p_buffer;
327     out_buf.i_nb_bytes = p_out->i_buffer;
328     out_buf.i_nb_samples = p_out->i_samples;
329
330     DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
331
332     p_block->pf_release( p_block );
333
334     p_out->i_buffer = out_buf.i_nb_bytes;
335     p_out->i_samples = out_buf.i_nb_samples;
336
337     return p_out;
338 }