]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/mpgatofixed32.c
mad: decode to FL32 (as in existing VLC releases)
[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     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
90     p_out_buf->i_buffer = p_in_buf->i_nb_samples * sizeof(float) *
91                                aout_FormatNbChannels( &p_filter->fmt_out.audio );
92
93     /* Do the actual decoding now. */
94     mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
95                        p_in_buf->i_buffer );
96     if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
97     {
98         msg_Dbg( p_filter, "libmad error: %s",
99                   mad_stream_errorstr( &p_sys->mad_stream ) );
100         p_sys->i_reject_count = 3;
101     }
102     else if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
103     {
104         p_sys->i_reject_count = 3;
105     }
106
107     if( p_sys->i_reject_count > 0 )
108     {
109         memset( p_out_buf->p_buffer, 0, p_out_buf->i_buffer );
110         p_sys->i_reject_count--;
111         return;
112     }
113
114
115     mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
116
117     struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
118     unsigned int i_samples = p_pcm->length;
119     mad_fixed_t const * p_left = p_pcm->samples[0];
120     mad_fixed_t const * p_right = p_pcm->samples[1];
121     float *p_samples = (float *)p_out_buf->p_buffer;
122
123     assert( i_samples == p_out_buf->i_nb_samples );
124     /* Interleave and keep buffers in mad_fixed_t format */
125     if ( p_pcm->channels == 2 )
126     {
127         while ( i_samples-- )
128         {
129             //assert( *p_left < MAD_F_ONE );
130             //assert( *p_left >= -MAD_F_ONE );
131             //assert( *p_right < MAD_F_ONE );
132             //assert( *p_right >= -MAD_F_ONE );
133             *p_samples++ = (float)*p_left++ / (float)MAD_F_ONE;
134             *p_samples++ = (float)*p_right++ / (float)MAD_F_ONE;
135         }
136     }
137     else
138     {
139         assert( p_pcm->channels == 1 );
140         while ( i_samples-- )
141         {
142             //assert( *p_left < MAD_F_ONE );
143             //assert( *p_left >= -MAD_F_ONE );
144             *p_samples++ = (float)*p_left++ / (float)MAD_F_ONE;
145         }
146     }
147 }
148
149 /*****************************************************************************
150  * OpenFilter:
151  *****************************************************************************/
152 static int OpenFilter( vlc_object_t *p_this )
153 {
154     filter_t *p_filter = (filter_t *)p_this;
155     filter_sys_t *p_sys;
156
157     if( p_filter->fmt_in.audio.i_format != VLC_CODEC_MPGA &&
158         p_filter->fmt_in.audio.i_format != VLC_FOURCC('m','p','g','3') )
159         return VLC_EGENERIC;
160
161     if( p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
162         return VLC_EGENERIC;
163
164     if( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
165         return VLC_EGENERIC;
166
167     /* Allocate the memory needed to store the module's structure */
168     p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
169     if( p_sys == NULL )
170         return -1;
171     p_sys->i_reject_count = 0;
172
173     p_filter->pf_audio_filter = Convert;
174
175     /* Initialize libmad */
176     mad_stream_init( &p_sys->mad_stream );
177     mad_frame_init( &p_sys->mad_frame );
178     mad_synth_init( &p_sys->mad_synth );
179     mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
180
181     msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
182              (char *)&p_filter->fmt_in.audio.i_format,
183              (char *)&p_filter->fmt_out.audio.i_format,
184              p_filter->fmt_out.audio.i_bitspersample );
185
186     return 0;
187 }
188
189 /*****************************************************************************
190  * CloseFilter : deallocate data structures
191  *****************************************************************************/
192 static void CloseFilter( vlc_object_t *p_this )
193 {
194     filter_t *p_filter = (filter_t *)p_this;
195     filter_sys_t *p_sys = p_filter->p_sys;
196
197     mad_synth_finish( &p_sys->mad_synth );
198     mad_frame_finish( &p_sys->mad_frame );
199     mad_stream_finish( &p_sys->mad_stream );
200     free( p_sys );
201 }
202
203 static block_t *Convert( filter_t *p_filter, block_t *p_block )
204 {
205     if( !p_block || !p_block->i_nb_samples )
206     {
207         if( p_block )
208             block_Release( p_block );
209         return NULL;
210     }
211
212     size_t i_out_size = p_block->i_nb_samples *
213       p_filter->fmt_out.audio.i_bitspersample *
214         p_filter->fmt_out.audio.i_channels / 8;
215
216     block_t *p_out = block_Alloc( i_out_size );
217     if( !p_out )
218     {
219         msg_Warn( p_filter, "can't get output buffer" );
220         block_Release( p_block );
221         return NULL;
222     }
223
224     p_out->i_nb_samples = p_block->i_nb_samples;
225     p_out->i_dts = p_block->i_dts;
226     p_out->i_pts = p_block->i_pts;
227     p_out->i_length = p_block->i_length;
228
229     DoWork( p_filter, p_block, p_out );
230
231     block_Release( p_block );
232
233     return p_out;
234 }