linear resampler: cosmetics (no functional changes)
[vlc.git] / modules / audio_filter / resampler / linear.c
1 /*****************************************************************************
2  * linear.c : linear interpolation resampler
3  *****************************************************************************
4  * Copyright (C) 2002, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Sigmund Augdal Helberg <dnumgis@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_aout.h>
36 #include <vlc_filter.h>
37 #include <vlc_block.h>
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  OpenFilter ( vlc_object_t * );
43 static void CloseFilter( vlc_object_t * );
44 static block_t *Resample( filter_t *, block_t * );
45
46 typedef float sample_t;
47
48 /*****************************************************************************
49  * Local structures
50  *****************************************************************************/
51 struct filter_sys_t
52 {
53     sample_t *p_prev_sample;      /* this filter introduces a 1 sample delay */
54
55     unsigned int i_remainder;                /* remainder of previous sample */
56
57     date_t       end_date;
58 };
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 vlc_module_begin ()
64     set_description( N_("Audio filter for linear interpolation resampling") )
65     set_category( CAT_AUDIO )
66     set_subcategory( SUBCAT_AUDIO_MISC )
67     set_capability( "audio filter2", 5 )
68     set_callbacks( OpenFilter, CloseFilter )
69 vlc_module_end ()
70
71 /*****************************************************************************
72  * Resample: convert a buffer
73  *****************************************************************************/
74 static block_t *Resample( filter_t *p_filter, block_t *p_in_buf )
75 {
76     if( !p_in_buf || !p_in_buf->i_nb_samples )
77     {
78         if( p_in_buf )
79             block_Release( p_in_buf );
80         return NULL;
81     }
82
83     filter_sys_t *p_sys = p_filter->p_sys;
84     unsigned i_nb_channels = p_filter->fmt_in.audio.i_channels;
85     sample_t *p_prev_sample = p_sys->p_prev_sample;
86
87     /* Check if we really need to run the resampler */
88     if( p_filter->fmt_out.audio.i_rate == p_filter->fmt_in.audio.i_rate )
89     {
90         if( !(p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY) )
91         {
92             p_in_buf = block_Realloc( p_in_buf,
93                                       sizeof(sample_t) * i_nb_channels,
94                                       p_in_buf->i_buffer );
95             if( !p_in_buf )
96                 return NULL;
97
98             memcpy( p_in_buf->p_buffer, p_prev_sample,
99                     i_nb_channels * sizeof(sample_t) );
100         }
101         return p_in_buf;
102     }
103
104     unsigned i_bytes_per_frame = p_filter->fmt_out.audio.i_channels *
105                                  p_filter->fmt_out.audio.i_bitspersample / 8;
106
107     size_t i_out_size = i_bytes_per_frame * (1 + (p_in_buf->i_nb_samples *
108               p_filter->fmt_out.audio.i_rate / p_filter->fmt_in.audio.i_rate));
109     block_t *p_out_buf = filter_NewAudioBuffer( p_filter, i_out_size );
110     if( !p_out_buf )
111         goto out;
112
113     sample_t *p_out = (sample_t *)p_out_buf->p_buffer;
114
115     unsigned i_in_nb = p_in_buf->i_nb_samples;
116     unsigned i_out = 0;
117     const sample_t *p_in = (sample_t *)p_in_buf->p_buffer;
118
119     /* Take care of the previous input sample (if any) */
120     if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
121     {
122         p_out_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
123         p_sys->i_remainder = 0;
124         date_Init( &p_sys->end_date, p_filter->fmt_out.audio.i_rate, 1 );
125     }
126     else
127     {
128         while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate )
129         {
130             for( unsigned i = 0; i < i_nb_channels ; i++ )
131             {
132                 p_out[i] = p_prev_sample[i];
133                 p_out[i] += (p_in[i] - p_prev_sample[i])
134                     * p_sys->i_remainder / p_filter->fmt_out.audio.i_rate;
135             }
136             p_out += i_nb_channels;
137             i_out++;
138
139             p_sys->i_remainder += p_filter->fmt_in.audio.i_rate;
140         }
141         p_sys->i_remainder -= p_filter->fmt_out.audio.i_rate;
142     }
143
144     /* Take care of the current input samples (minus last one) */
145     for( unsigned i_in = 0; i_in < i_in_nb - 1; i_in++ )
146     {
147         while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate )
148         {
149             for( unsigned i = 0; i < i_nb_channels ; i++ )
150             {
151                 p_out[i] = p_in[i];
152                 p_out[i] += (p_in[i + i_nb_channels] - p_in[i])
153                     * p_sys->i_remainder / p_filter->fmt_out.audio.i_rate;
154             }
155             p_out += i_nb_channels;
156             i_out++;
157
158             p_sys->i_remainder += p_filter->fmt_in.audio.i_rate;
159         }
160
161         p_in += i_nb_channels;
162         p_sys->i_remainder -= p_filter->fmt_out.audio.i_rate;
163     }
164
165     /* Backup the last input sample for next time */
166     memcpy( p_prev_sample, p_in, i_nb_channels * sizeof(sample_t) );
167
168     p_out_buf->i_nb_samples = i_out;
169     p_out_buf->i_pts = p_in_buf->i_pts;
170
171     if( p_in_buf->i_pts !=
172         date_Get( &p_sys->end_date ) )
173     {
174         date_Set( &p_sys->end_date, p_in_buf->i_pts );
175     }
176
177     p_out_buf->i_length = date_Increment( &p_sys->end_date,
178                                   p_out_buf->i_nb_samples ) - p_out_buf->i_pts;
179
180     p_out_buf->i_buffer = p_out_buf->i_nb_samples *
181         i_nb_channels * sizeof(sample_t);
182 out:
183     block_Release( p_in_buf );
184     return p_out_buf;
185 }
186
187 /*****************************************************************************
188  * OpenFilter:
189  *****************************************************************************/
190 static int OpenFilter( vlc_object_t *p_this )
191 {
192     filter_t *p_filter = (filter_t *)p_this;
193     filter_sys_t *p_sys;
194     int i_out_rate  = p_filter->fmt_out.audio.i_rate;
195
196     if( p_filter->fmt_in.audio.i_rate == p_filter->fmt_out.audio.i_rate ||
197         p_filter->fmt_in.i_codec != VLC_CODEC_FL32 )
198     {
199         return VLC_EGENERIC;
200     }
201  
202     /* Allocate the memory needed to store the module's structure */
203     p_filter->p_sys = p_sys = malloc( sizeof(struct filter_sys_t) );
204     if( p_sys == NULL )
205         return VLC_ENOMEM;
206
207     p_sys->p_prev_sample = malloc(
208         p_filter->fmt_in.audio.i_channels * sizeof(sample_t) );
209     if( p_sys->p_prev_sample == NULL )
210     {
211         free( p_sys );
212         return VLC_ENOMEM;
213     }
214     date_Init( &p_sys->end_date, p_filter->fmt_in.audio.i_rate, 1 );
215     p_sys->i_remainder = 0;
216
217     p_filter->pf_audio_filter = Resample;
218
219     msg_Dbg( p_this, "%4.4s/%iKHz/%i->%4.4s/%iKHz/%i",
220              (char *)&p_filter->fmt_in.i_codec,
221              p_filter->fmt_in.audio.i_rate,
222              p_filter->fmt_in.audio.i_channels,
223              (char *)&p_filter->fmt_out.i_codec,
224              p_filter->fmt_out.audio.i_rate,
225              p_filter->fmt_out.audio.i_channels);
226
227     p_filter->fmt_out = p_filter->fmt_in;
228     p_filter->fmt_out.audio.i_rate = i_out_rate;
229
230     return 0;
231 }
232
233 /*****************************************************************************
234  * CloseFilter : deallocate data structures
235  *****************************************************************************/
236 static void CloseFilter( vlc_object_t *p_this )
237 {
238     filter_t *p_filter = (filter_t *)p_this;
239     free( p_filter->p_sys->p_prev_sample );
240     free( p_filter->p_sys );
241 }