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

ugly.c

00001 /*****************************************************************************
00002  * ugly.c : ugly resampler (changes pitch)
00003  *****************************************************************************
00004  * Copyright (C) 2002 the VideoLAN team
00005  * $Id: ugly.c 12653 2005-09-22 20:56:57Z gbazin $
00006  *
00007  * Authors: Samuel Hocevar <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  * 
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>                                      /* malloc(), free() */
00028 #include <string.h>
00029 
00030 #include <vlc/vlc.h>
00031 #include "audio_output.h"
00032 #include "aout_internal.h"
00033 
00034 /*****************************************************************************
00035  * Local prototypes
00036  *****************************************************************************/
00037 static int  Create    ( vlc_object_t * );
00038 
00039 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
00040                         aout_buffer_t * );
00041 
00042 /*****************************************************************************
00043  * Module descriptor
00044  *****************************************************************************/
00045 vlc_module_begin();
00046     set_description( _("audio filter for ugly resampling") );
00047     set_capability( "audio filter", 2 );
00048     set_category( CAT_AUDIO );
00049     set_subcategory( SUBCAT_AUDIO_MISC );
00050     set_callbacks( Create, NULL );
00051 vlc_module_end();
00052 
00053 /*****************************************************************************
00054  * Create: allocate ugly resampler
00055  *****************************************************************************/
00056 static int Create( vlc_object_t *p_this )
00057 {
00058     aout_filter_t * p_filter = (aout_filter_t *)p_this;
00059 
00060     if ( p_filter->input.i_rate == p_filter->output.i_rate
00061           || p_filter->input.i_format != p_filter->output.i_format
00062           || p_filter->input.i_physical_channels
00063               != p_filter->output.i_physical_channels
00064           || p_filter->input.i_original_channels
00065               != p_filter->output.i_original_channels
00066           || (p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
00067                && p_filter->input.i_format != VLC_FOURCC('f','i','3','2')) )
00068     {
00069         return VLC_EGENERIC;
00070     }
00071 
00072     p_filter->pf_do_work = DoWork;
00073 
00074     /* We don't want a new buffer to be created because we're not sure we'll
00075      * actually need to resample anything. */
00076     p_filter->b_in_place = VLC_TRUE;
00077 
00078     return VLC_SUCCESS;
00079 }
00080 
00081 /*****************************************************************************
00082  * DoWork: convert a buffer
00083  *****************************************************************************/
00084 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
00085                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
00086 {
00087     int32_t *p_in, *p_out = (int32_t*)p_out_buf->p_buffer;
00088 #ifndef HAVE_ALLOCA
00089     int32_t *p_in_orig;
00090 #endif
00091 
00092     unsigned int i_nb_channels = aout_FormatNbChannels( &p_filter->input );
00093     unsigned int i_in_nb = p_in_buf->i_nb_samples;
00094     unsigned int i_out_nb = i_in_nb * p_filter->output.i_rate
00095                                     / p_filter->input.i_rate;
00096     unsigned int i_sample_bytes = i_nb_channels * sizeof(int32_t);
00097     unsigned int i_out, i_chan, i_remainder = 0;
00098 
00099     /* Check if we really need to run the resampler */
00100     if( p_aout->mixer.mixer.i_rate == p_filter->input.i_rate )
00101     {
00102         return;
00103     }
00104 
00105 #ifdef HAVE_ALLOCA
00106     p_in = (int32_t *)alloca( p_in_buf->i_nb_bytes );
00107 #else
00108     p_in_orig = p_in = (int32_t *)malloc( p_in_buf->i_nb_bytes );
00109 #endif
00110     if( p_in == NULL )
00111     {
00112         return;
00113     }
00114 
00115     p_aout->p_vlc->pf_memcpy( p_in, p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
00116 
00117     for( i_out = i_out_nb ; i_out-- ; )
00118     {
00119         for( i_chan = i_nb_channels ; i_chan ; )
00120         {
00121             i_chan--;
00122             p_out[i_chan] = p_in[i_chan];
00123         }
00124         p_out += i_nb_channels;
00125 
00126         i_remainder += p_filter->input.i_rate;
00127         while( i_remainder >= p_filter->output.i_rate )
00128         {
00129             p_in += i_nb_channels;
00130             i_remainder -= p_filter->output.i_rate;
00131         }
00132     }
00133 
00134     p_out_buf->i_nb_samples = i_out_nb;
00135     p_out_buf->i_nb_bytes = i_out_nb * i_sample_bytes;
00136     p_out_buf->start_date = p_in_buf->start_date;
00137     p_out_buf->end_date = p_out_buf->start_date + p_out_buf->i_nb_samples *
00138         1000000 / p_filter->output.i_rate;
00139 
00140 #ifndef HAVE_ALLOCA
00141     free( p_in_orig );
00142 #endif
00143 
00144 }

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