53c446ca83d3f372fe3abd0923cb9115a400769b
[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 /*****************************************************************************
47  * Local structures
48  *****************************************************************************/
49 struct filter_sys_t
50 {
51     int32_t *p_prev_sample;       /* this filter introduces a 1 sample delay */
52
53     unsigned int i_remainder;                /* remainder of previous sample */
54
55     date_t       end_date;
56 };
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 vlc_module_begin ()
62     set_description( N_("Audio filter for linear interpolation resampling") )
63     set_category( CAT_AUDIO )
64     set_subcategory( SUBCAT_AUDIO_MISC )
65     set_capability( "audio filter2", 5 )
66     set_callbacks( OpenFilter, CloseFilter )
67 vlc_module_end ()
68
69 /*****************************************************************************
70  * Resample: convert a buffer
71  *****************************************************************************/
72 static block_t *Resample( filter_t *p_filter, block_t *p_in_buf )
73 {
74     if( !p_in_buf || !p_in_buf->i_nb_samples )
75     {
76         if( p_in_buf )
77             block_Release( p_in_buf );
78         return NULL;
79     }
80
81     filter_sys_t *p_sys = p_filter->p_sys;
82     unsigned i_nb_channels = p_filter->fmt_in.audio.i_channels;
83     float *p_prev_sample = (float *)p_sys->p_prev_sample;
84
85     /* Check if we really need to run the resampler */
86     if( p_filter->fmt_out.audio.i_rate == p_filter->fmt_in.audio.i_rate )
87     {
88         if( !(p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY) )
89         {
90             p_in_buf = block_Realloc( p_in_buf, sizeof(float) * i_nb_channels,
91                                       p_in_buf->i_buffer );
92             if( !p_in_buf )
93                 return NULL;
94
95             memcpy( p_in_buf->p_buffer, p_prev_sample,
96                     i_nb_channels * sizeof(float) );
97         }
98         return p_in_buf;
99     }
100
101     unsigned i_bytes_per_frame = p_filter->fmt_out.audio.i_channels *
102                                  p_filter->fmt_out.audio.i_bitspersample / 8;
103
104     size_t i_out_size = i_bytes_per_frame * (1 + (p_in_buf->i_nb_samples *
105               p_filter->fmt_out.audio.i_rate / p_filter->fmt_in.audio.i_rate));
106     block_t *p_out_buf = filter_NewAudioBuffer( p_filter, i_out_size );
107     if( !p_out_buf )
108         goto out;
109
110     float *p_out = (float *)p_out_buf->p_buffer;
111
112     unsigned i_in_nb = p_in_buf->i_nb_samples;
113     unsigned i_out = 0;
114     const float *p_in = (float *)p_in_buf->p_buffer;
115
116     /* Take care of the previous input sample (if any) */
117     if( p_in_buf->i_flags & BLOCK_FLAG_DISCONTINUITY )
118     {
119         p_out_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
120         p_sys->i_remainder = 0;
121         date_Init( &p_sys->end_date, p_filter->fmt_out.audio.i_rate, 1 );
122     }
123     else
124     {
125         while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate )
126         {
127             for( unsigned i_chan = i_nb_channels ; i_chan ; )
128             {
129                 i_chan--;
130                 p_out[i_chan] = p_prev_sample[i_chan];
131                 p_out[i_chan] += ( ( p_in[i_chan] - p_prev_sample[i_chan] )
132                                    * p_sys->i_remainder
133                                    / p_filter->fmt_out.audio.i_rate );
134             }
135             p_out += i_nb_channels;
136             i_out++;
137
138             p_sys->i_remainder += p_filter->fmt_in.audio.i_rate;
139         }
140         p_sys->i_remainder -= p_filter->fmt_out.audio.i_rate;
141     }
142
143     /* Take care of the current input samples (minus last one) */
144     for( unsigned i_in = 0; i_in < i_in_nb - 1; i_in++ )
145     {
146         while( p_sys->i_remainder < p_filter->fmt_out.audio.i_rate )
147         {
148             for( unsigned i_chan = i_nb_channels ; i_chan ; )
149             {
150                 i_chan--;
151                 p_out[i_chan] = p_in[i_chan];
152                 p_out[i_chan] += ( ( p_in[i_chan + i_nb_channels]
153                     - p_in[i_chan] )
154                     * p_sys->i_remainder / p_filter->fmt_out.audio.i_rate );
155             }
156             p_out += i_nb_channels;
157             i_out++;
158
159             p_sys->i_remainder += p_filter->fmt_in.audio.i_rate;
160         }
161
162         p_in += i_nb_channels;
163         p_sys->i_remainder -= p_filter->fmt_out.audio.i_rate;
164     }
165
166     /* Backup the last input sample for next time */
167     for( unsigned i_chan = i_nb_channels ; i_chan ; )
168     {
169         i_chan--;
170         p_prev_sample[i_chan] = p_in[i_chan];
171     }
172
173     p_out_buf->i_nb_samples = i_out;
174     p_out_buf->i_pts = p_in_buf->i_pts;
175
176     if( p_in_buf->i_pts !=
177         date_Get( &p_sys->end_date ) )
178     {
179         date_Set( &p_sys->end_date, p_in_buf->i_pts );
180     }
181
182     p_out_buf->i_length = date_Increment( &p_sys->end_date,
183                                   p_out_buf->i_nb_samples ) - p_out_buf->i_pts;
184
185     p_out_buf->i_buffer = p_out_buf->i_nb_samples *
186         i_nb_channels * sizeof(int32_t);
187 out:
188     block_Release( p_in_buf );
189     return p_out_buf;
190 }
191
192 /*****************************************************************************
193  * OpenFilter:
194  *****************************************************************************/
195 static int OpenFilter( vlc_object_t *p_this )
196 {
197     filter_t *p_filter = (filter_t *)p_this;
198     filter_sys_t *p_sys;
199     int i_out_rate  = p_filter->fmt_out.audio.i_rate;
200
201     if( p_filter->fmt_in.audio.i_rate == p_filter->fmt_out.audio.i_rate ||
202         p_filter->fmt_in.i_codec != VLC_CODEC_FL32 )
203     {
204         return VLC_EGENERIC;
205     }
206  
207     /* Allocate the memory needed to store the module's structure */
208     p_filter->p_sys = p_sys = malloc( sizeof(struct filter_sys_t) );
209     if( p_sys == NULL )
210         return VLC_ENOMEM;
211
212     p_sys->p_prev_sample = malloc(
213         p_filter->fmt_in.audio.i_channels * sizeof(int32_t) );
214     if( p_sys->p_prev_sample == NULL )
215     {
216         free( p_sys );
217         return VLC_ENOMEM;
218     }
219     date_Init( &p_sys->end_date, p_filter->fmt_in.audio.i_rate, 1 );
220     p_sys->i_remainder = 0;
221
222     p_filter->pf_audio_filter = Resample;
223
224     msg_Dbg( p_this, "%4.4s/%iKHz/%i->%4.4s/%iKHz/%i",
225              (char *)&p_filter->fmt_in.i_codec,
226              p_filter->fmt_in.audio.i_rate,
227              p_filter->fmt_in.audio.i_channels,
228              (char *)&p_filter->fmt_out.i_codec,
229              p_filter->fmt_out.audio.i_rate,
230              p_filter->fmt_out.audio.i_channels);
231
232     p_filter->fmt_out = p_filter->fmt_in;
233     p_filter->fmt_out.audio.i_rate = i_out_rate;
234
235     return 0;
236 }
237
238 /*****************************************************************************
239  * CloseFilter : deallocate data structures
240  *****************************************************************************/
241 static void CloseFilter( vlc_object_t *p_this )
242 {
243     filter_t *p_filter = (filter_t *)p_this;
244     free( p_filter->p_sys->p_prev_sample );
245     free( p_filter->p_sys );
246 }