1 /*****************************************************************************
2 * bandlimited.c : band-limited interpolation resampler
3 *****************************************************************************
4 * Copyright (C) 2002, 2006 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, 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 filter", 20 )
91 set_callbacks( OpenFilter, CloseFilter )
94 /*****************************************************************************
95 * Resample: convert a buffer
96 *****************************************************************************/
97 static block_t *Resample( filter_t * p_filter, block_t * p_in_buf )
99 if( !p_in_buf || !p_in_buf->i_nb_samples )
102 block_Release( p_in_buf );
106 filter_sys_t *p_sys = p_filter->p_sys;
107 unsigned int i_out_rate = p_filter->fmt_out.audio.i_rate;
108 int i_nb_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
110 /* Check if we really need to run the resampler */
111 if( i_out_rate == p_filter->fmt_in.audio.i_rate )
113 if( !(p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY) &&
116 /* output the whole thing with the samples from last time */
117 p_in_buf = block_Realloc( p_in_buf,
118 p_sys->i_old_wing * p_filter->fmt_in.audio.i_bytes_per_frame,
119 p_in_buf->i_buffer );
122 memcpy( p_in_buf->p_buffer, p_sys->p_buf +
123 i_nb_channels * p_sys->i_old_wing,
125 p_filter->fmt_in.audio.i_bytes_per_frame );
127 p_in_buf->i_nb_samples += p_sys->i_old_wing;
129 p_in_buf->i_pts = date_Get( &p_sys->end_date );
131 date_Increment( &p_sys->end_date,
132 p_in_buf->i_nb_samples ) - p_in_buf->i_pts;
134 p_sys->i_old_wing = 0;
135 p_sys->b_first = true;
139 unsigned i_bytes_per_frame = p_filter->fmt_out.audio.i_channels *
140 p_filter->fmt_out.audio.i_bitspersample / 8;
141 size_t i_out_size = i_bytes_per_frame * ( 1 + ( p_in_buf->i_nb_samples *
142 p_filter->fmt_out.audio.i_rate / p_filter->fmt_in.audio.i_rate) )
143 + p_filter->p_sys->i_buf_size;
144 block_t *p_out_buf = filter_NewAudioBuffer( p_filter, i_out_size );
147 block_Release( p_in_buf );
151 if( (p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY) || p_sys->b_first )
153 /* Continuity in sound samples has been broken, we'd better reset
155 p_out_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
156 p_sys->i_remainder = 0;
157 date_Init( &p_sys->end_date, i_out_rate, 1 );
158 date_Set( &p_sys->end_date, p_in_buf->i_pts );
159 p_sys->d_old_factor = 1;
160 p_sys->i_old_wing = 0;
161 p_sys->b_first = false;
164 size_t i_in_nb = p_in_buf->i_nb_samples;
165 size_t i_in, i_out = 0;
166 double d_factor, d_scale_factor, d_old_scale_factor;
167 size_t i_filter_wing;
170 msg_Err( p_filter, "old rate: %i, old factor: %f, old wing: %i, i_in: %i",
171 p_sys->i_old_rate, p_sys->d_old_factor,
172 p_sys->i_old_wing, i_in_nb );
175 /* Same format in and out... */
176 assert( p_filter->fmt_in.audio.i_bytes_per_frame == i_bytes_per_frame );
178 /* Prepare the source buffer */
179 if( p_sys->i_old_wing )
180 { /* Copy all our samples in p_in_buf */
181 /* Normally, there should be enough room for the old wing in the
182 * buffer head room. Otherwise, we need to copy memory anyway. */
183 p_in_buf = block_Realloc( p_in_buf,
184 p_sys->i_old_wing * 2 * i_bytes_per_frame,
185 p_in_buf->i_buffer );
186 if( unlikely(p_in_buf == NULL) )
188 memcpy( p_in_buf->p_buffer, p_sys->p_buf,
189 p_sys->i_old_wing * 2 * i_bytes_per_frame );
191 i_in_nb += (p_sys->i_old_wing * 2);
192 float *p_in = (float *)p_in_buf->p_buffer;
193 const float *p_in_orig = p_in;
195 /* Make sure the output buffer is reset */
196 memset( p_out_buf->p_buffer, 0, p_out_buf->i_buffer );
198 /* Calculate the new length of the filter wing */
199 d_factor = (double)i_out_rate / p_filter->fmt_in.audio.i_rate;
200 i_filter_wing = ((SMALL_FILTER_NMULT+1)/2.0) * __MAX(1.0,1.0/d_factor) + 1;
202 /* Account for increased filter gain when using factors less than 1 */
203 d_old_scale_factor = SMALL_FILTER_SCALE *
204 p_sys->d_old_factor + 0.5;
205 d_scale_factor = SMALL_FILTER_SCALE * d_factor + 0.5;
207 /* Apply the old rate until we have enough samples for the new one */
208 i_in = p_sys->i_old_wing;
209 p_in += p_sys->i_old_wing * i_nb_channels;
211 size_t i_old_in_end = 0;
212 if( p_sys->i_old_wing <= i_in_nb )
213 i_old_in_end = __MIN( i_filter_wing, i_in_nb - p_sys->i_old_wing );
215 ResampleFloat( p_filter,
216 &p_out_buf, &i_out, &p_in,
218 p_sys->d_old_factor, true,
219 i_nb_channels, i_bytes_per_frame );
220 i_in = __MAX( i_in, i_old_in_end );
222 /* Apply the new rate for the rest of the samples */
223 if( i_in < i_in_nb - i_filter_wing )
225 p_sys->d_old_factor = d_factor;
226 p_sys->i_old_wing = i_filter_wing;
230 ResampleFloat( p_filter,
231 &p_out_buf, &i_out, &p_in,
232 i_in, i_in_nb - i_filter_wing,
234 i_nb_channels, i_bytes_per_frame );
236 /* Finalize aout buffer */
237 p_out_buf->i_nb_samples = i_out;
238 p_out_buf->i_pts = date_Get( &p_sys->end_date );
239 p_out_buf->i_length = date_Increment( &p_sys->end_date,
240 p_out_buf->i_nb_samples ) - p_out_buf->i_pts;
242 p_out_buf->i_buffer = p_out_buf->i_nb_samples *
243 i_nb_channels * sizeof(int32_t);
246 /* Buffer i_filter_wing * 2 samples for next time */
247 if( p_sys->i_old_wing )
249 size_t newsize = p_sys->i_old_wing * 2 * i_bytes_per_frame;
250 if( newsize > p_sys->i_buf_size )
252 free( p_sys->p_buf );
253 p_sys->p_buf = malloc( newsize );
254 if( p_sys->p_buf != NULL )
255 p_sys->i_buf_size = newsize;
258 p_sys->i_buf_size = p_sys->i_old_wing = 0; /* oops! */
259 block_Release( p_in_buf );
263 memcpy( p_sys->p_buf,
264 p_in_orig + (i_in_nb - 2 * p_sys->i_old_wing) *
265 i_nb_channels, (2 * p_sys->i_old_wing) *
266 p_filter->fmt_in.audio.i_bytes_per_frame );
270 msg_Err( p_filter, "p_out size: %i, nb bytes out: %i", p_out_buf->i_buffer,
271 i_out * p_filter->fmt_in.audio.i_bytes_per_frame );
274 block_Release( p_in_buf );
278 /*****************************************************************************
280 *****************************************************************************/
281 static int OpenFilter( vlc_object_t *p_this )
283 filter_t *p_filter = (filter_t *)p_this;
285 unsigned int i_out_rate = p_filter->fmt_out.audio.i_rate;
287 if ( p_filter->fmt_in.audio.i_rate == p_filter->fmt_out.audio.i_rate
288 || p_filter->fmt_in.audio.i_format != p_filter->fmt_out.audio.i_format
289 || p_filter->fmt_in.audio.i_physical_channels
290 != p_filter->fmt_out.audio.i_physical_channels
291 || p_filter->fmt_in.audio.i_original_channels
292 != p_filter->fmt_out.audio.i_original_channels
293 || p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 )
298 #if !defined( SYS_DARWIN )
299 if( !var_InheritInteger( p_this, "hq-resampling" ) )
305 /* Allocate the memory needed to store the module's structure */
306 p_filter->p_sys = p_sys = malloc( sizeof(struct filter_sys_t) );
311 p_sys->i_buf_size = 0;
313 p_sys->i_old_wing = 0;
314 p_sys->b_first = true;
315 p_filter->pf_audio_filter = Resample;
317 msg_Dbg( p_this, "%4.4s/%iKHz/%i->%4.4s/%iKHz/%i",
318 (char *)&p_filter->fmt_in.i_codec,
319 p_filter->fmt_in.audio.i_rate,
320 p_filter->fmt_in.audio.i_channels,
321 (char *)&p_filter->fmt_out.i_codec,
322 p_filter->fmt_out.audio.i_rate,
323 p_filter->fmt_out.audio.i_channels);
325 p_filter->fmt_out = p_filter->fmt_in;
326 p_filter->fmt_out.audio.i_rate = i_out_rate;
331 /*****************************************************************************
332 * CloseFilter : deallocate data structures
333 *****************************************************************************/
334 static void CloseFilter( vlc_object_t *p_this )
336 filter_t *p_filter = (filter_t *)p_this;
337 free( p_filter->p_sys->p_buf );
338 free( p_filter->p_sys );
341 static void FilterFloatUP( const float Imp[], const float ImpD[], uint16_t Nwing, float *p_in,
342 float *p_out, uint32_t ui_remainder,
343 uint32_t ui_output_rate, int16_t Inc, int i_nb_channels )
345 const float *Hp, *Hdp, *End;
347 uint32_t ui_linear_remainder;
350 Hp = &Imp[(ui_remainder<<Nhc)/ui_output_rate];
351 Hdp = &ImpD[(ui_remainder<<Nhc)/ui_output_rate];
355 ui_linear_remainder = (ui_remainder<<Nhc) -
356 (ui_remainder<<Nhc)/ui_output_rate*ui_output_rate;
358 if (Inc == 1) /* If doing right wing... */
359 { /* ...drop extra coeff, so when Ph is */
360 End--; /* 0.5, we don't do too many mult's */
361 if (ui_remainder == 0) /* If the phase is zero... */
362 { /* ...then we've already skipped the */
363 Hp += Npc; /* first sample, so we must also */
364 Hdp += Npc; /* skip ahead in Imp[] and ImpD[] */
369 t = *Hp; /* Get filter coeff */
370 /* t is now interp'd filter coeff */
371 t += *Hdp * ui_linear_remainder / ui_output_rate / Npc;
372 for( i = 0; i < i_nb_channels; i++ )
375 temp *= *(p_in+i); /* Mult coeff by input sample */
376 *(p_out+i) += temp; /* The filter output */
378 Hdp += Npc; /* Filter coeff differences step */
379 Hp += Npc; /* Filter coeff step */
380 p_in += (Inc * i_nb_channels); /* Input signal step */
384 static void FilterFloatUD( const float Imp[], const float ImpD[], uint16_t Nwing, float *p_in,
385 float *p_out, uint32_t ui_remainder,
386 uint32_t ui_output_rate, uint32_t ui_input_rate,
387 int16_t Inc, int i_nb_channels )
389 const float *Hp, *Hdp, *End;
391 uint32_t ui_linear_remainder;
392 int i, ui_counter = 0;
394 Hp = Imp + (ui_remainder<<Nhc) / ui_input_rate;
395 Hdp = ImpD + (ui_remainder<<Nhc) / ui_input_rate;
399 if (Inc == 1) /* If doing right wing... */
400 { /* ...drop extra coeff, so when Ph is */
401 End--; /* 0.5, we don't do too many mult's */
402 if (ui_remainder == 0) /* If the phase is zero... */
403 { /* ...then we've already skipped the */
404 Hp = Imp + /* first sample, so we must also */
405 (ui_output_rate << Nhc) / ui_input_rate;
406 Hdp = ImpD + /* skip ahead in Imp[] and ImpD[] */
407 (ui_output_rate << Nhc) / ui_input_rate;
413 t = *Hp; /* Get filter coeff */
414 /* t is now interp'd filter coeff */
415 ui_linear_remainder =
416 ((ui_output_rate * ui_counter + ui_remainder)<< Nhc) -
417 ((ui_output_rate * ui_counter + ui_remainder)<< Nhc) /
418 ui_input_rate * ui_input_rate;
419 t += *Hdp * ui_linear_remainder / ui_input_rate / Npc;
420 for( i = 0; i < i_nb_channels; i++ )
423 temp *= *(p_in+i); /* Mult coeff by input sample */
424 *(p_out+i) += temp; /* The filter output */
429 /* Filter coeff step */
430 Hp = Imp + ((ui_output_rate * ui_counter + ui_remainder)<< Nhc)
432 /* Filter coeff differences step */
433 Hdp = ImpD + ((ui_output_rate * ui_counter + ui_remainder)<< Nhc)
436 p_in += (Inc * i_nb_channels); /* Input signal step */
440 static int ReallocBuffer( block_t **pp_out_buf,
441 float **pp_out, size_t i_out,
442 int i_nb_channels, int i_bytes_per_frame )
444 if( i_out < (*pp_out_buf)->i_buffer/i_bytes_per_frame )
447 /* It may happen when the wing size changes */
448 const unsigned i_extra_frame = 256;
449 *pp_out_buf = block_Realloc( *pp_out_buf, 0,
450 (*pp_out_buf)->i_buffer +
451 i_extra_frame * i_bytes_per_frame );
455 *pp_out = (float*)(*pp_out_buf)->p_buffer + i_out * i_nb_channels;
456 memset( *pp_out, 0, i_extra_frame * i_bytes_per_frame );
460 static void ResampleFloat( filter_t *p_filter,
461 block_t **pp_out_buf, size_t *pi_out,
463 int i_in, int i_in_end,
464 double d_factor, bool b_factor_old,
465 int i_nb_channels, int i_bytes_per_frame )
467 filter_sys_t *p_sys = p_filter->p_sys;
469 float *p_in = *pp_in;
470 size_t i_out = *pi_out;
471 float *p_out = (float*)(*pp_out_buf)->p_buffer + i_out * i_nb_channels;
473 for( ; i_in < i_in_end; i_in++ )
475 if( b_factor_old && d_factor == 1 )
477 if( ReallocBuffer( pp_out_buf, &p_out,
478 i_out, i_nb_channels, i_bytes_per_frame ) )
480 /* Just copy the samples */
481 memcpy( p_out, p_in, i_bytes_per_frame );
482 p_in += i_nb_channels;
483 p_out += i_nb_channels;
488 while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate )
490 if( ReallocBuffer( pp_out_buf, &p_out,
491 i_out, i_nb_channels, i_bytes_per_frame ) )
496 /* FilterFloatUP() is faster if we can use it */
498 /* Perform left-wing inner product */
499 FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
500 SMALL_FILTER_NWING, p_in, p_out,
502 p_filter->fmt_out.audio.i_rate,
504 /* Perform right-wing inner product */
505 FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
506 SMALL_FILTER_NWING, p_in + i_nb_channels, p_out,
507 p_filter->fmt_out.audio.i_rate -
509 p_filter->fmt_out.audio.i_rate,
513 /* Normalize for unity filter gain */
514 for( i = 0; i < i_nb_channels; i++ )
516 *(p_out+i) *= d_old_scale_factor;
522 /* Perform left-wing inner product */
523 FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
524 SMALL_FILTER_NWING, p_in, p_out,
526 p_filter->fmt_out.audio.i_rate, p_filter->fmt_in.audio.i_rate,
528 /* Perform right-wing inner product */
529 FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
530 SMALL_FILTER_NWING, p_in + i_nb_channels, p_out,
531 p_filter->fmt_out.audio.i_rate -
533 p_filter->fmt_out.audio.i_rate, p_filter->fmt_in.audio.i_rate,
537 p_out += i_nb_channels;
540 p_sys->i_remainder += p_filter->fmt_in.audio.i_rate;
543 p_in += i_nb_channels;
544 p_sys->i_remainder -= p_filter->fmt_out.audio.i_rate;