]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/mpgatofixed32.c
macosx: remove dead code, small reformatting
[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 VLC authors and VideoLAN
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 it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * NOTA BENE: this module requires the linking against a library which is
28  * known to require licensing under the GNU General Public License version 2
29  * (or later). Therefore, the result of compiling this module will normally
30  * be subject to the terms of that later license.
31  *****************************************************************************/
32
33
34 /*****************************************************************************
35  * Preamble
36  *****************************************************************************/
37
38 #include <mad.h>
39
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43 #include <assert.h>
44
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47 #include <vlc_aout.h>
48 #include <vlc_block.h>
49 #include <vlc_filter.h>
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int  OpenFilter ( vlc_object_t * );
55 static void CloseFilter( vlc_object_t * );
56 static block_t *Convert( filter_t *, block_t * );
57
58 /*****************************************************************************
59  * Local structures
60  *****************************************************************************/
61 struct filter_sys_t
62 {
63     struct mad_stream mad_stream;
64     struct mad_frame  mad_frame;
65     struct mad_synth  mad_synth;
66
67     int               i_reject_count;
68 };
69
70 /*****************************************************************************
71  * Module descriptor
72  *****************************************************************************/
73 vlc_module_begin ()
74     set_category( CAT_INPUT )
75     set_subcategory( SUBCAT_INPUT_ACODEC )
76     set_description( N_("MPEG audio decoder") )
77     set_capability( "audio converter", 100 )
78     set_callbacks( OpenFilter, CloseFilter )
79 vlc_module_end ()
80
81 /*****************************************************************************
82  * DoWork: decode an MPEG audio frame.
83  *****************************************************************************/
84 static void DoWork( filter_t * p_filter,
85                     block_t * p_in_buf, block_t * p_out_buf )
86 {
87     filter_sys_t *p_sys = p_filter->p_sys;
88
89     /* Do the actual decoding now. */
90     mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
91                        p_in_buf->i_buffer );
92     if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
93     {
94         msg_Err( p_filter, "libmad error: %s",
95                   mad_stream_errorstr( &p_sys->mad_stream ) );
96         if( !MAD_RECOVERABLE( p_sys->mad_stream.error ) )
97             p_sys->i_reject_count = 3;
98     }
99     else if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
100     {
101         p_sys->i_reject_count = 3;
102     }
103
104     if( p_sys->i_reject_count > 0 )
105     {
106 reject:
107         memset( p_out_buf->p_buffer, 0, p_out_buf->i_buffer );
108         p_sys->i_reject_count--;
109         return;
110     }
111
112
113     mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
114
115     struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
116     unsigned int i_samples = p_pcm->length;
117     mad_fixed_t const * p_left = p_pcm->samples[0];
118     mad_fixed_t const * p_right = p_pcm->samples[1];
119     float *p_samples = (float *)p_out_buf->p_buffer;
120
121     if (p_pcm->channels > p_filter->fmt_out.audio.i_channels)
122     {
123         msg_Err( p_filter, "wrong channels count (corrupt stream?): %u > %u",
124                  p_pcm->channels, p_filter->fmt_out.audio.i_channels);
125         p_sys->i_reject_count = 3;
126         goto reject;
127     }
128
129     if( i_samples != p_out_buf->i_nb_samples )
130     {
131         msg_Err( p_filter, "unexpected samples count (corrupt stream?): "
132                  "%u / %u", i_samples, p_out_buf->i_nb_samples );
133         p_sys->i_reject_count = 3;
134         goto reject;
135     }
136
137     /* Interleave and keep buffers in mad_fixed_t format */
138     if ( p_pcm->channels == 2 )
139     {
140         while ( i_samples-- )
141         {
142             //assert( *p_left < MAD_F_ONE );
143             //assert( *p_left >= -MAD_F_ONE );
144             //assert( *p_right < MAD_F_ONE );
145             //assert( *p_right >= -MAD_F_ONE );
146             *p_samples++ = (float)*p_left++ / (float)MAD_F_ONE;
147             *p_samples++ = (float)*p_right++ / (float)MAD_F_ONE;
148         }
149     }
150     else
151     {
152         assert( p_pcm->channels == 1 );
153         while ( i_samples-- )
154         {
155             //assert( *p_left < MAD_F_ONE );
156             //assert( *p_left >= -MAD_F_ONE );
157             *p_samples++ = (float)*p_left++ / (float)MAD_F_ONE;
158         }
159     }
160 }
161
162 /*****************************************************************************
163  * OpenFilter:
164  *****************************************************************************/
165 static int OpenFilter( vlc_object_t *p_this )
166 {
167     filter_t *p_filter = (filter_t *)p_this;
168     filter_sys_t *p_sys;
169
170     if( p_filter->fmt_in.audio.i_format != VLC_CODEC_MPGA &&
171         p_filter->fmt_in.audio.i_format != VLC_CODEC_MP3 &&
172         p_filter->fmt_in.audio.i_format != VLC_FOURCC('m','p','g','3') )
173         return VLC_EGENERIC;
174
175     if( p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
176         return VLC_EGENERIC;
177
178     if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
179         return VLC_EGENERIC;
180
181     /* Allocate the memory needed to store the module's structure */
182     p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
183     if( p_sys == NULL )
184         return VLC_ENOMEM;
185     p_sys->i_reject_count = 0;
186
187
188     /* Initialize libmad */
189     mad_stream_init( &p_sys->mad_stream );
190     mad_frame_init( &p_sys->mad_frame );
191     mad_synth_init( &p_sys->mad_synth );
192     mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
193
194     msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
195              (char *)&p_filter->fmt_in.audio.i_format,
196              (char *)&p_filter->fmt_out.audio.i_format,
197              p_filter->fmt_out.audio.i_bitspersample );
198
199     p_filter->pf_audio_filter = Convert;
200
201     return VLC_SUCCESS;
202 }
203
204 /*****************************************************************************
205  * CloseFilter : deallocate data structures
206  *****************************************************************************/
207 static void CloseFilter( vlc_object_t *p_this )
208 {
209     filter_t *p_filter = (filter_t *)p_this;
210     filter_sys_t *p_sys = p_filter->p_sys;
211
212     mad_synth_finish( &p_sys->mad_synth );
213     mad_frame_finish( &p_sys->mad_frame );
214     mad_stream_finish( &p_sys->mad_stream );
215     free( p_sys );
216 }
217
218 static block_t *Convert( filter_t *p_filter, block_t *p_block )
219 {
220     if( !p_block || !p_block->i_nb_samples )
221     {
222         if( p_block )
223             block_Release( p_block );
224         return NULL;
225     }
226
227     size_t i_out_size = p_block->i_nb_samples *
228       p_filter->fmt_out.audio.i_bitspersample *
229         p_filter->fmt_out.audio.i_channels / 8;
230
231     block_t *p_out = block_Alloc( i_out_size );
232     if( unlikely( !p_out ) )
233     {
234         msg_Warn( p_filter, "can't get output buffer" );
235         block_Release( p_block );
236         return NULL;
237     }
238
239     p_out->i_nb_samples = p_block->i_nb_samples;
240     p_out->i_dts = p_block->i_dts;
241     p_out->i_pts = p_block->i_pts;
242     p_out->i_length = p_block->i_length;
243
244     DoWork( p_filter, p_block, p_out );
245
246     block_Release( p_block );
247
248     return p_out;
249 }