/*****************************************************************************
* linear.c : linear interpolation resampler
*****************************************************************************
- * Copyright (C) 2002 VideoLAN
- * $Id: linear.c,v 1.4 2002/11/11 22:27:01 gbazin Exp $
+ * Copyright (C) 2002, 2006 the VideoLAN team
+ * $Id$
*
* Authors: Gildas Bazin <gbazin@netcourrier.com>
- * Sigmund Augdal <sigmunau@idi.ntnu.no>
+ * Sigmund Augdal Helberg <dnumgis@videolan.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include <errno.h>
-#include <stdlib.h> /* malloc(), free() */
-#include <string.h>
-#include <vlc/vlc.h>
-#include "audio_output.h"
-#include "aout_internal.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_aout.h>
+#include <vlc_filter.h>
+#include <vlc_block.h>
+#include <vlc_cpu.h>
/*****************************************************************************
* Local prototypes
*****************************************************************************/
-static int Create ( vlc_object_t * );
-static void Close ( vlc_object_t * );
-static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
- aout_buffer_t * );
+static int OpenFilter ( vlc_object_t * );
+static void CloseFilter( vlc_object_t * );
+static block_t *Resample( filter_t *, block_t * );
+
+#if HAVE_FPU
+typedef float sample_t;
+# define VLC_CODEC_NATIVE VLC_CODEC_FL32
+#else
+typedef int32_t sample_t;
+# define VLC_CODEC_NATIVE VLC_CODEC_FI32
+#endif
/*****************************************************************************
* Local structures
*****************************************************************************/
-struct aout_filter_sys_t
+struct filter_sys_t
{
- int32_t *p_prev_sample; /* this filter introduces a 1 sample delay */
+ sample_t *p_prev_sample; /* this filter introduces a 1 sample delay */
- int i_remainder; /* remainder of previous sample */
+ unsigned int i_remainder; /* remainder of previous sample */
- audio_date_t end_date;
+ date_t end_date;
};
/*****************************************************************************
* Module descriptor
*****************************************************************************/
-vlc_module_begin();
- set_description( _("audio filter for linear interpolation resampling") );
- set_capability( "audio filter", 10 );
- set_callbacks( Create, Close );
-vlc_module_end();
+vlc_module_begin ()
+ set_description( N_("Audio filter for linear interpolation resampling") )
+ set_category( CAT_AUDIO )
+ set_subcategory( SUBCAT_AUDIO_MISC )
+ set_capability( "audio filter", 5 )
+ set_callbacks( OpenFilter, CloseFilter )
+vlc_module_end ()
/*****************************************************************************
- * Create: allocate linear resampler
+ * Resample: convert a buffer
*****************************************************************************/
-static int Create( vlc_object_t *p_this )
+static block_t *Resample( filter_t *p_filter, block_t *p_in_buf )
{
- aout_filter_t * p_filter = (aout_filter_t *)p_this;
- if ( p_filter->input.i_rate == p_filter->output.i_rate
- || p_filter->input.i_format != p_filter->output.i_format
- || p_filter->input.i_channels != p_filter->output.i_channels
- || p_filter->input.i_format != VLC_FOURCC('f','l','3','2') )
+ if( !p_in_buf || !p_in_buf->i_nb_samples )
{
- return VLC_EGENERIC;
+ if( p_in_buf )
+ block_Release( p_in_buf );
+ return NULL;
}
- /* Allocate the memory needed to store the module's structure */
- p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
- if( p_filter->p_sys == NULL )
- {
- msg_Err( p_filter, "out of memory" );
- return VLC_ENOMEM;
- }
- p_filter->p_sys->p_prev_sample = malloc( p_filter->input.i_channels
- * sizeof(int32_t) );
- if( p_filter->p_sys->p_prev_sample == NULL )
+ filter_sys_t *p_sys = p_filter->p_sys;
+ unsigned i_nb_channels = p_filter->fmt_in.audio.i_channels;
+ sample_t *p_prev_sample = p_sys->p_prev_sample;
+
+ /* Check if we really need to run the resampler */
+ if( p_filter->fmt_out.audio.i_rate == p_filter->fmt_in.audio.i_rate )
{
- msg_Err( p_filter, "out of memory" );
- return VLC_ENOMEM;
- }
+ if( !(p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY) )
+ {
+ p_in_buf = block_Realloc( p_in_buf,
+ sizeof(sample_t) * i_nb_channels,
+ p_in_buf->i_buffer );
+ if( !p_in_buf )
+ return NULL;
- p_filter->pf_do_work = DoWork;
- p_filter->b_in_place = VLC_FALSE;
+ memcpy( p_in_buf->p_buffer, p_prev_sample,
+ i_nb_channels * sizeof(sample_t) );
+ }
+ return p_in_buf;
+ }
- return VLC_SUCCESS;
-}
+ unsigned i_bytes_per_frame = p_filter->fmt_out.audio.i_channels *
+ p_filter->fmt_out.audio.i_bitspersample / 8;
-/*****************************************************************************
- * Close: free our resources
- *****************************************************************************/
-static void Close( vlc_object_t * p_this )
-{
- aout_filter_t * p_filter = (aout_filter_t *)p_this;
- free( p_filter->p_sys->p_prev_sample );
- free( p_filter->p_sys );
-}
+ size_t i_out_size = i_bytes_per_frame * (1 + (p_in_buf->i_nb_samples *
+ p_filter->fmt_out.audio.i_rate / p_filter->fmt_in.audio.i_rate));
+ block_t *p_out_buf = filter_NewAudioBuffer( p_filter, i_out_size );
+ if( !p_out_buf )
+ goto out;
-/*****************************************************************************
- * DoWork: convert a buffer
- *****************************************************************************/
-static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
- aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
-{
- float* p_in = (float*)p_in_buf->p_buffer;
- float* p_out = (float*)p_out_buf->p_buffer;
- float* p_prev_sample = (float*)p_filter->p_sys->p_prev_sample;
+ sample_t *p_out = (sample_t *)p_out_buf->p_buffer;
- int i_nb_channels = aout_FormatNbChannels( &p_filter->input );
- int i_in_nb = p_in_buf->i_nb_samples;
- int i_chan, i_in, i_out = 0;
+ unsigned i_in_nb = p_in_buf->i_nb_samples;
+ unsigned i_out = 0;
+ const sample_t *p_in = (sample_t *)p_in_buf->p_buffer;
/* Take care of the previous input sample (if any) */
- if( p_filter->b_reinit )
+ if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
{
- p_filter->b_reinit = VLC_FALSE;
- p_filter->p_sys->i_remainder = 0;
- aout_DateInit( &p_filter->p_sys->end_date, p_filter->output.i_rate );
+ p_out_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
+ p_sys->i_remainder = 0;
+ date_Init( &p_sys->end_date, p_filter->fmt_out.audio.i_rate, 1 );
}
else
{
- while( p_filter->p_sys->i_remainder < p_filter->output.i_rate )
+ while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate )
{
- for( i_chan = i_nb_channels ; i_chan ; )
+ for( unsigned i = 0; i < i_nb_channels ; i++ )
{
- i_chan--;
- p_out[i_chan] = p_prev_sample[i_chan];
- p_out[i_chan] += ( (p_prev_sample[i_chan] - p_in[i_chan])
- * p_filter->p_sys->i_remainder
- / p_filter->output.i_rate );
+ p_out[i] = p_prev_sample[i];
+#if HAVE_FPU
+ p_out[i] += (p_in[i] - p_prev_sample[i])
+#else
+ p_out[i] += (int64_t)(p_in[i] - p_prev_sample[i])
+#endif
+ * p_sys->i_remainder / p_filter->fmt_out.audio.i_rate;
}
p_out += i_nb_channels;
- i_out++;
+ i_out++;
- p_filter->p_sys->i_remainder += p_filter->input.i_rate;
+ p_sys->i_remainder += p_filter->fmt_in.audio.i_rate;
}
- p_filter->p_sys->i_remainder -= p_filter->output.i_rate;
+ p_sys->i_remainder -= p_filter->fmt_out.audio.i_rate;
}
/* Take care of the current input samples (minus last one) */
- for( i_in = 0; i_in < i_in_nb - 1; i_in++ )
+ for( unsigned i_in = 0; i_in < i_in_nb - 1; i_in++ )
{
- while( p_filter->p_sys->i_remainder < p_filter->output.i_rate )
+ while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate )
{
- for( i_chan = i_nb_channels ; i_chan ; )
+ for( unsigned i = 0; i < i_nb_channels ; i++ )
{
- i_chan--;
- p_out[i_chan] = p_in[i_chan];
- p_out[i_chan] += ( (p_in[i_chan] -
- p_in[i_chan + i_nb_channels])
- * p_filter->p_sys->i_remainder / p_filter->output.i_rate );
+ p_out[i] = p_in[i];
+#if HAVE_FPU
+ p_out[i] += (p_in[i + i_nb_channels] - p_in[i])
+#else
+ p_out[i] += (int64_t)(p_in[i + i_nb_channels] - p_in[i])
+#endif
+ * p_sys->i_remainder / p_filter->fmt_out.audio.i_rate;
}
p_out += i_nb_channels;
- i_out++;
+ i_out++;
- p_filter->p_sys->i_remainder += p_filter->input.i_rate;
+ p_sys->i_remainder += p_filter->fmt_in.audio.i_rate;
}
p_in += i_nb_channels;
- p_filter->p_sys->i_remainder -= p_filter->output.i_rate;
+ p_sys->i_remainder -= p_filter->fmt_out.audio.i_rate;
}
/* Backup the last input sample for next time */
- for( i_chan = i_nb_channels ; i_chan ; )
+ memcpy( p_prev_sample, p_in, i_nb_channels * sizeof(sample_t) );
+
+ p_out_buf->i_nb_samples = i_out;
+ p_out_buf->i_pts = p_in_buf->i_pts;
+
+ if( p_in_buf->i_pts !=
+ date_Get( &p_sys->end_date ) )
{
- i_chan--;
- p_prev_sample[i_chan] = p_in[i_chan];
+ date_Set( &p_sys->end_date, p_in_buf->i_pts );
}
- p_out_buf->i_nb_samples = i_out;
- p_out_buf->start_date = p_in_buf->start_date;
+ p_out_buf->i_length = date_Increment( &p_sys->end_date,
+ p_out_buf->i_nb_samples ) - p_out_buf->i_pts;
+
+ p_out_buf->i_buffer = p_out_buf->i_nb_samples *
+ i_nb_channels * sizeof(sample_t);
+out:
+ block_Release( p_in_buf );
+ return p_out_buf;
+}
+
+/*****************************************************************************
+ * OpenFilter:
+ *****************************************************************************/
+static int OpenFilter( vlc_object_t *p_this )
+{
+ filter_t *p_filter = (filter_t *)p_this;
+ filter_sys_t *p_sys;
+ int i_out_rate = p_filter->fmt_out.audio.i_rate;
+
+ if( p_filter->fmt_in.audio.i_rate == p_filter->fmt_out.audio.i_rate ||
+ p_filter->fmt_in.i_codec != VLC_CODEC_NATIVE )
+ {
+ return VLC_EGENERIC;
+ }
+
+ /* Allocate the memory needed to store the module's structure */
+ p_filter->p_sys = p_sys = malloc( sizeof(struct filter_sys_t) );
+ if( p_sys == NULL )
+ return VLC_ENOMEM;
- if( p_in_buf->start_date !=
- aout_DateGet( &p_filter->p_sys->end_date ) )
+ p_sys->p_prev_sample = malloc(
+ p_filter->fmt_in.audio.i_channels * sizeof(sample_t) );
+ if( p_sys->p_prev_sample == NULL )
{
- aout_DateSet( &p_filter->p_sys->end_date, p_in_buf->start_date );
+ free( p_sys );
+ return VLC_ENOMEM;
}
+ date_Init( &p_sys->end_date, p_filter->fmt_in.audio.i_rate, 1 );
+ p_sys->i_remainder = 0;
+
+ p_filter->pf_audio_filter = Resample;
+
+ msg_Dbg( p_this, "%4.4s/%iKHz/%i->%4.4s/%iKHz/%i",
+ (char *)&p_filter->fmt_in.i_codec,
+ p_filter->fmt_in.audio.i_rate,
+ p_filter->fmt_in.audio.i_channels,
+ (char *)&p_filter->fmt_out.i_codec,
+ p_filter->fmt_out.audio.i_rate,
+ p_filter->fmt_out.audio.i_channels);
- p_out_buf->end_date = aout_DateIncrement( &p_filter->p_sys->end_date,
- p_out_buf->i_nb_samples );
+ p_filter->fmt_out = p_filter->fmt_in;
+ p_filter->fmt_out.audio.i_rate = i_out_rate;
- p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples *
- i_nb_channels * sizeof(int32_t);
+ return 0;
+}
+/*****************************************************************************
+ * CloseFilter : deallocate data structures
+ *****************************************************************************/
+static void CloseFilter( vlc_object_t *p_this )
+{
+ filter_t *p_filter = (filter_t *)p_this;
+ free( p_filter->p_sys->p_prev_sample );
+ free( p_filter->p_sys );
}