]> git.sesse.net Git - vlc/blob - modules/audio_filter/resampler/linear.c
Improvements to preferences
[vlc] / modules / audio_filter / resampler / linear.c
1 /*****************************************************************************
2  * linear.c : linear interpolation resampler
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Sigmund Augdal <sigmunau@idi.ntnu.no>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include "audio_output.h"
33 #include "aout_internal.h"
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Create    ( vlc_object_t * );
39 static void Close     ( vlc_object_t * );
40 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
41                         aout_buffer_t * );
42
43 /*****************************************************************************
44  * Local structures
45  *****************************************************************************/
46 struct aout_filter_sys_t
47 {
48     int32_t *p_prev_sample;       /* this filter introduces a 1 sample delay */
49
50     unsigned int i_remainder;                /* remainder of previous sample */
51
52     audio_date_t end_date;
53 };
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58 vlc_module_begin();
59     set_description( _("audio filter for linear interpolation resampling") );
60     set_category( CAT_AUDIO );
61     set_subcategory( SUBCAT_AUDIO_MISC );
62     set_capability( "audio filter", 5 );
63     set_callbacks( Create, Close );
64 vlc_module_end();
65
66 /*****************************************************************************
67  * Create: allocate linear resampler
68  *****************************************************************************/
69 static int Create( vlc_object_t *p_this )
70 {
71     aout_filter_t * p_filter = (aout_filter_t *)p_this;
72     if ( p_filter->input.i_rate == p_filter->output.i_rate
73           || p_filter->input.i_format != p_filter->output.i_format
74           || p_filter->input.i_physical_channels
75               != p_filter->output.i_physical_channels
76           || p_filter->input.i_original_channels
77               != p_filter->output.i_original_channels
78           || p_filter->input.i_format != VLC_FOURCC('f','l','3','2') )
79     {
80         return VLC_EGENERIC;
81     }
82
83     /* Allocate the memory needed to store the module's structure */
84     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
85     if( p_filter->p_sys == NULL )
86     {
87         msg_Err( p_filter, "out of memory" );
88         return VLC_ENOMEM;
89     }
90     p_filter->p_sys->p_prev_sample = malloc(
91         aout_FormatNbChannels( &p_filter->input ) * sizeof(int32_t) );
92     if( p_filter->p_sys->p_prev_sample == NULL )
93     {
94         msg_Err( p_filter, "out of memory" );
95         return VLC_ENOMEM;
96     }
97
98     p_filter->pf_do_work = DoWork;
99
100     /* We don't want a new buffer to be created because we're not sure we'll
101      * actually need to resample anything. */
102     p_filter->b_in_place = VLC_TRUE;
103
104     return VLC_SUCCESS;
105 }
106
107 /*****************************************************************************
108  * Close: free our resources
109  *****************************************************************************/
110 static void Close( vlc_object_t * p_this )
111 {
112     aout_filter_t * p_filter = (aout_filter_t *)p_this;
113     free( p_filter->p_sys->p_prev_sample );
114     free( p_filter->p_sys );
115 }
116
117 /*****************************************************************************
118  * DoWork: convert a buffer
119  *****************************************************************************/
120 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
121                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
122 {
123     float *p_in_orig, *p_in, *p_out = (float *)p_out_buf->p_buffer;
124     float *p_prev_sample = (float *)p_filter->p_sys->p_prev_sample;
125
126     int i_nb_channels = aout_FormatNbChannels( &p_filter->input );
127     int i_in_nb = p_in_buf->i_nb_samples;
128     int i_chan, i_in, i_out = 0;
129
130     /* Check if we really need to run the resampler */
131     if( p_aout->mixer.mixer.i_rate == p_filter->input.i_rate )
132     {
133         if( p_filter->b_continuity &&
134             p_in_buf->i_size >=
135               p_in_buf->i_nb_bytes + sizeof(float) * i_nb_channels )
136         {
137             /* output the whole thing with the last sample from last time */
138             memmove( ((float *)(p_in_buf->p_buffer)) + i_nb_channels,
139                      p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
140             memcpy( p_in_buf->p_buffer, p_prev_sample,
141                     i_nb_channels * sizeof(float) );
142         }
143         p_filter->b_continuity = VLC_FALSE;
144         return;
145     }
146
147 #ifdef HAVE_ALLOCA
148     p_in = (float *)alloca( p_in_buf->i_nb_bytes );
149 #else
150     p_in_orig = p_in = (float *)malloc( p_in_buf->i_nb_bytes );
151 #endif
152     if( p_in == NULL )
153     {
154         return;
155     }
156
157     p_aout->p_vlc->pf_memcpy( p_in, p_in_buf->p_buffer, p_in_buf->i_nb_bytes );
158
159     /* Take care of the previous input sample (if any) */
160     if( !p_filter->b_continuity )
161     {
162         p_filter->b_continuity = VLC_TRUE;
163         p_filter->p_sys->i_remainder = 0;
164         aout_DateInit( &p_filter->p_sys->end_date, p_filter->output.i_rate );
165     }
166     else
167     {
168         while( p_filter->p_sys->i_remainder < p_filter->output.i_rate )
169         {
170             for( i_chan = i_nb_channels ; i_chan ; )
171             {
172                 i_chan--;
173                 p_out[i_chan] = p_prev_sample[i_chan];
174                 p_out[i_chan] += ( (p_prev_sample[i_chan] - p_in[i_chan])
175                                    * p_filter->p_sys->i_remainder
176                                    / p_filter->output.i_rate );
177             }
178             p_out += i_nb_channels;
179               i_out++;
180
181             p_filter->p_sys->i_remainder += p_filter->input.i_rate;
182         }
183         p_filter->p_sys->i_remainder -= p_filter->output.i_rate;
184     }
185
186     /* Take care of the current input samples (minus last one) */
187     for( i_in = 0; i_in < i_in_nb - 1; i_in++ )
188     {
189         while( p_filter->p_sys->i_remainder < p_filter->output.i_rate )
190         {
191             for( i_chan = i_nb_channels ; i_chan ; )
192             {
193                 i_chan--;
194                 p_out[i_chan] = p_in[i_chan];
195                 p_out[i_chan] += ( (p_in[i_chan] -
196                     p_in[i_chan + i_nb_channels])
197                     * p_filter->p_sys->i_remainder / p_filter->output.i_rate );
198             }
199             p_out += i_nb_channels;
200               i_out++;
201
202             p_filter->p_sys->i_remainder += p_filter->input.i_rate;
203         }
204
205         p_in += i_nb_channels;
206         p_filter->p_sys->i_remainder -= p_filter->output.i_rate;
207     }
208
209     /* Backup the last input sample for next time */
210     for( i_chan = i_nb_channels ; i_chan ; )
211     {
212         i_chan--;
213         p_prev_sample[i_chan] = p_in[i_chan];
214     }
215
216     p_out_buf->i_nb_samples = i_out;
217     p_out_buf->start_date = p_in_buf->start_date;
218
219     if( p_in_buf->start_date !=
220         aout_DateGet( &p_filter->p_sys->end_date ) )
221     {
222         aout_DateSet( &p_filter->p_sys->end_date, p_in_buf->start_date );
223     }
224
225     p_out_buf->end_date = aout_DateIncrement( &p_filter->p_sys->end_date,
226                                               p_out_buf->i_nb_samples );
227
228     p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples *
229         i_nb_channels * sizeof(int32_t);
230
231 #ifndef HAVE_ALLOCA
232     free( p_in_orig );
233 #endif
234
235 }