]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/simple.c
627a4893e10f98fe404100b8bd4af87b2256656f
[vlc] / modules / audio_filter / channel_mixer / simple.c
1 /*****************************************************************************
2  * simple.c : simple channel mixer plug-in
3  *****************************************************************************
4  * Copyright (C) 2002, 2004, 2006-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_aout.h>
34 #include <vlc_filter.h>
35 #include <vlc_block.h>
36 #include <assert.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  OpenFilter( vlc_object_t * );
42 static void CloseFilter( vlc_object_t * );
43
44 vlc_module_begin ()
45     set_description( N_("Audio filter for simple channel mixing") )
46     set_category( CAT_AUDIO )
47     set_subcategory( SUBCAT_AUDIO_MISC )
48     set_capability( "audio converter", 10 )
49     set_callbacks( OpenFilter, CloseFilter );
50 vlc_module_end ()
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 struct filter_sys_t
56 {
57     void (*pf_dowork)(filter_t *, block_t *, block_t * );
58 };
59
60 /*****************************************************************************
61  * IsSupported: can we downmix?
62  *****************************************************************************/
63 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output )
64 {
65     if( p_input->i_format != VLC_CODEC_FL32 ||
66         p_input->i_format != p_output->i_format ||
67         p_input->i_rate != p_output->i_rate )
68     {
69         return false;
70     }
71
72     if( p_input->i_physical_channels == p_output->i_physical_channels &&
73         p_input->i_original_channels == p_output->i_original_channels )
74     {
75         return false;
76     }
77
78     /* Only conversion to Mono, Stereo, 4.0 and 5.1 */
79     if( p_output->i_physical_channels != AOUT_CHAN_CENTER &&
80         p_output->i_physical_channels != AOUT_CHANS_2_0 &&
81         p_output->i_physical_channels != AOUT_CHANS_4_0 &&
82         p_output->i_physical_channels != AOUT_CHANS_5_1 )
83     {
84         return false;
85     }
86
87     /* Only from 7.x/5.x/4.0/3.x/2.0
88      * NB 5.X rear and middle are handled the same way
89      * We don't support 2.1 -> 2.0 (trivial can do it)
90      * TODO: We don't support any 8.1 input
91      * TODO: We don't support any 6.x input
92      * TODO: We don't support 4.0 rear and 4.0 middle
93      * */
94     if( (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_7_0 &&
95         (p_input->i_physical_channels)                  != AOUT_CHANS_6_1_MIDDLE &&
96         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0 &&
97         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0_MIDDLE &&
98         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_4_CENTER_REAR &&
99         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_3_0 &&
100          p_input->i_physical_channels != AOUT_CHANS_2_0 )
101     {
102         return false;
103     }
104
105     /* Only downmixing */
106     if( aout_FormatNbChannels( p_input ) <= aout_FormatNbChannels( p_output ) )
107         return false;
108
109     return true;
110 }
111
112 static block_t *Filter( filter_t *, block_t * );
113
114 static void DoWork_7_x_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
115     float *p_dest = (float *)p_out_buf->p_buffer;
116     const float *p_src = (const float *)p_in_buf->p_buffer;
117     for( int i = p_in_buf->i_nb_samples; i--; )
118     {
119         *p_dest++ = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4;
120         *p_dest++ = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4;
121
122         p_src += 7;
123
124         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
125     }
126 }
127
128 static void DoWork_5_x_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
129     float *p_dest = (float *)p_out_buf->p_buffer;
130     const float *p_src = (const float *)p_in_buf->p_buffer;
131     for( int i = p_in_buf->i_nb_samples; i--; )
132     {
133         *p_dest++ = p_src[0] + 0.7071 * (p_src[4] + p_src[2]);
134         *p_dest++ = p_src[1] + 0.7071 * (p_src[4] + p_src[3]);
135
136         p_src += 5;
137
138         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
139     }
140 }
141
142 static void DoWork_4_0_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
143     VLC_UNUSED(p_filter);
144     float *p_dest = (float *)p_out_buf->p_buffer;
145     const float *p_src = (const float *)p_in_buf->p_buffer;
146     for( int i = p_in_buf->i_nb_samples; i--; )
147     {
148         *p_dest++ = p_src[2] + p_src[3] + 0.5 * p_src[0];
149         *p_dest++ = p_src[2] + p_src[3] + 0.5 * p_src[1];
150         p_src += 4;
151     }
152 }
153
154 static void DoWork_3_x_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
155     float *p_dest = (float *)p_out_buf->p_buffer;
156     const float *p_src = (const float *)p_in_buf->p_buffer;
157     for( int i = p_in_buf->i_nb_samples; i--; )
158     {
159         *p_dest++ = p_src[2] + 0.5 * p_src[0];
160         *p_dest++ = p_src[2] + 0.5 * p_src[1];
161
162         p_src += 3;
163
164         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
165     }
166 }
167
168 static void DoWork_7_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
169     float *p_dest = (float *)p_out_buf->p_buffer;
170     const float *p_src = (const float *)p_in_buf->p_buffer;
171     for( int i = p_in_buf->i_nb_samples; i--; )
172     {
173         *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;
174
175         p_src += 7;
176
177         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
178     }
179 }
180
181 static void DoWork_5_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
182     float *p_dest = (float *)p_out_buf->p_buffer;
183     const float *p_src = (const float *)p_in_buf->p_buffer;
184     for( int i = p_in_buf->i_nb_samples; i--; )
185     {
186         *p_dest++ = 0.7071 * (p_src[0] + p_src[1]) + p_src[4] + 0.5f * (p_src[2] + p_src[3]);
187
188         p_src += 5;
189
190         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
191     }
192 }
193
194 static void DoWork_4_0_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
195     VLC_UNUSED(p_filter);
196     float *p_dest = (float *)p_out_buf->p_buffer;
197     const float *p_src = (const float *)p_in_buf->p_buffer;
198     for( int i = p_in_buf->i_nb_samples; i--; )
199     {
200         *p_dest++ = p_src[2] + p_src[3] + p_src[0] / 4 + p_src[1] / 4;
201         p_src += 4;
202     }
203 }
204
205 static void DoWork_3_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
206     float *p_dest = (float *)p_out_buf->p_buffer;
207     const float *p_src = (const float *)p_in_buf->p_buffer;
208     for( int i = p_in_buf->i_nb_samples; i--; )
209     {
210         *p_dest++ = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
211
212         p_src += 3;
213
214         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
215     }
216 }
217
218 static void DoWork_2_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
219     VLC_UNUSED(p_filter);
220     float *p_dest = (float *)p_out_buf->p_buffer;
221     const float *p_src = (const float *)p_in_buf->p_buffer;
222     for( int i = p_in_buf->i_nb_samples; i--; )
223     {
224         *p_dest++ = p_src[0] / 2 + p_src[1] / 2;
225
226         p_src += 2;
227     }
228 }
229
230 static void DoWork_7_x_to_4_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
231     float *p_dest = (float *)p_out_buf->p_buffer;
232     const float *p_src = (const float *)p_in_buf->p_buffer;
233     for( int i = p_in_buf->i_nb_samples; i--; )
234     {
235         *p_dest++ = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6;
236         *p_dest++ = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
237         *p_dest++ = p_src[2] / 6 +  p_src[4];
238         *p_dest++ = p_src[3] / 6 +  p_src[5];
239
240         p_src += 7;
241
242         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
243     }
244 }
245
246 static void DoWork_5_x_to_4_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
247     float *p_dest = (float *)p_out_buf->p_buffer;
248     const float *p_src = (const float *)p_in_buf->p_buffer;
249     for( int i = p_in_buf->i_nb_samples; i--; )
250     {
251         *p_dest++ = p_src[0] + p_src[4] * 0.7071;
252         *p_dest++ = p_src[1] + p_src[4] * 0.7071;
253         *p_dest++ = p_src[2];
254         *p_dest++ = p_src[3];
255
256         p_src += 5;
257
258         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
259     }
260 }
261
262 static void DoWork_7_x_to_5_x( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
263     float *p_dest = (float *)p_out_buf->p_buffer;
264     const float *p_src = (const float *)p_in_buf->p_buffer;
265     for( int i = p_in_buf->i_nb_samples; i--; )
266     {
267         *p_dest++ = p_src[0];
268         *p_dest++ = p_src[1];
269         *p_dest++ = (p_src[2] + p_src[4]) * 0.5;
270         *p_dest++ = (p_src[3] + p_src[5]) * 0.5;
271         *p_dest++ = p_src[6];
272
273         p_src += 7;
274
275         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE &&
276             p_filter->fmt_out.audio.i_physical_channels & AOUT_CHAN_LFE )
277             *p_dest++ = *p_src++;
278         else if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
279     }
280 }
281
282 static void DoWork_6_1_to_5_x( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
283     float *p_dest = (float *)p_out_buf->p_buffer;
284     const float *p_src = (const float *)p_in_buf->p_buffer;
285     for( int i = p_in_buf->i_nb_samples; i--; )
286     {
287         *p_dest++ = p_src[0];
288         *p_dest++ = p_src[1];
289         *p_dest++ = (p_src[2] + p_src[4]) * 0.5;
290         *p_dest++ = (p_src[3] + p_src[4]) * 0.5;
291         *p_dest++ = p_src[5];
292
293         p_src += 6;
294
295         /* We always have LFE here */
296         *p_dest++ = *p_src++;
297     }
298 }
299
300
301 /*****************************************************************************
302  * OpenFilter:
303  *****************************************************************************/
304 static int OpenFilter( vlc_object_t *p_this )
305 {
306     filter_t *p_filter = (filter_t *)p_this;
307     filter_sys_t *p_sys;
308
309     audio_format_t fmt_in  = p_filter->fmt_in.audio;
310     audio_format_t fmt_out = p_filter->fmt_out.audio;
311
312     fmt_in.i_format = p_filter->fmt_in.i_codec;
313     fmt_out.i_format = p_filter->fmt_out.i_codec;
314
315     if( !IsSupported( &fmt_in, &fmt_out ) )
316         return VLC_EGENERIC;
317
318     p_filter->p_sys = malloc( sizeof(*p_sys) );
319     if( unlikely(!p_filter->p_sys) )
320         return VLC_ENOMEM;
321
322     p_filter->pf_audio_filter = Filter;
323
324     const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels;
325     const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0;
326     const bool b_input_6_1 = !b_input_7_0 &&
327                              i_input_physical == AOUT_CHANS_6_1_MIDDLE;
328     const bool b_input_5_0 = !b_input_7_0 && !b_input_6_1 &&
329                              ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 ||
330                                (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE );
331     const bool b_input_4_center_rear =  !b_input_7_0 && !b_input_5_0 &&
332                              (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_4_CENTER_REAR;
333     const bool b_input_3_0 = !b_input_7_0 && !b_input_5_0 && !b_input_4_center_rear &&
334                              (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_3_0;
335
336     if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_0 )
337     {
338         if( b_input_7_0 )
339             p_filter->p_sys->pf_dowork = DoWork_7_x_to_2_0;
340         else if( b_input_5_0 )
341             p_filter->p_sys->pf_dowork = DoWork_5_x_to_2_0;
342         else if( b_input_4_center_rear )
343             p_filter->p_sys->pf_dowork = DoWork_4_0_to_2_0;
344         else if( b_input_3_0 )
345             p_filter->p_sys->pf_dowork = DoWork_3_x_to_2_0;
346     }
347     else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
348     {
349         if( b_input_7_0 )
350             p_filter->p_sys->pf_dowork = DoWork_7_x_to_1_0;
351         else if( b_input_5_0 )
352             p_filter->p_sys->pf_dowork = DoWork_5_x_to_1_0;
353         else if( b_input_4_center_rear )
354             p_filter->p_sys->pf_dowork = DoWork_4_0_to_1_0;
355         else if( b_input_3_0 )
356             p_filter->p_sys->pf_dowork = DoWork_3_x_to_1_0;
357         else
358             p_filter->p_sys->pf_dowork = DoWork_2_x_to_1_0;
359     }
360     else if(p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0)
361     {
362         if( b_input_7_0 )
363             p_filter->p_sys->pf_dowork = DoWork_7_x_to_4_0;
364         else
365             p_filter->p_sys->pf_dowork = DoWork_5_x_to_4_0;
366     }
367     else
368     {
369         assert( b_input_7_0 || b_input_6_1 );
370         if( b_input_7_0 )
371             p_filter->p_sys->pf_dowork = DoWork_7_x_to_5_x;
372         else
373             p_filter->p_sys->pf_dowork = DoWork_6_1_to_5_x;
374     }
375
376     return VLC_SUCCESS;
377 }
378
379 static void CloseFilter( vlc_object_t *p_this )
380 {
381     filter_t *p_filter = (filter_t *) p_this;
382     filter_sys_t *p_sys = p_filter->p_sys;
383     free( p_sys );
384 }
385
386 /*****************************************************************************
387  * Filter:
388  *****************************************************************************/
389 static block_t *Filter( filter_t *p_filter, block_t *p_block )
390 {
391     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
392     if( !p_block || !p_block->i_nb_samples )
393     {
394         if( p_block )
395             block_Release( p_block );
396         return NULL;
397     }
398
399     size_t i_out_size = p_block->i_nb_samples *
400       p_filter->fmt_out.audio.i_bitspersample *
401         p_filter->fmt_out.audio.i_channels / 8;
402
403     block_t *p_out = block_Alloc( i_out_size );
404     if( !p_out )
405     {
406         msg_Warn( p_filter, "can't get output buffer" );
407         block_Release( p_block );
408         return NULL;
409     }
410
411     p_out->i_nb_samples = p_block->i_nb_samples;
412     p_out->i_dts = p_block->i_dts;
413     p_out->i_pts = p_block->i_pts;
414     p_out->i_length = p_block->i_length;
415
416     int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio );
417     int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio );
418     p_out->i_nb_samples = p_block->i_nb_samples;
419     p_out->i_buffer = p_block->i_buffer * i_output_nb / i_input_nb;
420
421     p_sys->pf_dowork( p_filter, p_block, p_out );
422
423     block_Release( p_block );
424
425     return p_out;
426 }
427
428