]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/simple.c
* simple.c: Extended the filter for mono output, and also converted it to the
[vlc] / modules / audio_filter / channel_mixer / simple.c
1 /*****************************************************************************
2  * simple.c : simple channel mixer plug-in (only 7/7.1/5/5.1 -> Stereo for now)
3  *****************************************************************************
4  * Copyright (C) 2002, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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 #include <vlc/vlc.h>
28 #include <vlc_aout.h>
29 #include <vlc_filter.h>
30 #include <vlc_block.h>
31
32 /*****************************************************************************
33  * Local prototypes
34  *****************************************************************************/
35 static int  Create    ( vlc_object_t * );
36
37 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
38                         aout_buffer_t * );
39
40 static int  OpenFilter ( vlc_object_t * );
41 static block_t *Filter( filter_t *, block_t * );
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 vlc_module_begin();
47     set_description( _("Audio filter for simple channel mixing") );
48     set_capability( "audio filter", 10 );
49     set_category( CAT_AUDIO );
50     set_subcategory( SUBCAT_AUDIO_MISC );
51     set_callbacks( Create, NULL );
52
53     add_submodule();
54     set_description( _("audio filter for simple channel mixing") );
55     set_capability( "audio filter2", 10 );
56     set_callbacks( OpenFilter, NULL );
57 vlc_module_end();
58
59 /*****************************************************************************
60  * Create: allocate trivial channel mixer
61  *****************************************************************************/
62 static int Create( vlc_object_t *p_this )
63 {
64     aout_filter_t * p_filter = (aout_filter_t *)p_this;
65
66     if ( (p_filter->input.i_physical_channels
67            == p_filter->output.i_physical_channels
68            && p_filter->input.i_original_channels
69                == p_filter->output.i_original_channels)
70           || p_filter->input.i_format != p_filter->output.i_format
71           || p_filter->input.i_rate != p_filter->output.i_rate
72           || p_filter->input.i_format != VLC_FOURCC('f','l','3','2') )
73     {
74         return -1;
75     }
76
77     /* Only conversion to Mono, Stereo and 4.0 right now */
78     if( p_filter->output.i_physical_channels !=
79         (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
80         && p_filter->output.i_physical_channels !=
81         ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
82         AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)
83         && p_filter->output.i_physical_channels != AOUT_CHAN_CENTER )
84     {
85         return -1;
86     }
87
88     /* Only from 7/7.1/5/5.1/2.0 */
89     if( (p_filter->input.i_physical_channels & ~AOUT_CHAN_LFE) !=
90         (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
91          AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT) &&
92         (p_filter->input.i_physical_channels & ~AOUT_CHAN_LFE) !=
93         (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
94          AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
95          AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT) &&
96         p_filter->input.i_physical_channels !=
97         (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
98     {
99         return -1;
100     }
101
102     p_filter->pf_do_work = DoWork;
103     p_filter->b_in_place = 0;
104
105     return 0;
106 }
107
108 /*****************************************************************************
109  * DoWork: convert a buffer
110  *****************************************************************************/
111 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
112                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
113 {
114     int i_input_nb = aout_FormatNbChannels( &p_filter->input );
115     int i_output_nb = aout_FormatNbChannels( &p_filter->output );
116     float *p_dest = (float *)p_out_buf->p_buffer;
117     float *p_src = (float *)p_in_buf->p_buffer;
118     int i;
119
120     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
121     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
122
123     if( p_filter->output.i_physical_channels ==
124                             (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
125     {
126         if( p_filter->input.i_physical_channels & AOUT_CHAN_MIDDLELEFT )
127         for( i = p_in_buf->i_nb_samples; i--; )
128         {
129             *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4;
130             p_dest++;
131             *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4;
132             p_dest++;
133
134             p_src += 7;
135
136             if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
137         }
138         else
139         for( i = p_in_buf->i_nb_samples; i--; )
140         {
141             *p_dest = p_src[4] + 0.5 * p_src[0] + 0.33 * p_src[2];
142             p_dest++;
143             *p_dest = p_src[4] + 0.5 * p_src[1] + 0.33 * p_src[3];
144             p_dest++;
145
146             p_src += 5;
147
148             if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
149         }
150     }
151     else if( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
152     {
153         if( p_filter->input.i_physical_channels & AOUT_CHAN_MIDDLELEFT )
154         for( i = p_in_buf->i_nb_samples; i--; )
155         {
156             *p_dest = p_src[6] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 8 + p_src[3] / 8 + p_src[4] / 8 + p_src[5] / 8;
157             p_dest++;
158
159             p_src += 7;
160
161             if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
162         }
163         else if( p_filter->input.i_physical_channels & AOUT_CHAN_REARLEFT )
164         for( i = p_in_buf->i_nb_samples; i--; )
165         {
166             *p_dest = p_src[4] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 6 + p_src[3] / 6;
167             p_dest++;
168
169             p_src += 5;
170
171             if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
172         }
173         else
174         for( i = p_in_buf->i_nb_samples; i--; )
175         {
176             *p_dest = p_src[0] / 2 + p_src[1] / 2;
177             p_dest++;
178
179             p_src += 2;
180         }
181     }
182     else
183     {
184         if( p_filter->input.i_physical_channels & AOUT_CHAN_MIDDLELEFT )
185         for( i = p_in_buf->i_nb_samples; i--; )
186         {
187             *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6;
188             p_dest++;
189             *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
190             p_dest++;
191             *p_dest = p_src[2] / 6 +  p_src[4];
192             p_dest++;
193             *p_dest = p_src[3] / 6 +  p_src[5];
194             p_dest++;
195
196             p_src += 7;
197
198             if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
199         }
200         else
201         for( i = p_in_buf->i_nb_samples; i--; )
202         {
203             *p_dest = p_src[4] + 0.5 * p_src[0];
204             p_dest++;
205             *p_dest = p_src[4] + 0.5 * p_src[1];
206             p_dest++;
207             *p_dest = p_src[2];
208             p_dest++;
209             *p_dest = p_src[3];
210             p_dest++;
211
212             p_src += 5;
213
214             if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
215         }
216
217     }
218 }
219
220 /*****************************************************************************
221  * OpenFilter:
222  *****************************************************************************/
223 static int OpenFilter( vlc_object_t *p_this )
224 {
225     filter_t *p_filter = (filter_t *)p_this;
226
227     if ( (p_filter->fmt_in.audio.i_physical_channels
228            == p_filter->fmt_out.audio.i_physical_channels
229            && p_filter->fmt_in.audio.i_original_channels
230                == p_filter->fmt_out.audio.i_original_channels)
231           || p_filter->fmt_in.i_codec != p_filter->fmt_out.i_codec
232           || p_filter->fmt_in.audio.i_rate != p_filter->fmt_out.audio.i_rate
233           || p_filter->fmt_in.i_codec != VLC_FOURCC('f','l','3','2') )
234     {
235         return -1;
236     }
237
238     /* Only conversion to Mono, Stereo and 4.0 right now */
239     if( p_filter->fmt_out.audio.i_physical_channels !=
240         (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
241         && p_filter->fmt_out.audio.i_physical_channels !=
242         ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
243         AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)
244         && p_filter->fmt_out.audio.i_physical_channels != AOUT_CHAN_CENTER )
245     {
246         return -1;
247     }
248
249     /* Only from 7/7.1/5/5.1/2.0 */
250     if( (p_filter->fmt_in.audio.i_physical_channels & ~AOUT_CHAN_LFE) !=
251         (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
252          AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT) &&
253         (p_filter->fmt_in.audio.i_physical_channels & ~AOUT_CHAN_LFE) !=
254         (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
255          AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
256          AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT) &&
257         p_filter->fmt_in.audio.i_physical_channels !=
258         (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
259     {
260         return -1;
261     }
262
263     p_filter->pf_audio_filter = Filter;
264
265     return 0;
266 }
267
268 /*****************************************************************************
269  * Filter:
270  *****************************************************************************/
271 static block_t *Filter( filter_t *p_filter, block_t *p_block )
272 {
273     aout_filter_t aout_filter;
274     aout_buffer_t in_buf, out_buf;
275     block_t *p_out;
276     int i_out_size;
277
278     if( !p_block || !p_block->i_samples )
279     {
280         if( p_block ) p_block->pf_release( p_block );
281         return NULL;
282     }
283
284     i_out_size = p_block->i_samples *
285       p_filter->fmt_out.audio.i_bitspersample *
286         p_filter->fmt_out.audio.i_channels / 8;
287
288     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
289     if( !p_out )
290     {
291         msg_Warn( p_filter, "can't get output buffer" );
292         p_block->pf_release( p_block );
293         return NULL;
294     }
295
296     p_out->i_samples = p_block->i_samples;
297     p_out->i_dts = p_block->i_dts;
298     p_out->i_pts = p_block->i_pts;
299     p_out->i_length = p_block->i_length;
300
301     aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
302     aout_filter.input = p_filter->fmt_in.audio;
303     aout_filter.input.i_format = p_filter->fmt_in.i_codec;
304     aout_filter.output = p_filter->fmt_out.audio;
305     aout_filter.output.i_format = p_filter->fmt_out.i_codec;
306
307     in_buf.p_buffer = p_block->p_buffer;
308     in_buf.i_nb_bytes = p_block->i_buffer;
309     in_buf.i_nb_samples = p_block->i_samples;
310     out_buf.p_buffer = p_out->p_buffer;
311     out_buf.i_nb_bytes = p_out->i_buffer;
312     out_buf.i_nb_samples = p_out->i_samples;
313
314     DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
315
316     p_block->pf_release( p_block );
317
318     p_out->i_buffer = out_buf.i_nb_bytes;
319     p_out->i_samples = out_buf.i_nb_samples;
320
321     return p_out;
322 }
323