Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

mpgatofixed32.c

00001 /*****************************************************************************
00002  * mpgatofixed32.c: MPEG-1 & 2 audio layer I, II, III + MPEG 2.5 decoder,
00003  * using MAD (MPEG Audio Decoder)
00004  *****************************************************************************
00005  * Copyright (C) 2001-2005 the VideoLAN team
00006  * $Id: mpgatofixed32.c 11664 2005-07-09 06:17:09Z courmisch $
00007  *
00008  * Authors: Christophe Massiot <[email protected]>
00009  *          Jean-Paul Saman <[email protected]>
00010  *      
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  * 
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00024  *****************************************************************************/
00025 
00026 /*****************************************************************************
00027  * Preamble
00028  *****************************************************************************/
00029 #include <stdlib.h>                                      /* malloc(), free() */
00030 #include <string.h>                                              /* strdup() */
00031 
00032 #include <mad.h>
00033 
00034 #include <vlc/vlc.h>
00035 #include <vlc/decoder.h>
00036 #include "aout_internal.h"
00037 #include "vlc_filter.h"
00038 
00039 /*****************************************************************************
00040  * Local prototypes
00041  *****************************************************************************/
00042 static int  Create    ( vlc_object_t * );
00043 static void Destroy   ( vlc_object_t * );
00044 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,  
00045                         aout_buffer_t * );
00046 
00047 static int  OpenFilter ( vlc_object_t * );
00048 static void CloseFilter( vlc_object_t * );
00049 static block_t *Convert( filter_t *, block_t * );
00050 
00051 /*****************************************************************************
00052  * Local structures
00053  *****************************************************************************/
00054 struct filter_sys_t
00055 {
00056     struct mad_stream mad_stream;
00057     struct mad_frame mad_frame;
00058     struct mad_synth mad_synth;
00059 };
00060 
00061 /*****************************************************************************
00062  * Module descriptor
00063  *****************************************************************************/
00064 vlc_module_begin();
00065     set_category( CAT_INPUT );
00066     set_subcategory( SUBCAT_INPUT_ACODEC );
00067     set_description( _("MPEG audio decoder") );
00068     set_capability( "audio filter", 100 );
00069     set_callbacks( Create, Destroy );
00070 
00071     add_submodule();
00072     set_description( _("MPEG audio decoder") );
00073     set_capability( "audio filter2", 100 );
00074     set_callbacks( OpenFilter, CloseFilter );
00075 vlc_module_end();
00076 
00077 /*****************************************************************************
00078  * Create: 
00079  *****************************************************************************/
00080 static int Create( vlc_object_t *p_this )
00081 {
00082     aout_filter_t *p_filter = (aout_filter_t *)p_this;
00083     struct filter_sys_t *p_sys;
00084 
00085     if ( (p_filter->input.i_format != VLC_FOURCC('m','p','g','a')
00086            && p_filter->input.i_format != VLC_FOURCC('m','p','g','3'))
00087             || (p_filter->output.i_format != VLC_FOURCC('f','l','3','2')
00088                  && p_filter->output.i_format != VLC_FOURCC('f','i','3','2')) )
00089     {
00090         return -1;
00091     }
00092 
00093     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
00094     {
00095         return -1;
00096     }
00097 
00098     /* Allocate the memory needed to store the module's structure */
00099     p_sys = malloc( sizeof(filter_sys_t) );
00100     p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
00101     if( p_sys == NULL )
00102     {
00103         msg_Err( p_filter, "out of memory" );
00104         return -1;
00105     }
00106 
00107     /* Initialize libmad */
00108     mad_stream_init( &p_sys->mad_stream );
00109     mad_frame_init( &p_sys->mad_frame );
00110     mad_synth_init( &p_sys->mad_synth );
00111     mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
00112 
00113     p_filter->pf_do_work = DoWork;
00114     p_filter->b_in_place = 0;
00115 
00116     return 0;
00117 }
00118 
00119 /*****************************************************************************
00120  * DoWork: decode an MPEG audio frame.
00121  *****************************************************************************/
00122 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
00123                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
00124 {
00125     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
00126 
00127     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
00128     p_out_buf->i_nb_bytes = p_in_buf->i_nb_samples * sizeof(vlc_fixed_t) * 
00129                                aout_FormatNbChannels( &p_filter->output );
00130 
00131     /* Do the actual decoding now. */
00132     mad_stream_buffer( &p_sys->mad_stream, p_in_buf->p_buffer,
00133                        p_in_buf->i_nb_bytes );
00134     if ( mad_frame_decode( &p_sys->mad_frame, &p_sys->mad_stream ) == -1 )
00135     {
00136         msg_Dbg( p_aout, "libmad error: %s",
00137                   mad_stream_errorstr( &p_sys->mad_stream ) );
00138         if( p_filter->output.i_format == VLC_FOURCC('f','l','3','2') )
00139         {
00140             int i;
00141             int i_size = p_out_buf->i_nb_bytes / sizeof(float);
00142             
00143             float * a = (float *)p_out_buf->p_buffer;
00144             for ( i = 0 ; i < i_size ; i++ )
00145                 *a++ = 0.0;
00146         }
00147         else
00148         {
00149             memset( p_out_buf->p_buffer, 0, p_out_buf->i_nb_bytes );
00150         }
00151         return;
00152     }
00153 
00154     mad_synth_frame( &p_sys->mad_synth, &p_sys->mad_frame );
00155 
00156     if ( p_filter->output.i_format == VLC_FOURCC('f','i','3','2') )
00157     {
00158         /* Interleave and keep buffers in mad_fixed_t format */
00159         mad_fixed_t * p_samples = (mad_fixed_t *)p_out_buf->p_buffer;
00160         struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
00161         unsigned int i_samples = p_pcm->length;
00162         mad_fixed_t const * p_left = p_pcm->samples[0];
00163         mad_fixed_t const * p_right = p_pcm->samples[1];
00164 
00165         switch ( p_pcm->channels )
00166         {
00167         case 2:
00168             if ( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
00169             {
00170                 while ( i_samples-- )
00171                 {
00172                     *p_samples++ = (*p_left++ >> 1) + (*p_right++ >> 1);
00173                 }
00174             }
00175             else if ( p_filter->output.i_original_channels == AOUT_CHAN_LEFT )
00176             {
00177                 while ( i_samples-- )
00178                 {
00179                     *p_samples++ = *p_left;
00180                     *p_samples++ = *p_left++;
00181                 }
00182             }
00183             else if ( p_filter->output.i_original_channels == AOUT_CHAN_RIGHT )
00184             {
00185                 while ( i_samples-- )
00186                 {
00187                     *p_samples++ = *p_right;
00188                     *p_samples++ = *p_right++;
00189                 }
00190             }
00191             else
00192             {
00193                 while ( i_samples-- )
00194                 {
00195                     *p_samples++ = *p_left++;
00196                     *p_samples++ = *p_right++;
00197                 }
00198             }
00199             break;
00200 
00201         case 1:
00202             p_filter->p_vlc->pf_memcpy( p_samples, p_left,
00203                                         i_samples * sizeof(mad_fixed_t) );
00204             break;
00205 
00206         default:
00207             msg_Err( p_aout, "cannot interleave %i channels",
00208                      p_pcm->channels );
00209         }
00210     }
00211     else
00212     {
00213         /* float32 */
00214         float * p_samples = (float *)p_out_buf->p_buffer;
00215         struct mad_pcm * p_pcm = &p_sys->mad_synth.pcm;
00216         unsigned int i_samples = p_pcm->length;
00217         mad_fixed_t const * p_left = p_pcm->samples[0];
00218         mad_fixed_t const * p_right = p_pcm->samples[1];
00219         float f_temp = (float)FIXED32_ONE;
00220 
00221         switch ( p_pcm->channels )
00222         {
00223         case 2:
00224             if ( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
00225             {
00226                 while ( i_samples-- )
00227                 {
00228                     *p_samples++ = (float)*p_left++ / f_temp / 2 +
00229                                    (float)*p_right++ / f_temp / 2;
00230                 }
00231             }
00232             else if ( p_filter->output.i_original_channels == AOUT_CHAN_LEFT )
00233             {
00234                 while ( i_samples-- )
00235                 {
00236                     *p_samples++ = (float)*p_left / f_temp;
00237                     *p_samples++ = (float)*p_left++ / f_temp;
00238                 }
00239             }
00240             else if ( p_filter->output.i_original_channels == AOUT_CHAN_RIGHT )
00241             {
00242                 while ( i_samples-- )
00243                 {
00244                     *p_samples++ = (float)*p_right / f_temp;
00245                     *p_samples++ = (float)*p_right++ / f_temp;
00246                 }
00247             }
00248             else
00249             {
00250                 while ( i_samples-- )
00251                 {
00252                     *p_samples++ = (float)*p_left++ / f_temp;
00253                     *p_samples++ = (float)*p_right++ / f_temp;
00254                 }
00255             }
00256             break;
00257 
00258         case 1:
00259             while ( i_samples-- )
00260             {
00261                 *p_samples++ = (float)*p_left++ / f_temp;
00262             }
00263             break;
00264 
00265         default:
00266             msg_Err( p_aout, "cannot interleave %i channels",
00267                      p_pcm->channels );
00268         }
00269     }
00270 }
00271 
00272 /*****************************************************************************
00273  * Destroy : deallocate data structures
00274  *****************************************************************************/
00275 static void Destroy( vlc_object_t *p_this )
00276 {
00277     aout_filter_t *p_filter = (aout_filter_t *)p_this;
00278     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
00279 
00280     mad_synth_finish( &p_sys->mad_synth );
00281     mad_frame_finish( &p_sys->mad_frame );
00282     mad_stream_finish( &p_sys->mad_stream );
00283     free( p_sys );
00284 }
00285 
00286 /*****************************************************************************
00287  * OpenFilter: 
00288  *****************************************************************************/
00289 static int OpenFilter( vlc_object_t *p_this )
00290 {
00291     filter_t *p_filter = (filter_t *)p_this;
00292     filter_sys_t *p_sys;
00293 
00294     if( p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') &&
00295         p_filter->fmt_in.i_codec != VLC_FOURCC('m','p','g','3') )
00296     {
00297         return VLC_EGENERIC;
00298     }
00299 
00300     /* Allocate the memory needed to store the module's structure */
00301     p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
00302     if( p_sys == NULL )
00303     {
00304         msg_Err( p_filter, "out of memory" );
00305         return -1;
00306     }
00307 
00308     p_filter->pf_audio_filter = Convert;
00309 
00310     /* Initialize libmad */
00311     mad_stream_init( &p_sys->mad_stream );
00312     mad_frame_init( &p_sys->mad_frame );
00313     mad_synth_init( &p_sys->mad_synth );
00314     mad_stream_options( &p_sys->mad_stream, MAD_OPTION_IGNORECRC );
00315 
00316     if( p_this->p_libvlc->i_cpu & CPU_CAPABILITY_FPU )
00317         p_filter->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
00318     else
00319         p_filter->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
00320     p_filter->fmt_out.audio.i_format = p_filter->fmt_out.i_codec;
00321 
00322     p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
00323 
00324     msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
00325              (char *)&p_filter->fmt_in.i_codec,
00326              (char *)&p_filter->fmt_out.i_codec,
00327              p_filter->fmt_in.audio.i_bitspersample );
00328 
00329     return 0;
00330 }
00331 
00332 /*****************************************************************************
00333  * CloseFilter : deallocate data structures
00334  *****************************************************************************/
00335 static void CloseFilter( vlc_object_t *p_this )
00336 {
00337     filter_t *p_filter = (filter_t *)p_this;
00338     filter_sys_t *p_sys = p_filter->p_sys;
00339 
00340     mad_synth_finish( &p_sys->mad_synth );
00341     mad_frame_finish( &p_sys->mad_frame );
00342     mad_stream_finish( &p_sys->mad_stream );
00343     free( p_sys );
00344 }
00345 
00346 static block_t *Convert( filter_t *p_filter, block_t *p_block )
00347 {
00348     aout_filter_t aout_filter;
00349     aout_buffer_t in_buf, out_buf;
00350     block_t *p_out;
00351     int i_out_size;
00352 
00353     if( !p_block || !p_block->i_samples )
00354     {
00355         if( p_block ) p_block->pf_release( p_block );
00356         return NULL;
00357     }
00358 
00359     i_out_size = p_block->i_samples *
00360       p_filter->fmt_out.audio.i_bitspersample *
00361         p_filter->fmt_out.audio.i_channels / 8;
00362 
00363     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
00364     if( !p_out )
00365     {
00366         msg_Warn( p_filter, "can't get output buffer" );
00367         p_block->pf_release( p_block );
00368         return NULL;
00369     }
00370 
00371     p_out->i_samples = p_block->i_samples;
00372     p_out->i_dts = p_block->i_dts;
00373     p_out->i_pts = p_block->i_pts;
00374     p_out->i_length = p_block->i_length;
00375 
00376     aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
00377     aout_filter.input = p_filter->fmt_in.audio;
00378     aout_filter.input.i_format = p_filter->fmt_in.i_codec;
00379     aout_filter.output = p_filter->fmt_out.audio;
00380     aout_filter.output.i_format = p_filter->fmt_out.i_codec;
00381 
00382     in_buf.p_buffer = p_block->p_buffer;
00383     in_buf.i_nb_bytes = p_block->i_buffer;
00384     in_buf.i_nb_samples = p_block->i_samples;
00385     out_buf.p_buffer = p_out->p_buffer;
00386     out_buf.i_nb_bytes = p_out->i_buffer;
00387     out_buf.i_nb_samples = p_out->i_samples;
00388 
00389     DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
00390 
00391     p_block->pf_release( p_block );
00392 
00393     p_out->i_buffer = out_buf.i_nb_bytes;
00394     p_out->i_samples = out_buf.i_nb_samples;
00395 
00396     return p_out;
00397 }

Generated on Tue Dec 20 10:14:27 2005 for vlc-0.8.4a by  doxygen 1.4.2