00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdlib.h>
00029 #include <string.h>
00030
00031 #include <vlc/vlc.h>
00032 #include "audio_output.h"
00033 #include "aout_internal.h"
00034 #include "vlc_filter.h"
00035 #include "vlc_block.h"
00036
00037
00038
00039
00040 static int Create ( vlc_object_t * );
00041 static void Close ( vlc_object_t * );
00042 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
00043 aout_buffer_t * );
00044
00045 static int OpenFilter ( vlc_object_t * );
00046 static void CloseFilter( vlc_object_t * );
00047 static block_t *Resample( filter_t *, block_t * );
00048
00049
00050
00051
00052 struct filter_sys_t
00053 {
00054 int32_t *p_prev_sample;
00055
00056 unsigned int i_remainder;
00057
00058 audio_date_t end_date;
00059 };
00060
00061
00062
00063
00064 vlc_module_begin();
00065 set_description( _("audio filter for linear interpolation resampling") );
00066 set_category( CAT_AUDIO );
00067 set_subcategory( SUBCAT_AUDIO_MISC );
00068 set_capability( "audio filter", 5 );
00069 set_callbacks( Create, Close );
00070
00071 add_submodule();
00072 set_description( _("audio filter for linear interpolation resampling") );
00073 set_capability( "audio filter2", 5 );
00074 set_callbacks( OpenFilter, CloseFilter );
00075 vlc_module_end();
00076
00077
00078
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_rate == p_filter->output.i_rate
00086 || p_filter->input.i_format != p_filter->output.i_format
00087 || p_filter->input.i_physical_channels
00088 != p_filter->output.i_physical_channels
00089 || p_filter->input.i_original_channels
00090 != p_filter->output.i_original_channels
00091 || p_filter->input.i_format != VLC_FOURCC('f','l','3','2') )
00092 {
00093 return VLC_EGENERIC;
00094 }
00095
00096
00097 p_sys = malloc( sizeof(filter_sys_t) );
00098 p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
00099 if( p_sys == NULL )
00100 {
00101 msg_Err( p_filter, "out of memory" );
00102 return VLC_ENOMEM;
00103 }
00104 p_sys->p_prev_sample = malloc(
00105 aout_FormatNbChannels( &p_filter->input ) * sizeof(int32_t) );
00106 if( p_sys->p_prev_sample == NULL )
00107 {
00108 msg_Err( p_filter, "out of memory" );
00109 return VLC_ENOMEM;
00110 }
00111
00112 p_filter->pf_do_work = DoWork;
00113
00114
00115
00116 p_filter->b_in_place = VLC_TRUE;
00117
00118 return VLC_SUCCESS;
00119 }
00120
00121
00122
00123
00124 static void Close( vlc_object_t * p_this )
00125 {
00126 aout_filter_t * p_filter = (aout_filter_t *)p_this;
00127 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
00128
00129 free( p_sys->p_prev_sample );
00130 free( p_sys );
00131 }
00132
00133
00134
00135
00136 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
00137 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
00138 {
00139 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
00140 float *p_in, *p_out = (float *)p_out_buf->p_buffer;
00141 float *p_prev_sample = (float *)p_sys->p_prev_sample;
00142
00143 int i_nb_channels = aout_FormatNbChannels( &p_filter->input );
00144 int i_in_nb = p_in_buf->i_nb_samples;
00145 int i_chan, i_in, i_out = 0;
00146
00147
00148 if( p_aout->mixer.mixer.i_rate == p_filter->input.i_rate )
00149 {
00150 if( p_filter->b_continuity &&
00151 p_in_buf->i_size >=
00152 p_in_buf->i_nb_bytes + sizeof(float) * i_nb_channels )
00153 {
00154
00155 memmove( ((float *)(p_in_buf->p_buffer)) + i_nb_channels,
00156 p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
00157 memcpy( p_in_buf->p_buffer, p_prev_sample,
00158 i_nb_channels * sizeof(float) );
00159 }
00160 p_filter->b_continuity = VLC_FALSE;
00161 return;
00162 }
00163
00164 #ifdef HAVE_ALLOCA
00165 p_in = (float *)alloca( p_in_buf->i_nb_bytes );
00166 #else
00167 p_in_orig = p_in = (float *)malloc( p_in_buf->i_nb_bytes );
00168 #endif
00169 if( p_in == NULL )
00170 {
00171 return;
00172 }
00173
00174 p_aout->p_vlc->pf_memcpy( p_in, p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
00175
00176
00177 if( !p_filter->b_continuity )
00178 {
00179 p_filter->b_continuity = VLC_TRUE;
00180 p_sys->i_remainder = 0;
00181 aout_DateInit( &p_sys->end_date, p_filter->output.i_rate );
00182 }
00183 else
00184 {
00185 while( p_sys->i_remainder < p_filter->output.i_rate )
00186 {
00187 for( i_chan = i_nb_channels ; i_chan ; )
00188 {
00189 i_chan--;
00190 p_out[i_chan] = p_prev_sample[i_chan];
00191 p_out[i_chan] += ( ( p_in[i_chan] - p_prev_sample[i_chan] )
00192 * p_sys->i_remainder
00193 / p_filter->output.i_rate );
00194 }
00195 p_out += i_nb_channels;
00196 i_out++;
00197
00198 p_sys->i_remainder += p_filter->input.i_rate;
00199 }
00200 p_sys->i_remainder -= p_filter->output.i_rate;
00201 }
00202
00203
00204 for( i_in = 0; i_in < i_in_nb - 1; i_in++ )
00205 {
00206 while( p_sys->i_remainder < p_filter->output.i_rate )
00207 {
00208 for( i_chan = i_nb_channels ; i_chan ; )
00209 {
00210 i_chan--;
00211 p_out[i_chan] = p_in[i_chan];
00212 p_out[i_chan] += ( ( p_in[i_chan + i_nb_channels]
00213 - p_in[i_chan] )
00214 * p_sys->i_remainder / p_filter->output.i_rate );
00215 }
00216 p_out += i_nb_channels;
00217 i_out++;
00218
00219 p_sys->i_remainder += p_filter->input.i_rate;
00220 }
00221
00222 p_in += i_nb_channels;
00223 p_sys->i_remainder -= p_filter->output.i_rate;
00224 }
00225
00226
00227 for( i_chan = i_nb_channels ; i_chan ; )
00228 {
00229 i_chan--;
00230 p_prev_sample[i_chan] = p_in[i_chan];
00231 }
00232
00233 p_out_buf->i_nb_samples = i_out;
00234 p_out_buf->start_date = p_in_buf->start_date;
00235
00236 if( p_in_buf->start_date !=
00237 aout_DateGet( &p_sys->end_date ) )
00238 {
00239 aout_DateSet( &p_sys->end_date, p_in_buf->start_date );
00240 }
00241
00242 p_out_buf->end_date = aout_DateIncrement( &p_sys->end_date,
00243 p_out_buf->i_nb_samples );
00244
00245 p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples *
00246 i_nb_channels * sizeof(int32_t);
00247
00248 #ifndef HAVE_ALLOCA
00249 free( p_in_orig );
00250 #endif
00251
00252 }
00253
00254
00255
00256
00257 static int OpenFilter( vlc_object_t *p_this )
00258 {
00259 filter_t *p_filter = (filter_t *)p_this;
00260 filter_sys_t *p_sys;
00261 int i_out_rate = p_filter->fmt_out.audio.i_rate;
00262
00263 if( p_filter->fmt_in.audio.i_rate == p_filter->fmt_out.audio.i_rate ||
00264 p_filter->fmt_in.i_codec != VLC_FOURCC('f','l','3','2') )
00265 {
00266 return VLC_EGENERIC;
00267 }
00268
00269
00270 p_filter->p_sys = p_sys = malloc( sizeof(struct filter_sys_t) );
00271 if( p_sys == NULL )
00272 {
00273 msg_Err( p_filter, "out of memory" );
00274 return VLC_ENOMEM;
00275 }
00276
00277 p_sys->p_prev_sample = malloc(
00278 p_filter->fmt_in.audio.i_channels * sizeof(int32_t) );
00279 if( p_sys->p_prev_sample == NULL )
00280 {
00281 msg_Err( p_filter, "out of memory" );
00282 free( p_sys );
00283 return VLC_ENOMEM;
00284 }
00285
00286 p_filter->pf_audio_filter = Resample;
00287
00288 msg_Dbg( p_this, "%4.4s/%iKHz/%i->%4.4s/%iKHz/%i",
00289 (char *)&p_filter->fmt_in.i_codec,
00290 p_filter->fmt_in.audio.i_rate,
00291 p_filter->fmt_in.audio.i_channels,
00292 (char *)&p_filter->fmt_out.i_codec,
00293 p_filter->fmt_out.audio.i_rate,
00294 p_filter->fmt_out.audio.i_channels);
00295
00296 p_filter->fmt_out = p_filter->fmt_in;
00297 p_filter->fmt_out.audio.i_rate = i_out_rate;
00298
00299 return 0;
00300 }
00301
00302
00303
00304
00305 static void CloseFilter( vlc_object_t *p_this )
00306 {
00307 filter_t *p_filter = (filter_t *)p_this;
00308 free( p_filter->p_sys->p_prev_sample );
00309 free( p_filter->p_sys );
00310 }
00311
00312
00313
00314
00315 static block_t *Resample( filter_t *p_filter, block_t *p_block )
00316 {
00317 aout_filter_t aout_filter;
00318 aout_buffer_t in_buf, out_buf;
00319 block_t *p_out;
00320 int i_out_size;
00321 int i_bytes_per_frame;
00322
00323 if( !p_block || !p_block->i_samples )
00324 {
00325 if( p_block ) p_block->pf_release( p_block );
00326 return NULL;
00327 }
00328
00329 i_bytes_per_frame = p_filter->fmt_out.audio.i_channels *
00330 p_filter->fmt_out.audio.i_bitspersample / 8;
00331
00332 i_out_size = i_bytes_per_frame * ( 1 + (p_block->i_samples *
00333 p_filter->fmt_out.audio.i_rate / p_filter->fmt_in.audio.i_rate));
00334
00335 p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
00336 if( !p_out )
00337 {
00338 msg_Warn( p_filter, "can't get output buffer" );
00339 p_block->pf_release( p_block );
00340 return NULL;
00341 }
00342
00343 p_out->i_samples = i_out_size / i_bytes_per_frame;
00344 p_out->i_dts = p_block->i_dts;
00345 p_out->i_pts = p_block->i_pts;
00346 p_out->i_length = p_block->i_length;
00347
00348 aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
00349 aout_filter.input = p_filter->fmt_in.audio;
00350 aout_filter.output = p_filter->fmt_out.audio;
00351 aout_filter.b_continuity = VLC_FALSE;
00352
00353 in_buf.p_buffer = p_block->p_buffer;
00354 in_buf.i_nb_bytes = p_block->i_buffer;
00355 in_buf.i_nb_samples = p_block->i_samples;
00356 out_buf.p_buffer = p_out->p_buffer;
00357 out_buf.i_nb_bytes = p_out->i_buffer;
00358 out_buf.i_nb_samples = p_out->i_samples;
00359
00360 DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
00361
00362 p_block->pf_release( p_block );
00363
00364 p_out->i_buffer = out_buf.i_nb_bytes;
00365 p_out->i_samples = out_buf.i_nb_samples;
00366
00367 return p_out;
00368 }