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