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