]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/simple.c
d9152048bb16998ba32b890126d50ecf2474e9e1
[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     int i_input_nb = aout_FormatNbChannels( &p_filter->input );
119     int i_output_nb = aout_FormatNbChannels( &p_filter->output );
120     float *p_dest = (float *)p_out_buf->p_buffer;
121     float *p_src = (float *)p_in_buf->p_buffer;
122     int i;
123
124     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
125     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
126
127     if( p_filter->output.i_physical_channels ==
128                             (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
129     {
130         if( p_filter->input.i_physical_channels & AOUT_CHAN_MIDDLELEFT )
131         for( i = p_in_buf->i_nb_samples; i--; )
132         {
133             *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4;
134             p_dest++;
135             *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4;
136             p_dest++;
137
138             p_src += 7;
139
140             if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
141         }
142         else
143         for( i = p_in_buf->i_nb_samples; i--; )
144         {
145             *p_dest = p_src[4] + 0.5 * p_src[0] + 0.33 * p_src[2];
146             p_dest++;
147             *p_dest = p_src[4] + 0.5 * p_src[1] + 0.33 * p_src[3];
148             p_dest++;
149
150             p_src += 5;
151
152             if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
153         }
154     }
155     else if( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
156     {
157         if( p_filter->input.i_physical_channels & AOUT_CHAN_MIDDLELEFT )
158         for( i = p_in_buf->i_nb_samples; i--; )
159         {
160             *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;
161             p_dest++;
162
163             p_src += 7;
164
165             if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
166         }
167         else if( p_filter->input.i_physical_channels & AOUT_CHAN_REARLEFT )
168         for( i = p_in_buf->i_nb_samples; i--; )
169         {
170             *p_dest = p_src[4] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 6 + p_src[3] / 6;
171             p_dest++;
172
173             p_src += 5;
174
175             if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
176         }
177         else
178         for( i = p_in_buf->i_nb_samples; i--; )
179         {
180             *p_dest = p_src[0] / 2 + p_src[1] / 2;
181             p_dest++;
182
183             p_src += 2;
184         }
185     }
186     else
187     {
188         if( p_filter->input.i_physical_channels & AOUT_CHAN_MIDDLELEFT )
189         for( i = p_in_buf->i_nb_samples; i--; )
190         {
191             *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6;
192             p_dest++;
193             *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
194             p_dest++;
195             *p_dest = p_src[2] / 6 +  p_src[4];
196             p_dest++;
197             *p_dest = p_src[3] / 6 +  p_src[5];
198             p_dest++;
199
200             p_src += 7;
201
202             if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
203         }
204         else
205         for( i = p_in_buf->i_nb_samples; i--; )
206         {
207             *p_dest = p_src[4] + 0.5 * p_src[0];
208             p_dest++;
209             *p_dest = p_src[4] + 0.5 * p_src[1];
210             p_dest++;
211             *p_dest = p_src[2];
212             p_dest++;
213             *p_dest = p_src[3];
214             p_dest++;
215
216             p_src += 5;
217
218             if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
219         }
220
221     }
222 }
223
224 /*****************************************************************************
225  * OpenFilter:
226  *****************************************************************************/
227 static int OpenFilter( vlc_object_t *p_this )
228 {
229     filter_t *p_filter = (filter_t *)p_this;
230
231     if ( (p_filter->fmt_in.audio.i_physical_channels
232            == p_filter->fmt_out.audio.i_physical_channels
233            && p_filter->fmt_in.audio.i_original_channels
234                == p_filter->fmt_out.audio.i_original_channels)
235           || p_filter->fmt_in.i_codec != p_filter->fmt_out.i_codec
236           || p_filter->fmt_in.audio.i_rate != p_filter->fmt_out.audio.i_rate
237           || p_filter->fmt_in.i_codec != VLC_FOURCC('f','l','3','2') )
238     {
239         return -1;
240     }
241
242     /* Only conversion to Mono, Stereo and 4.0 right now */
243     if( p_filter->fmt_out.audio.i_physical_channels !=
244         (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
245         && p_filter->fmt_out.audio.i_physical_channels !=
246         ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
247         AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)
248         && p_filter->fmt_out.audio.i_physical_channels != AOUT_CHAN_CENTER )
249     {
250         return -1;
251     }
252
253     /* Only from 7/7.1/5/5.1/2.0 */
254     if( (p_filter->fmt_in.audio.i_physical_channels & ~AOUT_CHAN_LFE) !=
255         (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
256          AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT) &&
257         (p_filter->fmt_in.audio.i_physical_channels & ~AOUT_CHAN_LFE) !=
258         (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
259          AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
260          AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT) &&
261         p_filter->fmt_in.audio.i_physical_channels !=
262         (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
263     {
264         return -1;
265     }
266
267     p_filter->pf_audio_filter = Filter;
268
269     return 0;
270 }
271
272 /*****************************************************************************
273  * Filter:
274  *****************************************************************************/
275 static block_t *Filter( filter_t *p_filter, block_t *p_block )
276 {
277     aout_filter_t aout_filter;
278     aout_buffer_t in_buf, out_buf;
279     block_t *p_out;
280     int i_out_size;
281
282     if( !p_block || !p_block->i_samples )
283     {
284         if( p_block ) p_block->pf_release( p_block );
285         return NULL;
286     }
287
288     i_out_size = p_block->i_samples *
289       p_filter->fmt_out.audio.i_bitspersample *
290         p_filter->fmt_out.audio.i_channels / 8;
291
292     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
293     if( !p_out )
294     {
295         msg_Warn( p_filter, "can't get output buffer" );
296         p_block->pf_release( p_block );
297         return NULL;
298     }
299
300     p_out->i_samples = p_block->i_samples;
301     p_out->i_dts = p_block->i_dts;
302     p_out->i_pts = p_block->i_pts;
303     p_out->i_length = p_block->i_length;
304
305     aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
306     aout_filter.input = p_filter->fmt_in.audio;
307     aout_filter.input.i_format = p_filter->fmt_in.i_codec;
308     aout_filter.output = p_filter->fmt_out.audio;
309     aout_filter.output.i_format = p_filter->fmt_out.i_codec;
310
311     in_buf.p_buffer = p_block->p_buffer;
312     in_buf.i_nb_bytes = p_block->i_buffer;
313     in_buf.i_nb_samples = p_block->i_samples;
314     out_buf.p_buffer = p_out->p_buffer;
315     out_buf.i_nb_bytes = p_out->i_buffer;
316     out_buf.i_nb_samples = p_out->i_samples;
317
318     DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
319
320     p_block->pf_release( p_block );
321
322     p_out->i_buffer = out_buf.i_nb_bytes;
323     p_out->i_samples = out_buf.i_nb_samples;
324
325     return p_out;
326 }
327