]> git.sesse.net Git - vlc/blob - modules/audio_filter/converter/mpgatofixed32.c
* (float) 0 the buffer if it cannot be decoded and fixed32.
[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: mpgatofixed32.c,v 1.5 2003/02/21 14:17:46 hartman Exp $
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 "audio_output.h"
36 #include "aout_internal.h"
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int  Create    ( vlc_object_t * );
42 static void Destroy   ( vlc_object_t * );
43 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,  
44                         aout_buffer_t * );
45
46 /*****************************************************************************
47  * Local structures
48  *****************************************************************************/
49 struct aout_filter_sys_t
50 {
51     struct mad_stream mad_stream;
52     struct mad_frame mad_frame;
53     struct mad_synth mad_synth;
54 };
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 vlc_module_begin();
60     add_category_hint( N_("Miscellaneous"), NULL, VLC_TRUE );
61     set_description( _("MPEG audio decoder module") );
62     set_capability( "audio filter", 100 );
63     set_callbacks( Create, Destroy );
64 vlc_module_end();
65
66 /*****************************************************************************
67  * Create: 
68  *****************************************************************************/
69 static int Create( vlc_object_t * _p_filter )
70 {
71     aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
72     struct aout_filter_sys_t * p_sys;
73
74     if ( (p_filter->input.i_format != VLC_FOURCC('m','p','g','a')
75            && p_filter->input.i_format != VLC_FOURCC('m','p','g','3'))
76             || (p_filter->output.i_format != VLC_FOURCC('f','l','3','2')
77                  && p_filter->output.i_format != VLC_FOURCC('f','i','3','2')) )
78     {
79         return -1;
80     }
81
82     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
83     {
84         return -1;
85     }
86
87     /* Allocate the memory needed to store the module's structure */
88     p_sys = p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
89     if( p_sys == NULL )
90     {
91         msg_Err( p_filter, "out of memory" );
92         return -1;
93     }
94
95     /* Initialize libmad */
96     mad_stream_init( &p_sys->mad_stream );
97     mad_frame_init( &p_sys->mad_frame );
98     mad_synth_init( &p_sys->mad_synth );
99     mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
100
101     p_filter->pf_do_work = DoWork;
102     p_filter->b_in_place = 0;
103
104     return 0;
105 }
106
107 /*****************************************************************************
108  * DoWork: decode an MPEG audio frame.
109  *****************************************************************************/
110 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
111                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
112 {
113     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
114
115
116     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
117     p_out_buf->i_nb_bytes = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t);
118
119     /* Do the actual decoding now. */
120     mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
121                        p_in_buf->i_nb_bytes );
122     if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
123     {
124         msg_Warn( p_aout, "libmad error: %s",
125                   mad_stream_errorstr( &p_sys->mad_stream ) );
126         if( p_filter->output.i_format == VLC_FOURCC('f','l','3','2') )
127         {
128             int i;
129             float * a = (float *)p_out_buf->p_buffer;
130             for ( i = 0 ; i < p_out_buf->i_nb_samples ; i++ )
131                 *a++ = 0.0;
132             return;
133         }
134         else
135         {
136             memset( p_out_buf->p_buffer, 0, p_out_buf->i_nb_bytes );
137         } 
138     }
139     mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
140
141     if ( p_filter->output.i_format == VLC_FOURCC('f','i','3','2') )
142     {
143         /* Interleave and keep buffers in mad_fixed_t format */
144         mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;
145         struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
146         unsigned int i_samples = p_pcm->length;
147         mad_fixed_t const * p_left = p_pcm->samples[0];
148         mad_fixed_t const * p_right = p_pcm->samples[1];
149
150         switch ( p_pcm->channels )
151         {
152         case 2:
153             while ( i_samples-- )
154             {
155                 *p_samples++ = *p_left++;
156                 *p_samples++ = *p_right++;
157             }
158             break;
159
160         case 1:
161             p_filter->p_vlc->pf_memcpy( p_samples, p_left,
162                                         i_samples * sizeof(mad_fixed_t) );
163             break;
164
165         default:
166             msg_Err( p_filter, "cannot interleave %i channels",
167                      p_pcm->channels );
168         }
169     }
170     else
171     {
172         /* float32 */
173         float * p_samples = (float *)p_out_buf->p_buffer;
174         struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
175         unsigned int i_samples = p_pcm->length;
176         mad_fixed_t const * p_left = p_pcm->samples[0];
177         mad_fixed_t const * p_right = p_pcm->samples[1];
178
179         switch ( p_pcm->channels )
180         {
181         case 2:
182             while ( i_samples-- )
183             {
184                 *p_samples++ = (float)*p_left++ / (float)FIXED32_ONE;
185                 *p_samples++ = (float)*p_right++ / (float)FIXED32_ONE;
186             }
187             break;
188
189         case 1:
190             while ( i_samples-- )
191             {
192                 *p_samples++ = (float)*p_left++ / (float)FIXED32_ONE;
193             }
194             break;
195
196         default:
197             msg_Err( p_filter, "cannot interleave %i channels",
198                      p_pcm->channels );
199         }
200     }
201 }
202
203 /*****************************************************************************
204  * Destroy : deallocate data structures
205  *****************************************************************************/
206 static void Destroy( vlc_object_t * _p_filter )
207 {
208     aout_filter_t * p_filter = (aout_filter_t *)_p_filter;
209     struct aout_filter_sys_t * p_sys = p_filter->p_sys;
210
211     mad_synth_finish( &p_sys->mad_synth );
212     mad_frame_finish( &p_sys->mad_frame );
213     mad_stream_finish( &p_sys->mad_stream );
214     free( p_sys );
215 }
216