linear.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:8k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * linear.c : linear interpolation resampler
  3.  *****************************************************************************
  4.  * Copyright (C) 2002 VideoLAN
  5.  * $Id: linear.c 7998 2004-06-18 12:38:28Z sigmunau $
  6.  *
  7.  * Authors: Gildas Bazin <gbazin@netcourrier.com>
  8.  *          Sigmund Augdal <sigmunau@idi.ntnu.no>
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #include <stdlib.h>                                      /* malloc(), free() */
  28. #include <string.h>
  29. #include <vlc/vlc.h>
  30. #include "audio_output.h"
  31. #include "aout_internal.h"
  32. /*****************************************************************************
  33.  * Local prototypes
  34.  *****************************************************************************/
  35. static int  Create    ( vlc_object_t * );
  36. static void Close     ( vlc_object_t * );
  37. static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
  38.                         aout_buffer_t * );
  39. /*****************************************************************************
  40.  * Local structures
  41.  *****************************************************************************/
  42. struct aout_filter_sys_t
  43. {
  44.     int32_t *p_prev_sample;       /* this filter introduces a 1 sample delay */
  45.     unsigned int i_remainder;                /* remainder of previous sample */
  46.     audio_date_t end_date;
  47. };
  48. /*****************************************************************************
  49.  * Module descriptor
  50.  *****************************************************************************/
  51. vlc_module_begin();
  52.     set_description( _("audio filter for linear interpolation resampling") );
  53.     set_capability( "audio filter", 5 );
  54.     set_callbacks( Create, Close );
  55. vlc_module_end();
  56. /*****************************************************************************
  57.  * Create: allocate linear resampler
  58.  *****************************************************************************/
  59. static int Create( vlc_object_t *p_this )
  60. {
  61.     aout_filter_t * p_filter = (aout_filter_t *)p_this;
  62.     if ( p_filter->input.i_rate == p_filter->output.i_rate
  63.           || p_filter->input.i_format != p_filter->output.i_format
  64.           || p_filter->input.i_physical_channels
  65.               != p_filter->output.i_physical_channels
  66.           || p_filter->input.i_original_channels
  67.               != p_filter->output.i_original_channels
  68.           || p_filter->input.i_format != VLC_FOURCC('f','l','3','2') )
  69.     {
  70.         return VLC_EGENERIC;
  71.     }
  72.     /* Allocate the memory needed to store the module's structure */
  73.     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
  74.     if( p_filter->p_sys == NULL )
  75.     {
  76.         msg_Err( p_filter, "out of memory" );
  77.         return VLC_ENOMEM;
  78.     }
  79.     p_filter->p_sys->p_prev_sample = malloc(
  80.         aout_FormatNbChannels( &p_filter->input ) * sizeof(int32_t) );
  81.     if( p_filter->p_sys->p_prev_sample == NULL )
  82.     {
  83.         msg_Err( p_filter, "out of memory" );
  84.         return VLC_ENOMEM;
  85.     }
  86.     p_filter->pf_do_work = DoWork;
  87.     /* We don't want a new buffer to be created because we're not sure we'll
  88.      * actually need to resample anything. */
  89.     p_filter->b_in_place = VLC_TRUE;
  90.     return VLC_SUCCESS;
  91. }
  92. /*****************************************************************************
  93.  * Close: free our resources
  94.  *****************************************************************************/
  95. static void Close( vlc_object_t * p_this )
  96. {
  97.     aout_filter_t * p_filter = (aout_filter_t *)p_this;
  98.     free( p_filter->p_sys->p_prev_sample );
  99.     free( p_filter->p_sys );
  100. }
  101. /*****************************************************************************
  102.  * DoWork: convert a buffer
  103.  *****************************************************************************/
  104. static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
  105.                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
  106. {
  107.     float *p_in_orig, *p_in, *p_out = (float *)p_out_buf->p_buffer;
  108.     float *p_prev_sample = (float *)p_filter->p_sys->p_prev_sample;
  109.     int i_nb_channels = aout_FormatNbChannels( &p_filter->input );
  110.     int i_in_nb = p_in_buf->i_nb_samples;
  111.     int i_chan, i_in, i_out = 0;
  112.     /* Check if we really need to run the resampler */
  113.     if( p_aout->mixer.mixer.i_rate == p_filter->input.i_rate )
  114.     {
  115.         if( p_filter->b_continuity &&
  116.             p_in_buf->i_size >=
  117.               p_in_buf->i_nb_bytes + sizeof(float) * i_nb_channels )
  118.         {
  119.             /* output the whole thing with the last sample from last time */
  120.             memmove( ((float *)(p_in_buf->p_buffer)) + i_nb_channels,
  121.                      p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
  122.             memcpy( p_in_buf->p_buffer, p_prev_sample,
  123.                     i_nb_channels * sizeof(float) );
  124.         }
  125.         p_filter->b_continuity = VLC_FALSE;
  126.         return;
  127.     }
  128. #ifdef HAVE_ALLOCA
  129.     p_in = (float *)alloca( p_in_buf->i_nb_bytes );
  130. #else
  131.     p_in_orig = p_in = (float *)malloc( p_in_buf->i_nb_bytes );
  132. #endif
  133.     if( p_in == NULL )
  134.     {
  135.         return;
  136.     }
  137.     p_aout->p_vlc->pf_memcpy( p_in, p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
  138.     /* Take care of the previous input sample (if any) */
  139.     if( !p_filter->b_continuity )
  140.     {
  141.         p_filter->b_continuity = VLC_TRUE;
  142.         p_filter->p_sys->i_remainder = 0;
  143.         aout_DateInit( &p_filter->p_sys->end_date, p_filter->output.i_rate );
  144.     }
  145.     else
  146.     {
  147.         while( p_filter->p_sys->i_remainder < p_filter->output.i_rate )
  148.         {
  149.             for( i_chan = i_nb_channels ; i_chan ; )
  150.             {
  151.                 i_chan--;
  152.                 p_out[i_chan] = p_prev_sample[i_chan];
  153.                 p_out[i_chan] += ( (p_prev_sample[i_chan] - p_in[i_chan])
  154.                                    * p_filter->p_sys->i_remainder
  155.                                    / p_filter->output.i_rate );
  156.             }
  157.             p_out += i_nb_channels;
  158.               i_out++;
  159.             p_filter->p_sys->i_remainder += p_filter->input.i_rate;
  160.         }
  161.         p_filter->p_sys->i_remainder -= p_filter->output.i_rate;
  162.     }
  163.     /* Take care of the current input samples (minus last one) */
  164.     for( i_in = 0; i_in < i_in_nb - 1; i_in++ )
  165.     {
  166.         while( p_filter->p_sys->i_remainder < p_filter->output.i_rate )
  167.         {
  168.             for( i_chan = i_nb_channels ; i_chan ; )
  169.             {
  170.                 i_chan--;
  171.                 p_out[i_chan] = p_in[i_chan];
  172.                 p_out[i_chan] += ( (p_in[i_chan] -
  173.                     p_in[i_chan + i_nb_channels])
  174.                     * p_filter->p_sys->i_remainder / p_filter->output.i_rate );
  175.             }
  176.             p_out += i_nb_channels;
  177.               i_out++;
  178.             p_filter->p_sys->i_remainder += p_filter->input.i_rate;
  179.         }
  180.         p_in += i_nb_channels;
  181.         p_filter->p_sys->i_remainder -= p_filter->output.i_rate;
  182.     }
  183.     /* Backup the last input sample for next time */
  184.     for( i_chan = i_nb_channels ; i_chan ; )
  185.     {
  186.         i_chan--;
  187.         p_prev_sample[i_chan] = p_in[i_chan];
  188.     }
  189.     p_out_buf->i_nb_samples = i_out;
  190.     p_out_buf->start_date = p_in_buf->start_date;
  191.     if( p_in_buf->start_date !=
  192.         aout_DateGet( &p_filter->p_sys->end_date ) )
  193.     {
  194.         aout_DateSet( &p_filter->p_sys->end_date, p_in_buf->start_date );
  195.     }
  196.     p_out_buf->end_date = aout_DateIncrement( &p_filter->p_sys->end_date,
  197.                                               p_out_buf->i_nb_samples );
  198.     p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples *
  199.         i_nb_channels * sizeof(int32_t);
  200. #ifndef HAVE_ALLOCA
  201.     free( p_in_orig );
  202. #endif
  203. }