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