]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/simple.c
Simple downmixer: allow Xiph 6.1 -> 2.0
[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_6_1_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
129     VLC_UNUSED(p_filter);
130     float *p_dest = (float *)p_out_buf->p_buffer;
131     const float *p_src = (const float *)p_in_buf->p_buffer;
132     for( int i = p_in_buf->i_nb_samples; i--; )
133     {
134         *p_dest++ = p_src[0] + 0.7071 * (p_src[3] + (p_src[2] + p_src[5]) / 2);
135         *p_dest++ = p_src[1] + 0.7071 * (p_src[4] + (p_src[2] + p_src[5]) / 2);
136
137         p_src += 6;
138
139         /* We always have LFE here */
140         p_src++;
141     }
142 }
143
144 static void DoWork_5_x_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
145     float *p_dest = (float *)p_out_buf->p_buffer;
146     const float *p_src = (const float *)p_in_buf->p_buffer;
147     for( int i = p_in_buf->i_nb_samples; i--; )
148     {
149         *p_dest++ = p_src[0] + 0.7071 * (p_src[4] + p_src[2]);
150         *p_dest++ = p_src[1] + 0.7071 * (p_src[4] + p_src[3]);
151
152         p_src += 5;
153
154         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
155     }
156 }
157
158 static void DoWork_4_0_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
159     VLC_UNUSED(p_filter);
160     float *p_dest = (float *)p_out_buf->p_buffer;
161     const float *p_src = (const float *)p_in_buf->p_buffer;
162     for( int i = p_in_buf->i_nb_samples; i--; )
163     {
164         *p_dest++ = p_src[2] + p_src[3] + 0.5 * p_src[0];
165         *p_dest++ = p_src[2] + p_src[3] + 0.5 * p_src[1];
166         p_src += 4;
167     }
168 }
169
170 static void DoWork_3_x_to_2_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
171     float *p_dest = (float *)p_out_buf->p_buffer;
172     const float *p_src = (const float *)p_in_buf->p_buffer;
173     for( int i = p_in_buf->i_nb_samples; i--; )
174     {
175         *p_dest++ = p_src[2] + 0.5 * p_src[0];
176         *p_dest++ = p_src[2] + 0.5 * p_src[1];
177
178         p_src += 3;
179
180         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
181     }
182 }
183
184 static void DoWork_7_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
185     float *p_dest = (float *)p_out_buf->p_buffer;
186     const float *p_src = (const float *)p_in_buf->p_buffer;
187     for( int i = p_in_buf->i_nb_samples; i--; )
188     {
189         *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;
190
191         p_src += 7;
192
193         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
194     }
195 }
196
197 static void DoWork_5_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
198     float *p_dest = (float *)p_out_buf->p_buffer;
199     const float *p_src = (const float *)p_in_buf->p_buffer;
200     for( int i = p_in_buf->i_nb_samples; i--; )
201     {
202         *p_dest++ = 0.7071 * (p_src[0] + p_src[1]) + p_src[4] + 0.5f * (p_src[2] + p_src[3]);
203
204         p_src += 5;
205
206         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
207     }
208 }
209
210 static void DoWork_4_0_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
211     VLC_UNUSED(p_filter);
212     float *p_dest = (float *)p_out_buf->p_buffer;
213     const float *p_src = (const float *)p_in_buf->p_buffer;
214     for( int i = p_in_buf->i_nb_samples; i--; )
215     {
216         *p_dest++ = p_src[2] + p_src[3] + p_src[0] / 4 + p_src[1] / 4;
217         p_src += 4;
218     }
219 }
220
221 static void DoWork_3_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
222     float *p_dest = (float *)p_out_buf->p_buffer;
223     const float *p_src = (const float *)p_in_buf->p_buffer;
224     for( int i = p_in_buf->i_nb_samples; i--; )
225     {
226         *p_dest++ = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
227
228         p_src += 3;
229
230         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
231     }
232 }
233
234 static void DoWork_2_x_to_1_0( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
235     VLC_UNUSED(p_filter);
236     float *p_dest = (float *)p_out_buf->p_buffer;
237     const float *p_src = (const float *)p_in_buf->p_buffer;
238     for( int i = p_in_buf->i_nb_samples; i--; )
239     {
240         *p_dest++ = p_src[0] / 2 + p_src[1] / 2;
241
242         p_src += 2;
243     }
244 }
245
246 static void DoWork_7_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[6] + 0.5 * p_src[0] + p_src[2] / 6;
252         *p_dest++ = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
253         *p_dest++ = p_src[2] / 6 +  p_src[4];
254         *p_dest++ = p_src[3] / 6 +  p_src[5];
255
256         p_src += 7;
257
258         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
259     }
260 }
261
262 static void DoWork_5_x_to_4_0( 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] + p_src[4] * 0.7071;
268         *p_dest++ = p_src[1] + p_src[4] * 0.7071;
269         *p_dest++ = p_src[2];
270         *p_dest++ = p_src[3];
271
272         p_src += 5;
273
274         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
275     }
276 }
277
278 static void DoWork_7_x_to_5_x( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
279     float *p_dest = (float *)p_out_buf->p_buffer;
280     const float *p_src = (const float *)p_in_buf->p_buffer;
281     for( int i = p_in_buf->i_nb_samples; i--; )
282     {
283         *p_dest++ = p_src[0];
284         *p_dest++ = p_src[1];
285         *p_dest++ = (p_src[2] + p_src[4]) * 0.5;
286         *p_dest++ = (p_src[3] + p_src[5]) * 0.5;
287         *p_dest++ = p_src[6];
288
289         p_src += 7;
290
291         if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE &&
292             p_filter->fmt_out.audio.i_physical_channels & AOUT_CHAN_LFE )
293             *p_dest++ = *p_src++;
294         else if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
295     }
296 }
297
298 static void DoWork_6_1_to_5_x( filter_t * p_filter,  block_t * p_in_buf, block_t * p_out_buf ) {
299     VLC_UNUSED(p_filter);
300     float *p_dest = (float *)p_out_buf->p_buffer;
301     const float *p_src = (const float *)p_in_buf->p_buffer;
302     for( int i = p_in_buf->i_nb_samples; i--; )
303     {
304         *p_dest++ = p_src[0];
305         *p_dest++ = p_src[1];
306         *p_dest++ = (p_src[2] + p_src[4]) * 0.5;
307         *p_dest++ = (p_src[3] + p_src[4]) * 0.5;
308         *p_dest++ = p_src[5];
309
310         p_src += 6;
311
312         /* We always have LFE here */
313         *p_dest++ = *p_src++;
314     }
315 }
316
317
318 /*****************************************************************************
319  * OpenFilter:
320  *****************************************************************************/
321 static int OpenFilter( vlc_object_t *p_this )
322 {
323     filter_t *p_filter = (filter_t *)p_this;
324     filter_sys_t *p_sys;
325
326     audio_format_t fmt_in  = p_filter->fmt_in.audio;
327     audio_format_t fmt_out = p_filter->fmt_out.audio;
328
329     fmt_in.i_format = p_filter->fmt_in.i_codec;
330     fmt_out.i_format = p_filter->fmt_out.i_codec;
331
332     if( !IsSupported( &fmt_in, &fmt_out ) )
333         return VLC_EGENERIC;
334
335     p_filter->p_sys = malloc( sizeof(*p_sys) );
336     if( unlikely(!p_filter->p_sys) )
337         return VLC_ENOMEM;
338
339     p_filter->pf_audio_filter = Filter;
340
341     const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels;
342     const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0;
343     const bool b_input_6_1 = !b_input_7_0 &&
344                              i_input_physical == AOUT_CHANS_6_1_MIDDLE;
345     const bool b_input_5_0 = !b_input_7_0 && !b_input_6_1 &&
346                              ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 ||
347                                (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE );
348     const bool b_input_4_center_rear =  !b_input_7_0 && !b_input_5_0 &&
349                              (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_4_CENTER_REAR;
350     const bool b_input_3_0 = !b_input_7_0 && !b_input_5_0 && !b_input_4_center_rear &&
351                              (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_3_0;
352
353     if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_0 )
354     {
355         if( b_input_7_0 )
356             p_filter->p_sys->pf_dowork = DoWork_7_x_to_2_0;
357         else if( b_input_6_1 )
358             p_filter->p_sys->pf_dowork = DoWork_6_1_to_2_0;
359         else if( b_input_5_0 )
360             p_filter->p_sys->pf_dowork = DoWork_5_x_to_2_0;
361         else if( b_input_4_center_rear )
362             p_filter->p_sys->pf_dowork = DoWork_4_0_to_2_0;
363         else if( b_input_3_0 )
364             p_filter->p_sys->pf_dowork = DoWork_3_x_to_2_0;
365     }
366     else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
367     {
368         if( b_input_7_0 )
369             p_filter->p_sys->pf_dowork = DoWork_7_x_to_1_0;
370         else if( b_input_5_0 )
371             p_filter->p_sys->pf_dowork = DoWork_5_x_to_1_0;
372         else if( b_input_4_center_rear )
373             p_filter->p_sys->pf_dowork = DoWork_4_0_to_1_0;
374         else if( b_input_3_0 )
375             p_filter->p_sys->pf_dowork = DoWork_3_x_to_1_0;
376         else
377             p_filter->p_sys->pf_dowork = DoWork_2_x_to_1_0;
378     }
379     else if(p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0)
380     {
381         if( b_input_7_0 )
382             p_filter->p_sys->pf_dowork = DoWork_7_x_to_4_0;
383         else
384             p_filter->p_sys->pf_dowork = DoWork_5_x_to_4_0;
385     }
386     else
387     {
388         assert( b_input_7_0 || b_input_6_1 );
389         if( b_input_7_0 )
390             p_filter->p_sys->pf_dowork = DoWork_7_x_to_5_x;
391         else
392             p_filter->p_sys->pf_dowork = DoWork_6_1_to_5_x;
393     }
394
395     return VLC_SUCCESS;
396 }
397
398 static void CloseFilter( vlc_object_t *p_this )
399 {
400     filter_t *p_filter = (filter_t *) p_this;
401     filter_sys_t *p_sys = p_filter->p_sys;
402     free( p_sys );
403 }
404
405 /*****************************************************************************
406  * Filter:
407  *****************************************************************************/
408 static block_t *Filter( filter_t *p_filter, block_t *p_block )
409 {
410     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
411     if( !p_block || !p_block->i_nb_samples )
412     {
413         if( p_block )
414             block_Release( p_block );
415         return NULL;
416     }
417
418     size_t i_out_size = p_block->i_nb_samples *
419       p_filter->fmt_out.audio.i_bitspersample *
420         p_filter->fmt_out.audio.i_channels / 8;
421
422     block_t *p_out = block_Alloc( i_out_size );
423     if( !p_out )
424     {
425         msg_Warn( p_filter, "can't get output buffer" );
426         block_Release( p_block );
427         return NULL;
428     }
429
430     p_out->i_nb_samples = p_block->i_nb_samples;
431     p_out->i_dts = p_block->i_dts;
432     p_out->i_pts = p_block->i_pts;
433     p_out->i_length = p_block->i_length;
434
435     int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio );
436     int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio );
437     p_out->i_nb_samples = p_block->i_nb_samples;
438     p_out->i_buffer = p_block->i_buffer * i_output_nb / i_input_nb;
439
440     p_sys->pf_dowork( p_filter, p_block, p_out );
441
442     block_Release( p_block );
443
444     return p_out;
445 }
446