1 /*****************************************************************************
2 * bandlimited.c : band-limited interpolation resampler
3 *****************************************************************************
4 * Copyright (C) 2002, 2006 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
27 * This implementation of the band-limited interpolationis based on the
29 * http://ccrma-www.stanford.edu/~jos/resample/resample.html
31 * It uses a Kaiser-windowed sinc-function low-pass filter and the width of the
32 * filter is 13 samples.
34 *****************************************************************************/
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
43 #include <vlc_filter.h>
44 #include <vlc_block.h>
48 #include "bandlimited.h"
50 /*****************************************************************************
52 *****************************************************************************/
55 static int OpenFilter ( vlc_object_t * );
56 static void CloseFilter( vlc_object_t * );
57 static block_t *Resample( filter_t *, block_t * );
59 static void ResampleFloat( filter_t *p_filter,
60 block_t **pp_out_buf, size_t *pi_out,
62 int i_in, int i_in_end,
63 double d_factor, bool b_factor_old,
64 int i_nb_channels, int i_bytes_per_frame );
66 /*****************************************************************************
68 *****************************************************************************/
71 int32_t *p_buf; /* this filter introduces a delay */
77 unsigned int i_remainder; /* remainder of previous sample */
83 /*****************************************************************************
85 *****************************************************************************/
87 set_category( CAT_AUDIO )
88 set_subcategory( SUBCAT_AUDIO_MISC )
89 set_description( N_("Audio filter for band-limited interpolation resampling") )
90 set_capability( "audio converter", 20 )
91 set_callbacks( OpenFilter, CloseFilter )
94 set_capability( "audio resampler", 20 )
95 set_callbacks( OpenFilter, CloseFilter )
98 /*****************************************************************************
99 * Resample: convert a buffer
100 *****************************************************************************/
101 static block_t *Resample( filter_t * p_filter, block_t * p_in_buf )
103 if( !p_in_buf || !p_in_buf->i_nb_samples )
106 block_Release( p_in_buf );
110 filter_sys_t *p_sys = p_filter->p_sys;
111 unsigned int i_out_rate = p_filter->fmt_out.audio.i_rate;
112 int i_nb_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
114 /* Check if we really need to run the resampler */
115 if( i_out_rate == p_filter->fmt_in.audio.i_rate )
117 if( !(p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY) &&
120 /* output the whole thing with the samples from last time */
121 p_in_buf = block_Realloc( p_in_buf,
122 p_sys->i_old_wing * p_filter->fmt_in.audio.i_bytes_per_frame,
123 p_in_buf->i_buffer );
126 memcpy( p_in_buf->p_buffer, p_sys->p_buf +
127 i_nb_channels * p_sys->i_old_wing,
129 p_filter->fmt_in.audio.i_bytes_per_frame );
131 p_in_buf->i_nb_samples += p_sys->i_old_wing;
133 p_in_buf->i_pts = date_Get( &p_sys->end_date );
135 date_Increment( &p_sys->end_date,
136 p_in_buf->i_nb_samples ) - p_in_buf->i_pts;
138 p_sys->i_old_wing = 0;
139 p_sys->b_first = true;
143 unsigned i_bytes_per_frame = p_filter->fmt_out.audio.i_channels *
144 p_filter->fmt_out.audio.i_bitspersample / 8;
145 size_t i_out_size = i_bytes_per_frame * ( 1 + ( p_in_buf->i_nb_samples *
146 p_filter->fmt_out.audio.i_rate / p_filter->fmt_in.audio.i_rate) )
147 + p_filter->p_sys->i_buf_size;
148 block_t *p_out_buf = block_Alloc( i_out_size );
151 block_Release( p_in_buf );
155 if( (p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY) || p_sys->b_first )
157 /* Continuity in sound samples has been broken, we'd better reset
159 p_out_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
160 p_sys->i_remainder = 0;
161 date_Init( &p_sys->end_date, i_out_rate, 1 );
162 date_Set( &p_sys->end_date, p_in_buf->i_pts );
163 p_sys->d_old_factor = 1;
164 p_sys->i_old_wing = 0;
165 p_sys->b_first = false;
168 size_t i_in_nb = p_in_buf->i_nb_samples;
169 size_t i_in, i_out = 0;
170 double d_factor, d_scale_factor, d_old_scale_factor;
171 size_t i_filter_wing;
174 msg_Err( p_filter, "old rate: %i, old factor: %f, old wing: %i, i_in: %i",
175 p_sys->i_old_rate, p_sys->d_old_factor,
176 p_sys->i_old_wing, i_in_nb );
179 /* Same format in and out... */
180 assert( p_filter->fmt_in.audio.i_bytes_per_frame == i_bytes_per_frame );
182 /* Prepare the source buffer */
183 if( p_sys->i_old_wing )
184 { /* Copy all our samples in p_in_buf */
185 /* Normally, there should be enough room for the old wing in the
186 * buffer head room. Otherwise, we need to copy memory anyway. */
187 p_in_buf = block_Realloc( p_in_buf,
188 p_sys->i_old_wing * 2 * i_bytes_per_frame,
189 p_in_buf->i_buffer );
190 if( unlikely(p_in_buf == NULL) )
192 memcpy( p_in_buf->p_buffer, p_sys->p_buf,
193 p_sys->i_old_wing * 2 * i_bytes_per_frame );
195 i_in_nb += (p_sys->i_old_wing * 2);
196 float *p_in = (float *)p_in_buf->p_buffer;
197 const float *p_in_orig = p_in;
199 /* Make sure the output buffer is reset */
200 memset( p_out_buf->p_buffer, 0, p_out_buf->i_buffer );
202 /* Calculate the new length of the filter wing */
203 d_factor = (double)i_out_rate / p_filter->fmt_in.audio.i_rate;
204 i_filter_wing = ((SMALL_FILTER_NMULT+1)/2.0) * __MAX(1.0,1.0/d_factor) + 1;
206 /* Account for increased filter gain when using factors less than 1 */
207 d_old_scale_factor = SMALL_FILTER_SCALE *
208 p_sys->d_old_factor + 0.5;
209 d_scale_factor = SMALL_FILTER_SCALE * d_factor + 0.5;
211 /* Apply the old rate until we have enough samples for the new one */
212 i_in = p_sys->i_old_wing;
213 p_in += p_sys->i_old_wing * i_nb_channels;
215 size_t i_old_in_end = 0;
216 if( p_sys->i_old_wing <= i_in_nb )
217 i_old_in_end = __MIN( i_filter_wing, i_in_nb - p_sys->i_old_wing );
219 ResampleFloat( p_filter,
220 &p_out_buf, &i_out, &p_in,
222 p_sys->d_old_factor, true,
223 i_nb_channels, i_bytes_per_frame );
224 i_in = __MAX( i_in, i_old_in_end );
226 /* Apply the new rate for the rest of the samples */
227 if( i_in < i_in_nb - i_filter_wing )
229 p_sys->d_old_factor = d_factor;
230 p_sys->i_old_wing = i_filter_wing;
234 ResampleFloat( p_filter,
235 &p_out_buf, &i_out, &p_in,
236 i_in, i_in_nb - i_filter_wing,
238 i_nb_channels, i_bytes_per_frame );
240 /* Finalize aout buffer */
241 p_out_buf->i_nb_samples = i_out;
243 p_out_buf->i_pts = date_Get( &p_sys->end_date );
244 p_out_buf->i_length = date_Increment( &p_sys->end_date,
245 p_out_buf->i_nb_samples ) - p_out_buf->i_pts;
247 p_out_buf->i_buffer = p_out_buf->i_nb_samples *
248 i_nb_channels * sizeof(int32_t);
251 /* Buffer i_filter_wing * 2 samples for next time */
252 if( p_sys->i_old_wing )
254 size_t newsize = p_sys->i_old_wing * 2 * i_bytes_per_frame;
255 if( newsize > p_sys->i_buf_size )
257 free( p_sys->p_buf );
258 p_sys->p_buf = malloc( newsize );
259 if( p_sys->p_buf != NULL )
260 p_sys->i_buf_size = newsize;
263 p_sys->i_buf_size = p_sys->i_old_wing = 0; /* oops! */
264 block_Release( p_in_buf );
268 memcpy( p_sys->p_buf,
269 p_in_orig + (i_in_nb - 2 * p_sys->i_old_wing) *
270 i_nb_channels, (2 * p_sys->i_old_wing) *
271 p_filter->fmt_in.audio.i_bytes_per_frame );
275 msg_Err( p_filter, "p_out size: %i, nb bytes out: %i", p_out_buf->i_buffer,
276 i_out * p_filter->fmt_in.audio.i_bytes_per_frame );
279 block_Release( p_in_buf );
283 /*****************************************************************************
285 *****************************************************************************/
286 static int OpenFilter( vlc_object_t *p_this )
288 filter_t *p_filter = (filter_t *)p_this;
290 unsigned int i_out_rate = p_filter->fmt_out.audio.i_rate;
292 if ( p_filter->fmt_in.audio.i_rate == p_filter->fmt_out.audio.i_rate
293 || p_filter->fmt_in.audio.i_format != p_filter->fmt_out.audio.i_format
294 || p_filter->fmt_in.audio.i_physical_channels
295 != p_filter->fmt_out.audio.i_physical_channels
296 || p_filter->fmt_in.audio.i_original_channels
297 != p_filter->fmt_out.audio.i_original_channels
298 || p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 )
303 /* Allocate the memory needed to store the module's structure */
304 p_filter->p_sys = p_sys = malloc( sizeof(struct filter_sys_t) );
309 p_sys->i_buf_size = 0;
311 p_sys->i_old_wing = 0;
312 p_sys->b_first = true;
313 p_filter->pf_audio_filter = Resample;
315 msg_Dbg( p_this, "%4.4s/%iKHz/%i->%4.4s/%iKHz/%i",
316 (char *)&p_filter->fmt_in.i_codec,
317 p_filter->fmt_in.audio.i_rate,
318 p_filter->fmt_in.audio.i_channels,
319 (char *)&p_filter->fmt_out.i_codec,
320 p_filter->fmt_out.audio.i_rate,
321 p_filter->fmt_out.audio.i_channels);
323 p_filter->fmt_out = p_filter->fmt_in;
324 p_filter->fmt_out.audio.i_rate = i_out_rate;
329 /*****************************************************************************
330 * CloseFilter : deallocate data structures
331 *****************************************************************************/
332 static void CloseFilter( vlc_object_t *p_this )
334 filter_t *p_filter = (filter_t *)p_this;
335 free( p_filter->p_sys->p_buf );
336 free( p_filter->p_sys );
339 static void FilterFloatUP( const float Imp[], const float ImpD[], uint16_t Nwing, float *p_in,
340 float *p_out, uint32_t ui_remainder,
341 uint32_t ui_output_rate, int16_t Inc, int i_nb_channels )
343 const float *Hp, *Hdp, *End;
345 uint32_t ui_linear_remainder;
348 Hp = &Imp[(ui_remainder<<Nhc)/ui_output_rate];
349 Hdp = &ImpD[(ui_remainder<<Nhc)/ui_output_rate];
353 ui_linear_remainder = (ui_remainder<<Nhc) -
354 (ui_remainder<<Nhc)/ui_output_rate*ui_output_rate;
356 if (Inc == 1) /* If doing right wing... */
357 { /* ...drop extra coeff, so when Ph is */
358 End--; /* 0.5, we don't do too many mult's */
359 if (ui_remainder == 0) /* If the phase is zero... */
360 { /* ...then we've already skipped the */
361 Hp += Npc; /* first sample, so we must also */
362 Hdp += Npc; /* skip ahead in Imp[] and ImpD[] */
367 t = *Hp; /* Get filter coeff */
368 /* t is now interp'd filter coeff */
369 t += *Hdp * ui_linear_remainder / ui_output_rate / Npc;
370 for( i = 0; i < i_nb_channels; i++ )
373 temp *= *(p_in+i); /* Mult coeff by input sample */
374 *(p_out+i) += temp; /* The filter output */
376 Hdp += Npc; /* Filter coeff differences step */
377 Hp += Npc; /* Filter coeff step */
378 p_in += (Inc * i_nb_channels); /* Input signal step */
382 static void FilterFloatUD( const float Imp[], const float ImpD[], uint16_t Nwing, float *p_in,
383 float *p_out, uint32_t ui_remainder,
384 uint32_t ui_output_rate, uint32_t ui_input_rate,
385 int16_t Inc, int i_nb_channels )
387 const float *Hp, *Hdp, *End;
389 uint32_t ui_linear_remainder;
390 int i, ui_counter = 0;
392 Hp = Imp + (ui_remainder<<Nhc) / ui_input_rate;
393 Hdp = ImpD + (ui_remainder<<Nhc) / ui_input_rate;
397 if (Inc == 1) /* If doing right wing... */
398 { /* ...drop extra coeff, so when Ph is */
399 End--; /* 0.5, we don't do too many mult's */
400 if (ui_remainder == 0) /* If the phase is zero... */
401 { /* ...then we've already skipped the */
402 Hp = Imp + /* first sample, so we must also */
403 (ui_output_rate << Nhc) / ui_input_rate;
404 Hdp = ImpD + /* skip ahead in Imp[] and ImpD[] */
405 (ui_output_rate << Nhc) / ui_input_rate;
411 t = *Hp; /* Get filter coeff */
412 /* t is now interp'd filter coeff */
413 ui_linear_remainder =
414 ((ui_output_rate * ui_counter + ui_remainder)<< Nhc) -
415 ((ui_output_rate * ui_counter + ui_remainder)<< Nhc) /
416 ui_input_rate * ui_input_rate;
417 t += *Hdp * ui_linear_remainder / ui_input_rate / Npc;
418 for( i = 0; i < i_nb_channels; i++ )
421 temp *= *(p_in+i); /* Mult coeff by input sample */
422 *(p_out+i) += temp; /* The filter output */
427 /* Filter coeff step */
428 Hp = Imp + ((ui_output_rate * ui_counter + ui_remainder)<< Nhc)
430 /* Filter coeff differences step */
431 Hdp = ImpD + ((ui_output_rate * ui_counter + ui_remainder)<< Nhc)
434 p_in += (Inc * i_nb_channels); /* Input signal step */
438 static int ReallocBuffer( block_t **pp_out_buf,
439 float **pp_out, size_t i_out,
440 int i_nb_channels, int i_bytes_per_frame )
442 if( i_out < (*pp_out_buf)->i_buffer/i_bytes_per_frame )
445 /* It may happen when the wing size changes */
446 const unsigned i_extra_frame = 256;
447 *pp_out_buf = block_Realloc( *pp_out_buf, 0,
448 (*pp_out_buf)->i_buffer +
449 i_extra_frame * i_bytes_per_frame );
453 *pp_out = (float*)(*pp_out_buf)->p_buffer + i_out * i_nb_channels;
454 memset( *pp_out, 0, i_extra_frame * i_bytes_per_frame );
458 static void ResampleFloat( filter_t *p_filter,
459 block_t **pp_out_buf, size_t *pi_out,
461 int i_in, int i_in_end,
462 double d_factor, bool b_factor_old,
463 int i_nb_channels, int i_bytes_per_frame )
465 filter_sys_t *p_sys = p_filter->p_sys;
467 float *p_in = *pp_in;
468 size_t i_out = *pi_out;
469 float *p_out = (float*)(*pp_out_buf)->p_buffer + i_out * i_nb_channels;
471 for( ; i_in < i_in_end; i_in++ )
473 if( b_factor_old && d_factor == 1 )
475 if( ReallocBuffer( pp_out_buf, &p_out,
476 i_out, i_nb_channels, i_bytes_per_frame ) )
478 /* Just copy the samples */
479 memcpy( p_out, p_in, i_bytes_per_frame );
480 p_in += i_nb_channels;
481 p_out += i_nb_channels;
486 while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate )
488 if( ReallocBuffer( pp_out_buf, &p_out,
489 i_out, i_nb_channels, i_bytes_per_frame ) )
494 /* FilterFloatUP() is faster if we can use it */
496 /* Perform left-wing inner product */
497 FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
498 SMALL_FILTER_NWING, p_in, p_out,
500 p_filter->fmt_out.audio.i_rate,
502 /* Perform right-wing inner product */
503 FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
504 SMALL_FILTER_NWING, p_in + i_nb_channels, p_out,
505 p_filter->fmt_out.audio.i_rate -
507 p_filter->fmt_out.audio.i_rate,
511 /* Normalize for unity filter gain */
512 for( i = 0; i < i_nb_channels; i++ )
514 *(p_out+i) *= d_old_scale_factor;
520 /* Perform left-wing inner product */
521 FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
522 SMALL_FILTER_NWING, p_in, p_out,
524 p_filter->fmt_out.audio.i_rate, p_filter->fmt_in.audio.i_rate,
526 /* Perform right-wing inner product */
527 FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
528 SMALL_FILTER_NWING, p_in + i_nb_channels, p_out,
529 p_filter->fmt_out.audio.i_rate -
531 p_filter->fmt_out.audio.i_rate, p_filter->fmt_in.audio.i_rate,
535 p_out += i_nb_channels;
538 p_sys->i_remainder += p_filter->fmt_in.audio.i_rate;
541 p_in += i_nb_channels;
542 p_sys->i_remainder -= p_filter->fmt_out.audio.i_rate;