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