bandlimited resampler: fix heap overflow at high playback rate
[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     size_t 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     /* Finalize aout buffer */
364     p_out_buf->i_nb_samples = i_out;
365     p_out_buf->i_pts = date_Get( &p_sys->end_date );
366     p_out_buf->i_length = date_Increment( &p_sys->end_date,
367                                   p_out_buf->i_nb_samples ) - p_out_buf->i_pts;
368
369     p_out_buf->i_buffer = p_out_buf->i_nb_samples *
370         i_nb_channels * sizeof(int32_t);
371
372     /* Buffer i_filter_wing * 2 samples for next time */
373     if( p_sys->i_old_wing )
374     {
375         size_t newsize = p_sys->i_old_wing * 2
376                          * p_filter->fmt_in.audio.i_bytes_per_frame;
377         if( newsize > p_sys->i_buf_size )
378         {
379             free( p_sys->p_buf );
380             p_sys->p_buf = malloc( newsize );
381             if( p_sys->p_buf != NULL )
382                 p_sys->i_buf_size = newsize;
383             else
384             {
385                 p_sys->i_buf_size = p_sys->i_old_wing = 0; /* oops! */
386                 return p_out_buf;
387             }
388         }
389         memcpy( p_sys->p_buf,
390                 p_in_orig + (i_in_nb - 2 * p_sys->i_old_wing) *
391                 i_nb_channels, (2 * p_sys->i_old_wing) *
392                 p_filter->fmt_in.audio.i_bytes_per_frame );
393     }
394
395 #if 0
396     msg_Err( p_filter, "p_out size: %i, nb bytes out: %i", p_out_buf->i_buffer,
397              i_out * p_filter->fmt_in.audio.i_bytes_per_frame );
398 #endif
399
400     return p_out_buf;
401 }
402
403 /*****************************************************************************
404  * OpenFilter:
405  *****************************************************************************/
406 static int OpenFilter( vlc_object_t *p_this )
407 {
408     filter_t *p_filter = (filter_t *)p_this;
409     filter_sys_t *p_sys;
410     unsigned int i_out_rate  = p_filter->fmt_out.audio.i_rate;
411
412     if( p_filter->fmt_in.audio.i_rate == p_filter->fmt_out.audio.i_rate ||
413         p_filter->fmt_in.i_codec != VLC_CODEC_FL32 )
414     {
415         return VLC_EGENERIC;
416     }
417
418 #if !defined( SYS_DARWIN )
419     if( !config_GetInt( p_this, "hq-resampling" ) )
420     {
421         return VLC_EGENERIC;
422     }
423 #endif
424
425     /* Allocate the memory needed to store the module's structure */
426     p_filter->p_sys = p_sys = malloc( sizeof(struct filter_sys_t) );
427     if( p_sys == NULL )
428         return VLC_ENOMEM;
429
430     p_sys->p_buf = NULL;
431     p_sys->i_buf_size = 0;
432
433     p_sys->i_old_wing = 0;
434     p_sys->b_first = true;
435     p_filter->pf_audio_filter = Resample;
436
437     msg_Dbg( p_this, "%4.4s/%iKHz/%i->%4.4s/%iKHz/%i",
438              (char *)&p_filter->fmt_in.i_codec,
439              p_filter->fmt_in.audio.i_rate,
440              p_filter->fmt_in.audio.i_channels,
441              (char *)&p_filter->fmt_out.i_codec,
442              p_filter->fmt_out.audio.i_rate,
443              p_filter->fmt_out.audio.i_channels);
444
445     p_filter->fmt_out = p_filter->fmt_in;
446     p_filter->fmt_out.audio.i_rate = i_out_rate;
447
448     return 0;
449 }
450
451 /*****************************************************************************
452  * CloseFilter : deallocate data structures
453  *****************************************************************************/
454 static void CloseFilter( vlc_object_t *p_this )
455 {
456     filter_t *p_filter = (filter_t *)p_this;
457     free( p_filter->p_sys->p_buf );
458     free( p_filter->p_sys );
459 }
460
461 void FilterFloatUP( const float Imp[], const float ImpD[], uint16_t Nwing, float *p_in,
462                     float *p_out, uint32_t ui_remainder,
463                     uint32_t ui_output_rate, int16_t Inc, int i_nb_channels )
464 {
465     const float *Hp, *Hdp, *End;
466     float t, temp;
467     uint32_t ui_linear_remainder;
468     int i;
469
470     Hp = &Imp[(ui_remainder<<Nhc)/ui_output_rate];
471     Hdp = &ImpD[(ui_remainder<<Nhc)/ui_output_rate];
472
473     End = &Imp[Nwing];
474
475     ui_linear_remainder = (ui_remainder<<Nhc) -
476                             (ui_remainder<<Nhc)/ui_output_rate*ui_output_rate;
477
478     if (Inc == 1)               /* If doing right wing...              */
479     {                           /* ...drop extra coeff, so when Ph is  */
480         End--;                  /*    0.5, we don't do too many mult's */
481         if (ui_remainder == 0)  /* If the phase is zero...           */
482         {                       /* ...then we've already skipped the */
483             Hp += Npc;          /*    first sample, so we must also  */
484             Hdp += Npc;         /*    skip ahead in Imp[] and ImpD[] */
485         }
486     }
487
488     while (Hp < End) {
489         t = *Hp;                /* Get filter coeff */
490                                 /* t is now interp'd filter coeff */
491         t += *Hdp * ui_linear_remainder / ui_output_rate / Npc;
492         for( i = 0; i < i_nb_channels; i++ )
493         {
494             temp = t;
495             temp *= *(p_in+i);  /* Mult coeff by input sample */
496             *(p_out+i) += temp; /* The filter output */
497         }
498         Hdp += Npc;             /* Filter coeff differences step */
499         Hp += Npc;              /* Filter coeff step */
500         p_in += (Inc * i_nb_channels); /* Input signal step */
501     }
502 }
503
504 void FilterFloatUD( const float Imp[], const float ImpD[], uint16_t Nwing, float *p_in,
505                     float *p_out, uint32_t ui_remainder,
506                     uint32_t ui_output_rate, uint32_t ui_input_rate,
507                     int16_t Inc, int i_nb_channels )
508 {
509     const float *Hp, *Hdp, *End;
510     float t, temp;
511     uint32_t ui_linear_remainder;
512     int i, ui_counter = 0;
513
514     Hp = Imp + (ui_remainder<<Nhc) / ui_input_rate;
515     Hdp = ImpD  + (ui_remainder<<Nhc) / ui_input_rate;
516
517     End = &Imp[Nwing];
518
519     if (Inc == 1)               /* If doing right wing...              */
520     {                           /* ...drop extra coeff, so when Ph is  */
521         End--;                  /*    0.5, we don't do too many mult's */
522         if (ui_remainder == 0)  /* If the phase is zero...           */
523         {                       /* ...then we've already skipped the */
524             Hp = Imp +          /* first sample, so we must also  */
525                   (ui_output_rate << Nhc) / ui_input_rate;
526             Hdp = ImpD +        /* skip ahead in Imp[] and ImpD[] */
527                   (ui_output_rate << Nhc) / ui_input_rate;
528             ui_counter++;
529         }
530     }
531
532     while (Hp < End) {
533         t = *Hp;                /* Get filter coeff */
534                                 /* t is now interp'd filter coeff */
535         ui_linear_remainder =
536           ((ui_output_rate * ui_counter + ui_remainder)<< Nhc) -
537           ((ui_output_rate * ui_counter + ui_remainder)<< Nhc) /
538           ui_input_rate * ui_input_rate;
539         t += *Hdp * ui_linear_remainder / ui_input_rate / Npc;
540         for( i = 0; i < i_nb_channels; i++ )
541         {
542             temp = t;
543             temp *= *(p_in+i);  /* Mult coeff by input sample */
544             *(p_out+i) += temp; /* The filter output */
545         }
546
547         ui_counter++;
548
549         /* Filter coeff step */
550         Hp = Imp + ((ui_output_rate * ui_counter + ui_remainder)<< Nhc)
551                     / ui_input_rate;
552         /* Filter coeff differences step */
553         Hdp = ImpD + ((ui_output_rate * ui_counter + ui_remainder)<< Nhc)
554                      / ui_input_rate;
555
556         p_in += (Inc * i_nb_channels); /* Input signal step */
557     }
558 }