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