12e1f0b5138a50c8140baf09e12567b52b5f7898
[vlc.git] / modules / audio_filter / resampler / bandlimited.c
1 /*****************************************************************************
2  * bandlimited.c : band-limited interpolation resampler
3  *****************************************************************************
4  * Copyright (C) 2002, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble:
26  *
27  * This implementation of the band-limited interpolationis based on the
28  * following paper:
29  * http://ccrma-www.stanford.edu/~jos/resample/resample.html
30  *
31  * It uses a Kaiser-windowed sinc-function low-pass filter and the width of the
32  * filter is 13 samples.
33  *
34  *****************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_aout.h>
43 #include <vlc_filter.h>
44 #include <vlc_block.h>
45
46 #include "bandlimited.h"
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static int  Create    ( vlc_object_t * );
52 static void Close     ( vlc_object_t * );
53 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
54                         aout_buffer_t * );
55
56 /* audio filter2 */
57 static int  OpenFilter ( vlc_object_t * );
58 static void CloseFilter( vlc_object_t * );
59 static block_t *Resample( filter_t *, block_t * );
60
61
62 static void FilterFloatUP( const float Imp[], const float ImpD[], uint16_t Nwing,
63                            float *f_in, float *f_out, uint32_t ui_remainder,
64                            uint32_t ui_output_rate, int16_t Inc,
65                            int i_nb_channels );
66
67 static void FilterFloatUD( const float Imp[], const float ImpD[], uint16_t Nwing,
68                            float *f_in, float *f_out, uint32_t ui_remainder,
69                            uint32_t ui_output_rate, uint32_t ui_input_rate,
70                            int16_t Inc, int i_nb_channels );
71
72 /*****************************************************************************
73  * Local structures
74  *****************************************************************************/
75 struct filter_sys_t
76 {
77     int32_t *p_buf;                        /* this filter introduces a delay */
78     int i_buf_size;
79
80     int i_old_rate;
81     double d_old_factor;
82     int i_old_wing;
83
84     unsigned int i_remainder;                /* remainder of previous sample */
85
86     audio_date_t end_date;
87
88     bool b_first;
89     bool b_filter2;
90 };
91
92 /*****************************************************************************
93  * Module descriptor
94  *****************************************************************************/
95 vlc_module_begin ()
96     set_category( CAT_AUDIO )
97     set_subcategory( SUBCAT_AUDIO_MISC )
98     set_description( N_("Audio filter for band-limited interpolation resampling") )
99     set_capability( "audio filter", 20 )
100     set_callbacks( Create, Close )
101
102     add_submodule ()
103     set_description( _("Audio filter for band-limited interpolation resampling") )
104     set_capability( "audio filter2", 20 )
105     set_callbacks( OpenFilter, CloseFilter )
106 vlc_module_end ()
107
108 /*****************************************************************************
109  * Create: allocate linear resampler
110  *****************************************************************************/
111 static int Create( vlc_object_t *p_this )
112 {
113     aout_filter_t * p_filter = (aout_filter_t *)p_this;
114     struct filter_sys_t * p_sys;
115     double d_factor;
116     int i_filter_wing;
117
118     if ( p_filter->input.i_rate == p_filter->output.i_rate
119           || p_filter->input.i_format != p_filter->output.i_format
120           || p_filter->input.i_physical_channels
121               != p_filter->output.i_physical_channels
122           || p_filter->input.i_original_channels
123               != p_filter->output.i_original_channels
124           || p_filter->input.i_format != VLC_FOURCC('f','l','3','2') )
125     {
126         return VLC_EGENERIC;
127     }
128
129 #if !defined( __APPLE__ )
130     if( !config_GetInt( p_this, "hq-resampling" ) )
131     {
132         return VLC_EGENERIC;
133     }
134 #endif
135
136     /* Allocate the memory needed to store the module's structure */
137     p_sys = malloc( sizeof(filter_sys_t) );
138     if( p_sys == NULL )
139         return VLC_ENOMEM;
140     p_filter->p_sys = (struct aout_filter_sys_t *)p_sys;
141
142     /* Calculate worst case for the length of the filter wing */
143     d_factor = (double)p_filter->output.i_rate
144                         / p_filter->input.i_rate / AOUT_MAX_INPUT_RATE;
145     i_filter_wing = ((SMALL_FILTER_NMULT + 1)/2.0)
146                       * __MAX(1.0, 1.0/d_factor) + 10;
147     p_sys->i_buf_size = aout_FormatNbChannels( &p_filter->input ) *
148         sizeof(int32_t) * 2 * i_filter_wing;
149
150     /* Allocate enough memory to buffer previous samples */
151     p_sys->p_buf = malloc( p_sys->i_buf_size );
152     if( p_sys->p_buf == NULL )
153     {
154         free( p_sys );
155         return VLC_ENOMEM;
156     }
157
158     p_sys->i_old_wing = 0;
159     p_sys->b_filter2 = false;           /* It seams to be a good valuefor this module */
160     p_filter->pf_do_work = DoWork;
161
162     /* We don't want a new buffer to be created because we're not sure we'll
163      * actually need to resample anything. */
164     p_filter->b_in_place = true;
165
166     return VLC_SUCCESS;
167 }
168
169 /*****************************************************************************
170  * Close: free our resources
171  *****************************************************************************/
172 static void Close( vlc_object_t * p_this )
173 {
174     aout_filter_t * p_filter = (aout_filter_t *)p_this;
175     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
176     free( p_sys->p_buf );
177     free( p_sys );
178 }
179
180 /*****************************************************************************
181  * DoWork: convert a buffer
182  *****************************************************************************/
183 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
184                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
185 {
186     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
187     float *p_in, *p_in_orig, *p_out = (float *)p_out_buf->p_buffer;
188
189     int i_nb_channels = aout_FormatNbChannels( &p_filter->input );
190     int i_in_nb = p_in_buf->i_nb_samples;
191     int i_in, i_out = 0;
192     unsigned int i_out_rate;
193     double d_factor, d_scale_factor, d_old_scale_factor;
194     int i_filter_wing;
195
196     if( p_sys->b_filter2 )
197         i_out_rate = p_filter->output.i_rate;
198     else
199         i_out_rate = p_aout->mixer.mixer.i_rate;
200
201     /* Check if we really need to run the resampler */
202     if( i_out_rate == p_filter->input.i_rate )
203     {
204         if( /*p_filter->b_continuity && /--* What difference does it make ? :) */
205             p_sys->i_old_wing &&
206             p_in_buf->i_size >=
207               p_in_buf->i_nb_bytes + p_sys->i_old_wing *
208               p_filter->input.i_bytes_per_frame )
209         {
210             /* output the whole thing with the samples from last time */
211             memmove( ((float *)(p_in_buf->p_buffer)) +
212                      i_nb_channels * p_sys->i_old_wing,
213                      p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
214             memcpy( p_in_buf->p_buffer, p_sys->p_buf +
215                     i_nb_channels * p_sys->i_old_wing,
216                     p_sys->i_old_wing *
217                     p_filter->input.i_bytes_per_frame );
218
219             p_out_buf->i_nb_samples = p_in_buf->i_nb_samples +
220                 p_sys->i_old_wing;
221
222             p_out_buf->start_date = aout_DateGet( &p_sys->end_date );
223             p_out_buf->end_date =
224                 aout_DateIncrement( &p_sys->end_date,
225                                     p_out_buf->i_nb_samples );
226
227             p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples *
228                 p_filter->input.i_bytes_per_frame;
229         }
230         p_filter->b_continuity = false;
231         p_sys->i_old_wing = 0;
232         return;
233     }
234
235     if( !p_filter->b_continuity )
236     {
237         /* Continuity in sound samples has been broken, we'd better reset
238          * everything. */
239         p_filter->b_continuity = true;
240         p_sys->i_remainder = 0;
241         aout_DateInit( &p_sys->end_date, i_out_rate );
242         aout_DateSet( &p_sys->end_date, p_in_buf->start_date );
243         p_sys->i_old_rate   = p_filter->input.i_rate;
244         p_sys->d_old_factor = 1;
245         p_sys->i_old_wing   = 0;
246     }
247
248 #if 0
249     msg_Err( p_filter, "old rate: %i, old factor: %f, old wing: %i, i_in: %i",
250              p_sys->i_old_rate, p_sys->d_old_factor,
251              p_sys->i_old_wing, i_in_nb );
252 #endif
253
254     /* Prepare the source buffer */
255     i_in_nb += (p_sys->i_old_wing * 2);
256 #ifdef HAVE_ALLOCA
257     p_in = p_in_orig = (float *)alloca( i_in_nb *
258                                         p_filter->input.i_bytes_per_frame );
259 #else
260     p_in = p_in_orig = (float *)malloc( i_in_nb *
261                                         p_filter->input.i_bytes_per_frame );
262 #endif
263     if( p_in == NULL )
264     {
265         return;
266     }
267
268     /* Copy all our samples in p_in */
269     if( p_sys->i_old_wing )
270     {
271         vlc_memcpy( p_in, p_sys->p_buf,
272                     p_sys->i_old_wing * 2 *
273                       p_filter->input.i_bytes_per_frame );
274     }
275     vlc_memcpy( p_in + p_sys->i_old_wing * 2 * i_nb_channels,
276                 p_in_buf->p_buffer,
277                 p_in_buf->i_nb_samples * p_filter->input.i_bytes_per_frame );
278
279     /* Make sure the output buffer is reset */
280     memset( p_out, 0, p_out_buf->i_size );
281
282     /* Calculate the new length of the filter wing */
283     d_factor = (double)i_out_rate / p_filter->input.i_rate;
284     i_filter_wing = ((SMALL_FILTER_NMULT+1)/2.0) * __MAX(1.0,1.0/d_factor) + 1;
285
286     /* Account for increased filter gain when using factors less than 1 */
287     d_old_scale_factor = SMALL_FILTER_SCALE *
288         p_sys->d_old_factor + 0.5;
289     d_scale_factor = SMALL_FILTER_SCALE * d_factor + 0.5;
290
291     /* Apply the old rate until we have enough samples for the new one */
292     i_in = p_sys->i_old_wing;
293     p_in += p_sys->i_old_wing * i_nb_channels;
294     for( ; i_in < i_filter_wing &&
295            (i_in + p_sys->i_old_wing) < i_in_nb; i_in++ )
296     {
297         if( p_sys->d_old_factor == 1 )
298         {
299             /* Just copy the samples */
300             memcpy( p_out, p_in,
301                     p_filter->input.i_bytes_per_frame );
302             p_in += i_nb_channels;
303             p_out += i_nb_channels;
304             i_out++;
305             continue;
306         }
307
308         while( p_sys->i_remainder < p_filter->output.i_rate )
309         {
310
311             if( p_sys->d_old_factor >= 1 )
312             {
313                 /* FilterFloatUP() is faster if we can use it */
314
315                 /* Perform left-wing inner product */
316                 FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
317                                SMALL_FILTER_NWING, p_in, p_out,
318                                p_sys->i_remainder,
319                                p_filter->output.i_rate,
320                                -1, i_nb_channels );
321                 /* Perform right-wing inner product */
322                 FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
323                                SMALL_FILTER_NWING, p_in + i_nb_channels, p_out,
324                                p_filter->output.i_rate -
325                                p_sys->i_remainder,
326                                p_filter->output.i_rate,
327                                1, i_nb_channels );
328
329 #if 0
330                 /* Normalize for unity filter gain */
331                 for( i = 0; i < i_nb_channels; i++ )
332                 {
333                     *(p_out+i) *= d_old_scale_factor;
334                 }
335 #endif
336
337                 /* Sanity check */
338                 if( p_out_buf->i_size/p_filter->input.i_bytes_per_frame
339                     <= (unsigned int)i_out+1 )
340                 {
341                     p_out += i_nb_channels;
342                     i_out++;
343                     p_sys->i_remainder += p_filter->input.i_rate;
344                     break;
345                 }
346             }
347             else
348             {
349                 /* Perform left-wing inner product */
350                 FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
351                                SMALL_FILTER_NWING, p_in, p_out,
352                                p_sys->i_remainder,
353                                p_filter->output.i_rate, p_filter->input.i_rate,
354                                -1, i_nb_channels );
355                 /* Perform right-wing inner product */
356                 FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
357                                SMALL_FILTER_NWING, p_in + i_nb_channels, p_out,
358                                p_filter->output.i_rate -
359                                p_sys->i_remainder,
360                                p_filter->output.i_rate, p_filter->input.i_rate,
361                                1, i_nb_channels );
362             }
363
364             p_out += i_nb_channels;
365             i_out++;
366
367             p_sys->i_remainder += p_filter->input.i_rate;
368         }
369
370         p_in += i_nb_channels;
371         p_sys->i_remainder -= p_filter->output.i_rate;
372     }
373
374     /* Apply the new rate for the rest of the samples */
375     if( i_in < i_in_nb - i_filter_wing )
376     {
377         p_sys->i_old_rate   = p_filter->input.i_rate;
378         p_sys->d_old_factor = d_factor;
379         p_sys->i_old_wing   = i_filter_wing;
380     }
381     for( ; i_in < i_in_nb - i_filter_wing; i_in++ )
382     {
383         while( p_sys->i_remainder < p_filter->output.i_rate )
384         {
385
386             if( d_factor >= 1 )
387             {
388                 /* FilterFloatUP() is faster if we can use it */
389
390                 /* Perform left-wing inner product */
391                 FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
392                                SMALL_FILTER_NWING, p_in, p_out,
393                                p_sys->i_remainder,
394                                p_filter->output.i_rate,
395                                -1, i_nb_channels );
396
397                 /* Perform right-wing inner product */
398                 FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
399                                SMALL_FILTER_NWING, p_in + i_nb_channels, p_out,
400                                p_filter->output.i_rate -
401                                p_sys->i_remainder,
402                                p_filter->output.i_rate,
403                                1, i_nb_channels );
404
405 #if 0
406                 /* Normalize for unity filter gain */
407                 for( int i = 0; i < i_nb_channels; i++ )
408                 {
409                     *(p_out+i) *= d_old_scale_factor;
410                 }
411 #endif
412                 /* Sanity check */
413                 if( p_out_buf->i_size/p_filter->input.i_bytes_per_frame
414                     <= (unsigned int)i_out+1 )
415                 {
416                     p_out += i_nb_channels;
417                     i_out++;
418                     p_sys->i_remainder += p_filter->input.i_rate;
419                     break;
420                 }
421             }
422             else
423             {
424                 /* Perform left-wing inner product */
425                 FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
426                                SMALL_FILTER_NWING, p_in, p_out,
427                                p_sys->i_remainder,
428                                p_filter->output.i_rate, p_filter->input.i_rate,
429                                -1, i_nb_channels );
430                 /* Perform right-wing inner product */
431                 FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
432                                SMALL_FILTER_NWING, p_in + i_nb_channels, p_out,
433                                p_filter->output.i_rate -
434                                p_sys->i_remainder,
435                                p_filter->output.i_rate, p_filter->input.i_rate,
436                                1, i_nb_channels );
437             }
438
439             p_out += i_nb_channels;
440             i_out++;
441
442             p_sys->i_remainder += p_filter->input.i_rate;
443         }
444
445         p_in += i_nb_channels;
446         p_sys->i_remainder -= p_filter->output.i_rate;
447     }
448
449     /* Buffer i_filter_wing * 2 samples for next time */
450     if( p_sys->i_old_wing )
451     {
452         memcpy( p_sys->p_buf,
453                 p_in_orig + (i_in_nb - 2 * p_sys->i_old_wing) *
454                 i_nb_channels, (2 * p_sys->i_old_wing) *
455                 p_filter->input.i_bytes_per_frame );
456     }
457
458 #if 0
459     msg_Err( p_filter, "p_out size: %i, nb bytes out: %i", p_out_buf->i_size,
460              i_out * p_filter->input.i_bytes_per_frame );
461 #endif
462
463     /* Free the temp buffer */
464 #ifndef HAVE_ALLOCA
465     free( p_in_orig );
466 #endif
467
468     /* Finalize aout buffer */
469     p_out_buf->i_nb_samples = i_out;
470     p_out_buf->start_date = aout_DateGet( &p_sys->end_date );
471     p_out_buf->end_date = aout_DateIncrement( &p_sys->end_date,
472                                               p_out_buf->i_nb_samples );
473
474     p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples *
475         i_nb_channels * sizeof(int32_t);
476
477 }
478
479 /*****************************************************************************
480  * OpenFilter:
481  *****************************************************************************/
482 static int OpenFilter( vlc_object_t *p_this )
483 {
484     filter_t *p_filter = (filter_t *)p_this;
485     filter_sys_t *p_sys;
486     unsigned int i_out_rate  = p_filter->fmt_out.audio.i_rate;
487     double d_factor;
488     int i_filter_wing;
489
490     if( p_filter->fmt_in.audio.i_rate == p_filter->fmt_out.audio.i_rate ||
491         p_filter->fmt_in.i_codec != VLC_FOURCC('f','l','3','2') )
492     {
493         return VLC_EGENERIC;
494     }
495
496 #if !defined( SYS_DARWIN )
497     if( !config_GetInt( p_this, "hq-resampling" ) )
498     {
499         return VLC_EGENERIC;
500     }
501 #endif
502
503     /* Allocate the memory needed to store the module's structure */
504     p_filter->p_sys = p_sys = malloc( sizeof(struct filter_sys_t) );
505     if( p_sys == NULL )
506         return VLC_ENOMEM;
507
508     /* Calculate worst case for the length of the filter wing */
509     d_factor = (double)i_out_rate / p_filter->fmt_in.audio.i_rate;
510     i_filter_wing = ((SMALL_FILTER_NMULT + 1)/2.0)
511                       * __MAX(1.0, 1.0/d_factor) + 10;
512     p_filter->p_sys->i_buf_size = p_filter->fmt_in.audio.i_channels *
513         sizeof(int32_t) * 2 * i_filter_wing;
514
515     /* Allocate enough memory to buffer previous samples */
516     p_filter->p_sys->p_buf = malloc( p_filter->p_sys->i_buf_size );
517     if( p_filter->p_sys->p_buf == NULL )
518     {
519         free( p_sys );
520         return VLC_ENOMEM;
521     }
522
523     p_filter->p_sys->i_old_wing = 0;
524     p_sys->b_first = true;
525     p_sys->b_filter2 = true;
526     p_filter->pf_audio_filter = Resample;
527
528     msg_Dbg( p_this, "%4.4s/%iKHz/%i->%4.4s/%iKHz/%i",
529              (char *)&p_filter->fmt_in.i_codec,
530              p_filter->fmt_in.audio.i_rate,
531              p_filter->fmt_in.audio.i_channels,
532              (char *)&p_filter->fmt_out.i_codec,
533              p_filter->fmt_out.audio.i_rate,
534              p_filter->fmt_out.audio.i_channels);
535
536     p_filter->fmt_out = p_filter->fmt_in;
537     p_filter->fmt_out.audio.i_rate = i_out_rate;
538
539     return 0;
540 }
541
542 /*****************************************************************************
543  * CloseFilter : deallocate data structures
544  *****************************************************************************/
545 static void CloseFilter( vlc_object_t *p_this )
546 {
547     filter_t *p_filter = (filter_t *)p_this;
548     free( p_filter->p_sys->p_buf );
549     free( p_filter->p_sys );
550 }
551
552 /*****************************************************************************
553  * Resample
554  *****************************************************************************/
555 static block_t *Resample( filter_t *p_filter, block_t *p_block )
556 {
557     aout_filter_t aout_filter;
558     aout_buffer_t in_buf, out_buf;
559     block_t *p_out;
560     int i_out_size;
561     int i_bytes_per_frame;
562
563     if( !p_block || !p_block->i_samples )
564     {
565         if( p_block )
566             block_Release( p_block );
567         return NULL;
568     }
569
570     i_bytes_per_frame = p_filter->fmt_out.audio.i_channels *
571                   p_filter->fmt_out.audio.i_bitspersample / 8;
572
573     i_out_size = i_bytes_per_frame * ( 1 + (p_block->i_samples *
574         p_filter->fmt_out.audio.i_rate / p_filter->fmt_in.audio.i_rate));
575
576     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
577     if( !p_out )
578     {
579         msg_Warn( p_filter, "can't get output buffer" );
580         block_Release( p_block );
581         return NULL;
582     }
583
584     p_out->i_samples = i_out_size / i_bytes_per_frame;
585     p_out->i_dts = p_block->i_dts;
586     p_out->i_pts = p_block->i_pts;
587     p_out->i_length = p_block->i_length;
588
589     aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
590     aout_filter.input = p_filter->fmt_in.audio;
591     aout_filter.input.i_bytes_per_frame = p_filter->fmt_in.audio.i_channels *
592                   p_filter->fmt_in.audio.i_bitspersample / 8;
593     aout_filter.output = p_filter->fmt_out.audio;
594     aout_filter.output.i_bytes_per_frame = p_filter->fmt_out.audio.i_channels *
595                   p_filter->fmt_out.audio.i_bitspersample / 8;
596     aout_filter.b_continuity = !p_filter->p_sys->b_first;
597     p_filter->p_sys->b_first = false;
598
599     in_buf.p_buffer = p_block->p_buffer;
600     in_buf.i_nb_bytes = in_buf.i_size = p_block->i_buffer;
601     in_buf.i_nb_samples = p_block->i_samples;
602     out_buf.p_buffer = p_out->p_buffer;
603     out_buf.i_nb_bytes = out_buf.i_size = p_out->i_buffer;
604     out_buf.i_nb_samples = p_out->i_samples;
605
606     DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
607
608     block_Release( p_block );
609
610     p_out->i_buffer = out_buf.i_nb_bytes;
611     p_out->i_samples = out_buf.i_nb_samples;
612
613     return p_out;
614 }
615
616 void FilterFloatUP( const float Imp[], const float ImpD[], uint16_t Nwing, float *p_in,
617                     float *p_out, uint32_t ui_remainder,
618                     uint32_t ui_output_rate, int16_t Inc, int i_nb_channels )
619 {
620     const float *Hp, *Hdp, *End;
621     float t, temp;
622     uint32_t ui_linear_remainder;
623     int i;
624
625     Hp = &Imp[(ui_remainder<<Nhc)/ui_output_rate];
626     Hdp = &ImpD[(ui_remainder<<Nhc)/ui_output_rate];
627
628     End = &Imp[Nwing];
629
630     ui_linear_remainder = (ui_remainder<<Nhc) -
631                             (ui_remainder<<Nhc)/ui_output_rate*ui_output_rate;
632
633     if (Inc == 1)               /* If doing right wing...              */
634     {                           /* ...drop extra coeff, so when Ph is  */
635         End--;                  /*    0.5, we don't do too many mult's */
636         if (ui_remainder == 0)  /* If the phase is zero...           */
637         {                       /* ...then we've already skipped the */
638             Hp += Npc;          /*    first sample, so we must also  */
639             Hdp += Npc;         /*    skip ahead in Imp[] and ImpD[] */
640         }
641     }
642
643     while (Hp < End) {
644         t = *Hp;                /* Get filter coeff */
645                                 /* t is now interp'd filter coeff */
646         t += *Hdp * ui_linear_remainder / ui_output_rate / Npc;
647         for( i = 0; i < i_nb_channels; i++ )
648         {
649             temp = t;
650             temp *= *(p_in+i);  /* Mult coeff by input sample */
651             *(p_out+i) += temp; /* The filter output */
652         }
653         Hdp += Npc;             /* Filter coeff differences step */
654         Hp += Npc;              /* Filter coeff step */
655         p_in += (Inc * i_nb_channels); /* Input signal step */
656     }
657 }
658
659 void FilterFloatUD( const float Imp[], const float ImpD[], uint16_t Nwing, float *p_in,
660                     float *p_out, uint32_t ui_remainder,
661                     uint32_t ui_output_rate, uint32_t ui_input_rate,
662                     int16_t Inc, int i_nb_channels )
663 {
664     const float *Hp, *Hdp, *End;
665     float t, temp;
666     uint32_t ui_linear_remainder;
667     int i, ui_counter = 0;
668
669     Hp = Imp + (ui_remainder<<Nhc) / ui_input_rate;
670     Hdp = ImpD  + (ui_remainder<<Nhc) / ui_input_rate;
671
672     End = &Imp[Nwing];
673
674     if (Inc == 1)               /* If doing right wing...              */
675     {                           /* ...drop extra coeff, so when Ph is  */
676         End--;                  /*    0.5, we don't do too many mult's */
677         if (ui_remainder == 0)  /* If the phase is zero...           */
678         {                       /* ...then we've already skipped the */
679             Hp = Imp +          /* first sample, so we must also  */
680                   (ui_output_rate << Nhc) / ui_input_rate;
681             Hdp = ImpD +        /* skip ahead in Imp[] and ImpD[] */
682                   (ui_output_rate << Nhc) / ui_input_rate;
683             ui_counter++;
684         }
685     }
686
687     while (Hp < End) {
688         t = *Hp;                /* Get filter coeff */
689                                 /* t is now interp'd filter coeff */
690         ui_linear_remainder =
691           ((ui_output_rate * ui_counter + ui_remainder)<< Nhc) -
692           ((ui_output_rate * ui_counter + ui_remainder)<< Nhc) /
693           ui_input_rate * ui_input_rate;
694         t += *Hdp * ui_linear_remainder / ui_input_rate / Npc;
695         for( i = 0; i < i_nb_channels; i++ )
696         {
697             temp = t;
698             temp *= *(p_in+i);  /* Mult coeff by input sample */
699             *(p_out+i) += temp; /* The filter output */
700         }
701
702         ui_counter++;
703
704         /* Filter coeff step */
705         Hp = Imp + ((ui_output_rate * ui_counter + ui_remainder)<< Nhc)
706                     / ui_input_rate;
707         /* Filter coeff differences step */
708         Hdp = ImpD + ((ui_output_rate * ui_counter + ui_remainder)<< Nhc)
709                      / ui_input_rate;
710
711         p_in += (Inc * i_nb_channels); /* Input signal step */
712     }
713 }