601ec8517876a81d5f55d11998bae29acc6a52e6
[vlc.git] / modules / audio_filter / resampler / linear.c
1 /*****************************************************************************
2  * linear.c : linear interpolation resampler
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: linear.c,v 1.8 2003/02/16 01:29:40 massiot Exp $
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_capability( "audio filter", 10 );
61     set_callbacks( Create, Close );
62 vlc_module_end();
63
64 /*****************************************************************************
65  * Create: allocate linear resampler
66  *****************************************************************************/
67 static int Create( vlc_object_t *p_this )
68 {
69     aout_filter_t * p_filter = (aout_filter_t *)p_this;
70     if ( p_filter->input.i_rate == p_filter->output.i_rate
71           || p_filter->input.i_format != p_filter->output.i_format
72           || p_filter->input.i_physical_channels
73               != p_filter->output.i_physical_channels
74           || p_filter->input.i_original_channels
75               != p_filter->output.i_original_channels
76           || p_filter->input.i_format != VLC_FOURCC('f','l','3','2') )
77     {
78         return VLC_EGENERIC;
79     }
80
81     /* Allocate the memory needed to store the module's structure */
82     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
83     if( p_filter->p_sys == NULL )
84     {
85         msg_Err( p_filter, "out of memory" );
86         return VLC_ENOMEM;
87     }
88     p_filter->p_sys->p_prev_sample = malloc(
89         aout_FormatNbChannels( &p_filter->input ) * sizeof(int32_t) );
90     if( p_filter->p_sys->p_prev_sample == NULL )
91     {
92         msg_Err( p_filter, "out of memory" );
93         return VLC_ENOMEM;
94     }
95
96     p_filter->pf_do_work = DoWork;
97     p_filter->b_in_place = VLC_FALSE;
98
99     return VLC_SUCCESS;
100 }
101
102 /*****************************************************************************
103  * Close: free our resources
104  *****************************************************************************/
105 static void Close( vlc_object_t * p_this )
106 {
107     aout_filter_t * p_filter = (aout_filter_t *)p_this;
108     free( p_filter->p_sys->p_prev_sample );
109     free( p_filter->p_sys );
110 }
111
112 /*****************************************************************************
113  * DoWork: convert a buffer
114  *****************************************************************************/
115 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
116                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
117 {
118     float* p_in = (float*)p_in_buf->p_buffer;
119     float* p_out = (float*)p_out_buf->p_buffer;
120     float* p_prev_sample = (float*)p_filter->p_sys->p_prev_sample;
121
122     int i_nb_channels = aout_FormatNbChannels( &p_filter->input );
123     int i_in_nb = p_in_buf->i_nb_samples;
124     int i_chan, i_in, i_out = 0;
125
126     /* Take care of the previous input sample (if any) */
127     if( p_filter->b_reinit )
128     {
129         p_filter->b_reinit = VLC_FALSE;
130         p_filter->p_sys->i_remainder = 0;
131         aout_DateInit( &p_filter->p_sys->end_date, p_filter->output.i_rate );
132     }
133     else
134     {
135         while( p_filter->p_sys->i_remainder < p_filter->output.i_rate )
136         {
137             for( i_chan = i_nb_channels ; i_chan ; )
138             {
139                 i_chan--;
140                 p_out[i_chan] = p_prev_sample[i_chan];
141                 p_out[i_chan] += ( (p_prev_sample[i_chan] - p_in[i_chan])
142                                    * p_filter->p_sys->i_remainder
143                                    / p_filter->output.i_rate );
144             }
145             p_out += i_nb_channels;
146             i_out++;
147
148             p_filter->p_sys->i_remainder += p_filter->input.i_rate;
149         }
150         p_filter->p_sys->i_remainder -= p_filter->output.i_rate;
151     }
152
153     /* Take care of the current input samples (minus last one) */
154     for( i_in = 0; i_in < i_in_nb - 1; i_in++ )
155     {
156         while( p_filter->p_sys->i_remainder < p_filter->output.i_rate )
157         {
158             for( i_chan = i_nb_channels ; i_chan ; )
159             {
160                 i_chan--;
161                 p_out[i_chan] = p_in[i_chan];
162                 p_out[i_chan] += ( (p_in[i_chan] -
163                     p_in[i_chan + i_nb_channels])
164                     * p_filter->p_sys->i_remainder / p_filter->output.i_rate );
165             }
166             p_out += i_nb_channels;
167             i_out++;
168
169             p_filter->p_sys->i_remainder += p_filter->input.i_rate;
170         }
171
172         p_in += i_nb_channels;
173         p_filter->p_sys->i_remainder -= p_filter->output.i_rate;
174     }
175
176     /* Backup the last input sample for next time */
177     for( i_chan = i_nb_channels ; i_chan ; )
178     {
179         i_chan--;
180         p_prev_sample[i_chan] = p_in[i_chan];
181     }
182
183     p_out_buf->i_nb_samples = i_out;
184     p_out_buf->start_date = p_in_buf->start_date;
185
186     if( p_in_buf->start_date !=
187         aout_DateGet( &p_filter->p_sys->end_date ) )
188     {
189         aout_DateSet( &p_filter->p_sys->end_date, p_in_buf->start_date );
190     }
191
192     p_out_buf->end_date = aout_DateIncrement( &p_filter->p_sys->end_date,
193                                               p_out_buf->i_nb_samples );
194
195     p_out_buf->i_nb_bytes = p_out_buf->i_nb_samples *
196         i_nb_channels * sizeof(int32_t);
197
198 }