* modules/audio_filter/resampler/bandlimited.c: small bug-fixes.
[vlc.git] / modules / audio_filter / resampler / bandlimited.c
1 /*****************************************************************************
2  * bandlimited.c : bandlimited interpolation resampler
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: bandlimited.c,v 1.3 2003/03/04 22:08:33 gbazin Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble:
26  *
27  * This implementation of the bandlimited 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 #include <stdlib.h>                                      /* malloc(), free() */
36 #include <string.h>
37
38 #include <vlc/vlc.h>
39 #include "audio_output.h"
40 #include "aout_internal.h"
41 #include "bandlimited.h"
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int  Create    ( vlc_object_t * );
47 static void Close     ( vlc_object_t * );
48 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
49                         aout_buffer_t * );
50
51 static void FilterFloatUP( float Imp[], float ImpD[], uint16_t Nwing,
52                            float *f_in, float *f_out, uint32_t ui_remainder,
53                            uint32_t ui_output_rate, int16_t Inc,
54                            int i_nb_channels );
55
56 static void FilterFloatUD( float Imp[], float ImpD[], uint16_t Nwing,
57                            float *f_in, float *f_out, uint32_t ui_remainder,
58                            uint32_t ui_output_rate, uint32_t ui_input_rate,
59                            int16_t Inc, int i_nb_channels );
60
61 /*****************************************************************************
62  * Local structures
63  *****************************************************************************/
64 struct aout_filter_sys_t
65 {
66     int32_t *p_buf;                        /* this filter introduces a delay */
67     int i_buf_size;
68
69     int i_old_rate;
70     double d_old_factor;
71     int i_old_wing;
72
73     unsigned int i_remainder;                /* remainder of previous sample */
74
75     audio_date_t end_date;
76 };
77
78 /*****************************************************************************
79  * Module descriptor
80  *****************************************************************************/
81 vlc_module_begin();
82     set_description( _("audio filter for bandlimited interpolation resampling") );
83     set_capability( "audio filter", 20 );
84     set_callbacks( Create, Close );
85 vlc_module_end();
86
87 /*****************************************************************************
88  * Create: allocate linear resampler
89  *****************************************************************************/
90 static int Create( vlc_object_t *p_this )
91 {
92     aout_filter_t * p_filter = (aout_filter_t *)p_this;
93     double d_factor;
94     int i_filter_wing;
95
96     if ( p_filter->input.i_rate == p_filter->output.i_rate
97           || p_filter->input.i_format != p_filter->output.i_format
98           || p_filter->input.i_physical_channels
99               != p_filter->output.i_physical_channels
100           || p_filter->input.i_original_channels
101               != p_filter->output.i_original_channels
102           || p_filter->input.i_format != VLC_FOURCC('f','l','3','2') )
103     {
104         return VLC_EGENERIC;
105     }
106
107     /* Allocate the memory needed to store the module's structure */
108     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
109     if( p_filter->p_sys == NULL )
110     {
111         msg_Err( p_filter, "out of memory" );
112         return VLC_ENOMEM;
113     }
114
115     /* Calculate worst case for the length of the filter wing */
116     d_factor = (double)p_filter->output.i_rate
117                         / p_filter->input.i_rate;
118     i_filter_wing = ((SMALL_FILTER_NMULT + 1)/2.0)
119                       * __MAX(1.0, 1.0/d_factor) + 10;
120     p_filter->p_sys->i_buf_size = aout_FormatNbChannels( &p_filter->input ) *
121         sizeof(int32_t) * 2 * i_filter_wing;
122
123     /* Allocate enough memory to buffer previous samples */
124     p_filter->p_sys->p_buf = malloc( p_filter->p_sys->i_buf_size );
125     if( p_filter->p_sys->p_buf == NULL )
126     {
127         msg_Err( p_filter, "out of memory" );
128         return VLC_ENOMEM;
129     }
130
131     p_filter->pf_do_work = DoWork;
132
133     /* We don't want a new buffer to be created because we're not sure we'll
134      * actually need to resample anything. */
135     p_filter->b_in_place = VLC_TRUE;
136
137     return VLC_SUCCESS;
138 }
139
140 /*****************************************************************************
141  * Close: free our resources
142  *****************************************************************************/
143 static void Close( vlc_object_t * p_this )
144 {
145     aout_filter_t * p_filter = (aout_filter_t *)p_this;
146     free( p_filter->p_sys->p_buf );
147     free( p_filter->p_sys );
148 }
149
150 /*****************************************************************************
151  * DoWork: convert a buffer
152  *****************************************************************************/
153 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
154                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
155 {
156     float *p_in, *p_in_orig, *p_out = (float *)p_out_buf->p_buffer;
157
158     int i_nb_channels = aout_FormatNbChannels( &p_filter->input );
159     int i_in_nb = p_in_buf->i_nb_samples;
160     int i_in, i_out = 0;
161     double d_factor, d_scale_factor, d_old_scale_factor;
162     int i_filter_wing;
163 #if 0
164     int i;
165 #endif
166
167     /* Check if we really need to run the resampler */
168     if( p_aout->mixer.mixer.i_rate == p_filter->input.i_rate )
169     {
170         if( p_filter->b_continuity &&
171             p_in_buf->i_size >=
172               p_in_buf->i_nb_bytes + sizeof(float) * i_nb_channels )
173         {
174             if( p_filter->p_sys->i_old_wing )
175             {
176                 /* output the whole thing with the samples from last time */
177                 memmove( ((float *)(p_in_buf->p_buffer)) +
178                          i_nb_channels * p_filter->p_sys->i_old_wing,
179                          p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
180                 memcpy( p_in_buf->p_buffer, p_filter->p_sys->p_buf +
181                         i_nb_channels * p_filter->p_sys->i_old_wing,
182                         p_filter->p_sys->i_old_wing *
183                         p_filter->input.i_bytes_per_frame );
184
185                 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples +
186                     p_filter->p_sys->i_old_wing;
187
188                 p_out_buf->end_date =
189                     aout_DateIncrement( &p_filter->p_sys->end_date,
190                                         p_out_buf->i_nb_samples );
191
192                 p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples *
193                     p_filter->input.i_bytes_per_frame;
194             }
195         }
196         p_filter->b_continuity = VLC_FALSE;
197         return;
198     }
199
200     if( !p_filter->b_continuity )
201     {
202         /* Continuity in sound samples has been broken, we'd better reset
203          * everything. */
204         p_filter->b_continuity = VLC_TRUE;
205         p_filter->p_sys->i_remainder = 0;
206         aout_DateInit( &p_filter->p_sys->end_date, p_filter->output.i_rate );
207         aout_DateSet( &p_filter->p_sys->end_date, p_in_buf->start_date );
208         p_filter->p_sys->i_old_rate   = p_filter->input.i_rate;
209         p_filter->p_sys->d_old_factor = 1;
210         p_filter->p_sys->i_old_wing   = 0;
211     }
212
213 #if 0
214     msg_Err( p_filter, "old rate: %i, old factor: %f, old wing: %i, i_in: %i",
215              p_filter->p_sys->i_old_rate, p_filter->p_sys->d_old_factor,
216              p_filter->p_sys->i_old_wing, i_in_nb );
217 #endif
218
219     /* Prepare the source buffer */
220     i_in_nb += (p_filter->p_sys->i_old_wing * 2);
221 #ifdef HAVE_ALLOCA
222     p_in = p_in_orig = (float *)alloca( i_in_nb *
223                                         p_filter->input.i_bytes_per_frame );
224 #else
225     p_in = p_in_orig = (float *)malloc( i_in_nb *
226                                         p_filter->input.i_bytes_per_frame );
227 #endif
228     if( p_in == NULL )
229     {
230         return;
231     }
232
233     /* Copy all our samples in p_in */
234     if( p_filter->p_sys->i_old_wing )
235     {
236         p_aout->p_vlc->pf_memcpy( p_in, p_filter->p_sys->p_buf,
237                                   p_filter->p_sys->i_old_wing * 2 *
238                                   p_filter->input.i_bytes_per_frame );
239     }
240     p_aout->p_vlc->pf_memcpy( p_in + p_filter->p_sys->i_old_wing * 2 *
241                               i_nb_channels, p_in_buf->p_buffer,
242                               p_in_buf->i_nb_samples *
243                               p_filter->input.i_bytes_per_frame );
244
245     /* Make sure the output buffer is reset */
246     memset( p_out, 0, p_out_buf->i_size );
247
248     /* Calculate the new length of the filter wing */
249     d_factor = (double)p_aout->mixer.mixer.i_rate / p_filter->input.i_rate;
250     i_filter_wing = ((SMALL_FILTER_NMULT+1)/2.0) * __MAX(1.0,1.0/d_factor) + 1;
251
252     /* Account for increased filter gain when using factors less than 1 */
253     d_old_scale_factor = SMALL_FILTER_SCALE *
254         p_filter->p_sys->d_old_factor + 0.5;
255     d_scale_factor = SMALL_FILTER_SCALE * d_factor + 0.5;
256
257     /* Apply the old rate until we have enough samples for the new one */
258     /* TODO: Check we have enough samples */
259     i_in = p_filter->p_sys->i_old_wing;
260     p_in += p_filter->p_sys->i_old_wing * i_nb_channels;
261     for( ; i_in < i_filter_wing; i_in++ )
262     {
263         if( p_filter->p_sys->d_old_factor == 1 )
264         {
265             /* Just copy the samples */
266             memcpy( p_out_buf->p_buffer, p_in, 
267                     p_filter->input.i_bytes_per_frame );          
268             p_in += i_nb_channels;
269             p_out += i_nb_channels;
270             i_out++;
271             continue;
272         }
273
274         while( p_filter->p_sys->i_remainder < p_filter->output.i_rate )
275         {
276
277             if( p_filter->p_sys->d_old_factor >= 1 )
278             {
279                 /* FilterFloatUP() is faster if we can use it */
280
281                 /* Perform left-wing inner product */
282                 FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
283                                SMALL_FILTER_NWING, p_in, p_out,
284                                p_filter->p_sys->i_remainder,
285                                p_filter->output.i_rate,
286                                -1, i_nb_channels );
287                 /* Perform right-wing inner product */
288                 FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
289                                SMALL_FILTER_NWING, p_in + i_nb_channels, p_out,
290                                p_filter->output.i_rate -
291                                p_filter->p_sys->i_remainder,
292                                p_filter->output.i_rate,
293                                1, i_nb_channels );
294
295 #if 0
296                 /* Normalize for unity filter gain */
297                 for( i = 0; i < i_nb_channels; i++ )
298                 {
299                     *(p_out+i) *= d_old_scale_factor;
300                 }
301 #endif
302             }
303             else
304             {
305                 /* Perform left-wing inner product */
306                 FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
307                                SMALL_FILTER_NWING, p_in, p_out,
308                                p_filter->p_sys->i_remainder,
309                                p_filter->output.i_rate, p_filter->input.i_rate,
310                                -1, i_nb_channels );
311                 /* Perform right-wing inner product */
312                 FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
313                                SMALL_FILTER_NWING, p_in + i_nb_channels, p_out,
314                                p_filter->output.i_rate -
315                                p_filter->p_sys->i_remainder,
316                                p_filter->output.i_rate, p_filter->input.i_rate,
317                                1, i_nb_channels );
318             }
319
320             p_out += i_nb_channels;
321             i_out++;
322
323             p_filter->p_sys->i_remainder += p_filter->input.i_rate;
324         }
325
326         p_in += i_nb_channels;
327         p_filter->p_sys->i_remainder -= p_filter->output.i_rate;
328     }
329
330     /* Apply the new rate for the rest of the samples */
331     /* TODO: Check we have enough future samples for the new rate */
332     if( i_in < i_in_nb - i_filter_wing )
333     {
334         p_filter->p_sys->i_old_rate   = p_filter->input.i_rate;
335         p_filter->p_sys->d_old_factor = d_factor;
336         p_filter->p_sys->i_old_wing   = i_filter_wing;
337     }
338     for( ; i_in < i_in_nb - i_filter_wing; i_in++ )
339     {
340         while( p_filter->p_sys->i_remainder < p_filter->output.i_rate )
341         {
342
343             if( d_factor >= 1 )
344             {
345                 /* FilterFloatUP() is faster if we can use it */
346
347                 /* Perform left-wing inner product */
348                 FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
349                                SMALL_FILTER_NWING, p_in, p_out,
350                                p_filter->p_sys->i_remainder,
351                                p_filter->output.i_rate,
352                                -1, i_nb_channels );
353
354                 /* Perform right-wing inner product */
355                 FilterFloatUP( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
356                                SMALL_FILTER_NWING, p_in + i_nb_channels, p_out,
357                                p_filter->output.i_rate -
358                                p_filter->p_sys->i_remainder,
359                                p_filter->output.i_rate,
360                                1, i_nb_channels );
361
362 #if 0
363                 /* Normalize for unity filter gain */
364                 for( i = 0; i < i_nb_channels; i++ )
365                 {
366                     *(p_out+i) *= d_old_scale_factor;
367                 }
368 #endif
369             }
370             else
371             {
372                 /* Perform left-wing inner product */
373                 FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
374                                SMALL_FILTER_NWING, p_in, p_out,
375                                p_filter->p_sys->i_remainder,
376                                p_filter->output.i_rate, p_filter->input.i_rate,
377                                -1, i_nb_channels );
378                 /* Perform right-wing inner product */
379                 FilterFloatUD( SMALL_FILTER_FLOAT_IMP, SMALL_FILTER_FLOAT_IMPD,
380                                SMALL_FILTER_NWING, p_in + i_nb_channels, p_out,
381                                p_filter->output.i_rate -
382                                p_filter->p_sys->i_remainder,
383                                p_filter->output.i_rate, p_filter->input.i_rate,
384                                1, i_nb_channels );
385             }
386
387             p_out += i_nb_channels;
388             i_out++;
389
390             p_filter->p_sys->i_remainder += p_filter->input.i_rate;
391         }
392
393         p_in += i_nb_channels;
394         p_filter->p_sys->i_remainder -= p_filter->output.i_rate;
395     }
396
397     /* Buffer i_filter_wing * 2 samples for next time */
398     if( p_filter->p_sys->i_old_wing )
399     {
400         memcpy( p_filter->p_sys->p_buf,
401                 p_in_orig + (i_in_nb - 2 * p_filter->p_sys->i_old_wing) *
402                 i_nb_channels, (2 * p_filter->p_sys->i_old_wing) *
403                 p_filter->input.i_bytes_per_frame );
404     }
405
406 #if 0
407     msg_Err( p_filter, "p_out size: %i, nb bytes out: %i", p_out_buf->i_size,
408              i_out * p_filter->input.i_bytes_per_frame );
409 #endif
410
411     /* Free the temp buffer */
412 #ifndef HAVE_ALLOCA
413     free( p_in_orig );
414 #endif
415
416     /* Finalize aout buffer */
417     p_out_buf->i_nb_samples = i_out;
418     p_out_buf->start_date = p_in_buf->start_date;
419
420     p_out_buf->end_date = aout_DateIncrement( &p_filter->p_sys->end_date,
421                                               p_out_buf->i_nb_samples );
422
423     p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples *
424         i_nb_channels * sizeof(int32_t);
425
426 }
427
428 void FilterFloatUP( float Imp[], float ImpD[], uint16_t Nwing, float *p_in,
429                     float *p_out, uint32_t ui_remainder,
430                     uint32_t ui_output_rate, int16_t Inc, int i_nb_channels )
431 {
432     float *Hp, *Hdp, *End;
433     float t, temp;
434     uint32_t ui_linear_remainder;
435     int i;
436
437     Hp = &Imp[(ui_remainder<<Nhc)/ui_output_rate];
438     Hdp = &ImpD[(ui_remainder<<Nhc)/ui_output_rate];
439
440     End = &Imp[Nwing];
441
442     ui_linear_remainder = (ui_remainder<<Nhc) -
443                             (ui_remainder<<Nhc)/ui_output_rate*ui_output_rate;
444
445     if (Inc == 1)               /* If doing right wing...              */
446     {                           /* ...drop extra coeff, so when Ph is  */
447         End--;                  /*    0.5, we don't do too many mult's */
448         if (ui_remainder == 0)  /* If the phase is zero...           */
449         {                       /* ...then we've already skipped the */
450             Hp += Npc;          /*    first sample, so we must also  */
451             Hdp += Npc;         /*    skip ahead in Imp[] and ImpD[] */
452         }
453     }
454
455     while (Hp < End) {
456         t = *Hp;                /* Get filter coeff */
457                                 /* t is now interp'd filter coeff */
458         t += *Hdp * ui_linear_remainder / ui_output_rate / Npc;
459         for( i = 0; i < i_nb_channels; i++ )
460         {
461             temp = t;
462             temp *= *(p_in+i);  /* Mult coeff by input sample */
463             *(p_out+i) += temp; /* The filter output */
464         }
465         Hdp += Npc;             /* Filter coeff differences step */
466         Hp += Npc;              /* Filter coeff step */
467         p_in += (Inc * i_nb_channels); /* Input signal step */
468     }
469 }
470
471 void FilterFloatUD( float Imp[], float ImpD[], uint16_t Nwing, float *p_in,
472                     float *p_out, uint32_t ui_remainder,
473                     uint32_t ui_output_rate, uint32_t ui_input_rate,
474                     int16_t Inc, int i_nb_channels )
475 {
476     float *Hp, *Hdp, *End;
477     float t, temp;
478     uint32_t ui_linear_remainder;
479     int i, ui_counter = 0;
480
481     Hp = Imp + (ui_remainder<<Nhc) / ui_input_rate;
482     Hdp = ImpD  + (ui_remainder<<Nhc) / ui_input_rate;
483
484     End = &Imp[Nwing];
485
486     if (Inc == 1)               /* If doing right wing...              */
487     {                           /* ...drop extra coeff, so when Ph is  */
488         End--;                  /*    0.5, we don't do too many mult's */
489         if (ui_remainder == 0)  /* If the phase is zero...           */
490         {                       /* ...then we've already skipped the */
491             Hp = Imp +          /* first sample, so we must also  */
492                   (ui_output_rate << Nhc) / ui_input_rate;
493             Hdp = ImpD +        /* skip ahead in Imp[] and ImpD[] */
494                   (ui_output_rate << Nhc) / ui_input_rate;
495             ui_counter++;
496         }
497     }
498
499     while (Hp < End) {
500         t = *Hp;                /* Get filter coeff */
501                                 /* t is now interp'd filter coeff */
502         ui_linear_remainder =
503           ((ui_output_rate * ui_counter + ui_remainder)<< Nhc) -
504           ((ui_output_rate * ui_counter + ui_remainder)<< Nhc) /
505           ui_input_rate * ui_input_rate;
506         t += *Hdp * ui_linear_remainder / ui_input_rate / Npc;
507         for( i = 0; i < i_nb_channels; i++ )
508         {
509             temp = t;
510             temp *= *(p_in+i);  /* Mult coeff by input sample */
511             *(p_out+i) += temp; /* The filter output */
512         }
513
514         ui_counter++;
515
516         /* Filter coeff step */
517         Hp = Imp + ((ui_output_rate * ui_counter + ui_remainder)<< Nhc)
518                     / ui_input_rate;
519         /* Filter coeff differences step */
520         Hdp = ImpD + ((ui_output_rate * ui_counter + ui_remainder)<< Nhc)
521                      / ui_input_rate;
522
523         p_in += (Inc * i_nb_channels); /* Input signal step */
524     }
525 }