]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/simple.c
LGPL
[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
43 vlc_module_begin ()
44     set_description( N_("Audio filter for simple channel mixing") )
45     set_category( CAT_AUDIO )
46     set_subcategory( SUBCAT_AUDIO_MISC )
47     set_capability( "audio converter", 10 )
48     set_callbacks( OpenFilter, NULL )
49 vlc_module_end ()
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output );
55
56 static block_t *Filter( filter_t *, block_t * );
57
58 /*****************************************************************************
59  * DoWork: convert a buffer
60  *****************************************************************************/
61 static void DoWork( filter_t * p_filter,
62                     block_t * p_in_buf, block_t * p_out_buf )
63 {
64     const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels;
65
66     const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0;
67     const bool b_input_5_0 = !b_input_7_0 &&
68                              ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 ||
69                                (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE );
70     const bool b_input_4_center_rear =  !b_input_7_0 && !b_input_5_0 &&
71                              (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_4_CENTER_REAR;
72     const bool b_input_3_0 = !b_input_7_0 && !b_input_5_0 && !b_input_4_center_rear &&
73                              (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_3_0;
74
75     int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio );
76     int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio );
77     float *p_dest = (float *)p_out_buf->p_buffer;
78     const float *p_src = (const float *)p_in_buf->p_buffer;
79     int i;
80
81     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
82     p_out_buf->i_buffer = p_in_buf->i_buffer * i_output_nb / i_input_nb;
83
84     if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_0 )
85     {
86         if( b_input_7_0 )
87         for( i = p_in_buf->i_nb_samples; i--; )
88         {
89             *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4;
90             p_dest++;
91             *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4;
92             p_dest++;
93
94             p_src += 7;
95
96             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
97         }
98         else if( b_input_5_0 )
99         for( i = p_in_buf->i_nb_samples; i--; )
100         {
101             *p_dest = p_src[4] + 0.5 * p_src[0] + 0.33 * p_src[2];
102             p_dest++;
103             *p_dest = p_src[4] + 0.5 * p_src[1] + 0.33 * p_src[3];
104             p_dest++;
105
106             p_src += 5;
107
108             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
109         }
110         else if( b_input_3_0 )
111         for( i = p_in_buf->i_nb_samples; i--; )
112         {
113             *p_dest = p_src[2] + 0.5 * p_src[0];
114             p_dest++;
115             *p_dest = p_src[2] + 0.5 * p_src[1];
116             p_dest++;
117
118             p_src += 3;
119
120             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
121         }
122         else if (b_input_4_center_rear)
123         for( i = p_in_buf->i_nb_samples; i--; )
124         {
125           *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[0];
126           p_dest++;
127           *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[1];
128           p_dest++;
129           p_src += 4;
130         }
131     }
132     else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
133     {
134         if( b_input_7_0 )
135         for( i = p_in_buf->i_nb_samples; i--; )
136         {
137             *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;
138             p_dest++;
139
140             p_src += 7;
141
142             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
143         }
144         else if( b_input_5_0 )
145         for( i = p_in_buf->i_nb_samples; i--; )
146         {
147             *p_dest = p_src[4] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 6 + p_src[3] / 6;
148             p_dest++;
149
150             p_src += 5;
151
152             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
153         }
154         else if( b_input_3_0 )
155         for( i = p_in_buf->i_nb_samples; i--; )
156         {
157             *p_dest = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
158             p_dest++;
159
160             p_src += 3;
161
162             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
163         }
164         else
165         for( i = p_in_buf->i_nb_samples; i--; )
166         {
167             *p_dest = p_src[0] / 2 + p_src[1] / 2;
168             p_dest++;
169
170             p_src += 2;
171         }
172     }
173     else
174     {
175         assert( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0 );
176         assert( b_input_7_0 || b_input_5_0 );
177
178         if( b_input_7_0 )
179         for( i = p_in_buf->i_nb_samples; i--; )
180         {
181             *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6;
182             p_dest++;
183             *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
184             p_dest++;
185             *p_dest = p_src[2] / 6 +  p_src[4];
186             p_dest++;
187             *p_dest = p_src[3] / 6 +  p_src[5];
188             p_dest++;
189
190             p_src += 7;
191
192             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
193         }
194         else
195         for( i = p_in_buf->i_nb_samples; i--; )
196         {
197             *p_dest = p_src[4] + 0.5 * p_src[0];
198             p_dest++;
199             *p_dest = p_src[4] + 0.5 * p_src[1];
200             p_dest++;
201             *p_dest = p_src[2];
202             p_dest++;
203             *p_dest = p_src[3];
204             p_dest++;
205
206             p_src += 5;
207
208             if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
209         }
210     }
211 }
212
213 /*****************************************************************************
214  * OpenFilter:
215  *****************************************************************************/
216 static int OpenFilter( vlc_object_t *p_this )
217 {
218     filter_t *p_filter = (filter_t *)p_this;
219
220     audio_format_t fmt_in  = p_filter->fmt_in.audio;
221     audio_format_t fmt_out = p_filter->fmt_out.audio;
222
223     fmt_in.i_format = p_filter->fmt_in.i_codec;
224     fmt_out.i_format = p_filter->fmt_out.i_codec;
225
226     if( !IsSupported( &fmt_in, &fmt_out ) )
227         return -1;
228
229     p_filter->pf_audio_filter = Filter;
230
231     return 0;
232 }
233
234 /*****************************************************************************
235  * Filter:
236  *****************************************************************************/
237 static block_t *Filter( filter_t *p_filter, block_t *p_block )
238 {
239     if( !p_block || !p_block->i_nb_samples )
240     {
241         if( p_block )
242             block_Release( p_block );
243         return NULL;
244     }
245
246     size_t i_out_size = p_block->i_nb_samples *
247       p_filter->fmt_out.audio.i_bitspersample *
248         p_filter->fmt_out.audio.i_channels / 8;
249
250     block_t *p_out = filter_NewAudioBuffer( p_filter, i_out_size );
251     if( !p_out )
252     {
253         msg_Warn( p_filter, "can't get output buffer" );
254         block_Release( p_block );
255         return NULL;
256     }
257
258     p_out->i_nb_samples = p_block->i_nb_samples;
259     p_out->i_dts = p_block->i_dts;
260     p_out->i_pts = p_block->i_pts;
261     p_out->i_length = p_block->i_length;
262
263     DoWork( p_filter, p_block, p_out );
264
265     block_Release( p_block );
266
267     return p_out;
268 }
269
270 /*****************************************************************************
271  * Helpers:
272  *****************************************************************************/
273 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output )
274 {
275     if( p_input->i_format != VLC_CODEC_FL32 ||
276           p_input->i_format != p_output->i_format ||
277           p_input->i_rate != p_output->i_rate )
278         return false;
279
280     if( p_input->i_physical_channels == p_output->i_physical_channels &&
281         p_input->i_original_channels == p_output->i_original_channels )
282     {
283         return false;
284     }
285
286     /* Only conversion to Mono, Stereo and 4.0 right now */
287     if( p_output->i_physical_channels != AOUT_CHAN_CENTER &&
288         p_output->i_physical_channels != AOUT_CHANS_2_0 &&
289         p_output->i_physical_channels != AOUT_CHANS_4_0 )
290     {
291         return false;
292     }
293
294     /* Only from 7/7.1/5/5.1/3/3.1/2.0
295      * XXX 5.X rear and middle are handled the same way */
296     if( (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_7_0 &&
297         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0 &&
298         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0_MIDDLE &&
299         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_3_0 &&
300         (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_4_CENTER_REAR &&
301          p_input->i_physical_channels != AOUT_CHANS_2_0 )
302     {
303         return false;
304     }
305
306     /* Only if we downmix */
307     if( aout_FormatNbChannels( p_input ) <= aout_FormatNbChannels( p_output ) )
308         return false;
309
310     return true;
311 }
312