]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/mpgatofixed32.c
Merge branch 'master' into lpcm_encoder
[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 #include <assert.h>
36
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_aout.h>
40 #include <vlc_block.h>
41 #include <vlc_filter.h>
42 #include <vlc_cpu.h>
43
44 /*****************************************************************************
45  * Local prototypes
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     int               i_reject_count;
61 };
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 vlc_module_begin ()
67     set_category( CAT_INPUT )
68     set_subcategory( SUBCAT_INPUT_ACODEC )
69     set_description( N_("MPEG audio decoder") )
70     set_capability( "audio filter", 100 )
71     set_callbacks( OpenFilter, CloseFilter )
72 vlc_module_end ()
73
74 /*****************************************************************************
75  * DoWork: decode an MPEG audio frame.
76  *****************************************************************************/
77 static void DoWork( filter_t * p_filter,
78                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
79 {
80     filter_sys_t *p_sys = p_filter->p_sys;
81
82     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
83     p_out_buf->i_buffer = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t) *
84                                aout_FormatNbChannels( &p_filter->fmt_out.audio );
85
86     /* Do the actual decoding now. */
87     mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
88                        p_in_buf->i_buffer );
89     if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
90     {
91         msg_Dbg( p_filter, "libmad error: %s",
92                   mad_stream_errorstr( &p_sys->mad_stream ) );
93         p_sys->i_reject_count = 3;
94     }
95     else if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
96     {
97         p_sys->i_reject_count = 3;
98     }
99
100     if( p_sys->i_reject_count > 0 )
101     {
102         if( p_filter->fmt_out.audio.i_format == VLC_CODEC_FL32 )
103         {
104             int i;
105             int i_size = p_out_buf->i_buffer / sizeof(float);
106
107             float * a = (float *)p_out_buf->p_buffer;
108             for ( i = 0 ; i < i_size ; i++ )
109                 *a++ = 0.0;
110         }
111         else
112         {
113             memset( p_out_buf->p_buffer, 0, p_out_buf->i_buffer );
114         }
115         p_sys->i_reject_count--;
116         return;
117     }
118
119
120     mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
121
122     struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
123     unsigned int i_samples = p_pcm->length;
124     mad_fixed_t const * p_left = p_pcm->samples[0];
125     mad_fixed_t const * p_right = p_pcm->samples[1];
126
127     assert( i_samples == p_out_buf->i_nb_samples );
128     if ( p_filter->fmt_out.audio.i_format == VLC_CODEC_FI32 )
129     {
130         /* Interleave and keep buffers in mad_fixed_t format */
131         mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;
132
133         if ( p_pcm->channels == 2 )
134         {
135             while ( i_samples-- )
136             {
137                 *p_samples++ = *p_left++;
138                 *p_samples++ = *p_right++;
139             }
140         }
141         else
142         {
143             assert( p_pcm->channels == 1 );
144             vlc_memcpy( p_samples, p_left, i_samples * sizeof(mad_fixed_t) );
145         }
146     }
147     else
148     {
149         /* float32 */
150         float * p_samples = (float *)p_out_buf->p_buffer;
151         const float f_temp = (float)FIXED32_ONE;
152
153         if ( p_pcm->channels == 2 )
154         {
155             while ( i_samples-- )
156             {
157                 *p_samples++ = (float)*p_left++ / f_temp;
158                 *p_samples++ = (float)*p_right++ / f_temp;
159             }
160         }
161         else
162         {
163             assert( p_pcm->channels == 1 );
164             while ( i_samples-- )
165             {
166                 *p_samples++ = (float)*p_left++ / f_temp;
167             }
168         }
169     }
170 }
171
172 /*****************************************************************************
173  * OpenFilter:
174  *****************************************************************************/
175 static int OpenFilter( vlc_object_t *p_this )
176 {
177     filter_t *p_filter = (filter_t *)p_this;
178     filter_sys_t *p_sys;
179
180     if( p_filter->fmt_in.i_codec != VLC_CODEC_MPGA &&
181         p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','3') )
182         return VLC_EGENERIC;
183     if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
184         return VLC_EGENERIC;
185
186     /* Allocate the memory needed to store the module's structure */
187     p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
188     if( p_sys == NULL )
189         return -1;
190     p_sys->i_reject_count = 0;
191
192     p_filter->pf_audio_filter = Convert;
193
194     /* Initialize libmad */
195     mad_stream_init( &p_sys->mad_stream );
196     mad_frame_init( &p_sys->mad_frame );
197     mad_synth_init( &p_sys->mad_synth );
198     mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
199
200     p_filter->fmt_out.i_codec = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_FI32;
201     p_filter->fmt_out.audio.i_format = p_filter->fmt_out.i_codec;
202     p_filter->fmt_out.audio.i_bitspersample =
203         aout_BitsPerSample( p_filter->fmt_out.i_codec );
204
205     msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
206              (char *)&p_filter->fmt_in.i_codec,
207              (char *)&p_filter->fmt_out.i_codec,
208              p_filter->fmt_in.audio.i_bitspersample );
209
210     return 0;
211 }
212
213 /*****************************************************************************
214  * CloseFilter : deallocate data structures
215  *****************************************************************************/
216 static void CloseFilter( vlc_object_t *p_this )
217 {
218     filter_t *p_filter = (filter_t *)p_this;
219     filter_sys_t *p_sys = p_filter->p_sys;
220
221     mad_synth_finish( &p_sys->mad_synth );
222     mad_frame_finish( &p_sys->mad_frame );
223     mad_stream_finish( &p_sys->mad_stream );
224     free( p_sys );
225 }
226
227 static block_t *Convert( filter_t *p_filter, block_t *p_block )
228 {
229     if( !p_block || !p_block->i_nb_samples )
230     {
231         if( p_block )
232             block_Release( p_block );
233         return NULL;
234     }
235
236     size_t i_out_size = p_block->i_nb_samples *
237       p_filter->fmt_out.audio.i_bitspersample *
238         p_filter->fmt_out.audio.i_channels / 8;
239
240     block_t *p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
241     if( !p_out )
242     {
243         msg_Warn( p_filter, "can't get output buffer" );
244         block_Release( p_block );
245         return NULL;
246     }
247
248     p_out->i_nb_samples = p_block->i_nb_samples;
249     p_out->i_dts = p_block->i_dts;
250     p_out->i_pts = p_block->i_pts;
251     p_out->i_length = p_block->i_length;
252
253     DoWork( p_filter, p_block, p_out );
254
255     block_Release( p_block );
256
257     return p_out;
258 }