1 /*****************************************************************************
2 * simple.c : simple channel mixer plug-in
3 *****************************************************************************
4 * Copyright (C) 2002, 2004, 2006-2009 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35 #include <vlc_block.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 static int OpenFilter( vlc_object_t * );
42 static void CloseFilter( vlc_object_t * );
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 );
52 /*****************************************************************************
54 *****************************************************************************/
57 void (*pf_dowork)(filter_t *, block_t *, block_t * );
60 /*****************************************************************************
61 * IsSupported: can we downmix?
62 *****************************************************************************/
63 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output )
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 )
72 if( p_input->i_physical_channels == p_output->i_physical_channels &&
73 p_input->i_original_channels == p_output->i_original_channels )
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 )
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
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 )
105 /* Only downmixing */
106 if( aout_FormatNbChannels( p_input ) <= aout_FormatNbChannels( p_output ) )
112 static block_t *Filter( filter_t *, block_t * );
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--; )
119 float ctr = p_src[6] * 0.7071f;
120 *p_dest++ = ctr + p_src[0] + p_src[2] / 4 + p_src[4] / 4;
121 *p_dest++ = ctr + p_src[1] + p_src[3] / 4 + p_src[5] / 4;
125 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
129 static void DoWork_6_1_to_2_0( filter_t *p_filter, block_t *p_in_buf,
132 VLC_UNUSED(p_filter);
133 float *p_dest = (float *)p_out_buf->p_buffer;
134 const float *p_src = (const float *)p_in_buf->p_buffer;
135 for( int i = p_in_buf->i_nb_samples; i--; )
137 float ctr = (p_src[2] + p_src[5]) * 0.7071f;
138 *p_dest++ = p_src[0] + p_src[3] + ctr;
139 *p_dest++ = p_src[1] + p_src[4] + ctr;
143 /* We always have LFE here */
148 static void DoWork_5_x_to_2_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) {
149 float *p_dest = (float *)p_out_buf->p_buffer;
150 const float *p_src = (const float *)p_in_buf->p_buffer;
151 for( int i = p_in_buf->i_nb_samples; i--; )
153 *p_dest++ = p_src[0] + 0.7071f * (p_src[4] + p_src[2]);
154 *p_dest++ = p_src[1] + 0.7071f * (p_src[4] + p_src[3]);
158 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
162 static void DoWork_4_0_to_2_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) {
163 VLC_UNUSED(p_filter);
164 float *p_dest = (float *)p_out_buf->p_buffer;
165 const float *p_src = (const float *)p_in_buf->p_buffer;
166 for( int i = p_in_buf->i_nb_samples; i--; )
168 *p_dest++ = p_src[2] + p_src[3] + 0.5f * p_src[0];
169 *p_dest++ = p_src[2] + p_src[3] + 0.5f * p_src[1];
174 static void DoWork_3_x_to_2_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) {
175 float *p_dest = (float *)p_out_buf->p_buffer;
176 const float *p_src = (const float *)p_in_buf->p_buffer;
177 for( int i = p_in_buf->i_nb_samples; i--; )
179 *p_dest++ = p_src[2] + 0.5f * p_src[0];
180 *p_dest++ = p_src[2] + 0.5f * p_src[1];
184 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
188 static void DoWork_7_x_to_1_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) {
189 float *p_dest = (float *)p_out_buf->p_buffer;
190 const float *p_src = (const float *)p_in_buf->p_buffer;
191 for( int i = p_in_buf->i_nb_samples; i--; )
193 *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;
197 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
201 static void DoWork_5_x_to_1_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) {
202 float *p_dest = (float *)p_out_buf->p_buffer;
203 const float *p_src = (const float *)p_in_buf->p_buffer;
204 for( int i = p_in_buf->i_nb_samples; i--; )
206 *p_dest++ = 0.7071f * (p_src[0] + p_src[1]) + p_src[4]
207 + 0.5f * (p_src[2] + p_src[3]);
211 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
215 static void DoWork_4_0_to_1_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) {
216 VLC_UNUSED(p_filter);
217 float *p_dest = (float *)p_out_buf->p_buffer;
218 const float *p_src = (const float *)p_in_buf->p_buffer;
219 for( int i = p_in_buf->i_nb_samples; i--; )
221 *p_dest++ = p_src[2] + p_src[3] + p_src[0] / 4 + p_src[1] / 4;
226 static void DoWork_3_x_to_1_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) {
227 float *p_dest = (float *)p_out_buf->p_buffer;
228 const float *p_src = (const float *)p_in_buf->p_buffer;
229 for( int i = p_in_buf->i_nb_samples; i--; )
231 *p_dest++ = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
235 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
239 static void DoWork_2_x_to_1_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) {
240 VLC_UNUSED(p_filter);
241 float *p_dest = (float *)p_out_buf->p_buffer;
242 const float *p_src = (const float *)p_in_buf->p_buffer;
243 for( int i = p_in_buf->i_nb_samples; i--; )
245 *p_dest++ = p_src[0] / 2 + p_src[1] / 2;
251 static void DoWork_7_x_to_4_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) {
252 float *p_dest = (float *)p_out_buf->p_buffer;
253 const float *p_src = (const float *)p_in_buf->p_buffer;
254 for( int i = p_in_buf->i_nb_samples; i--; )
256 *p_dest++ = p_src[6] + 0.5f * p_src[0] + p_src[2] / 6;
257 *p_dest++ = p_src[6] + 0.5f * p_src[1] + p_src[3] / 6;
258 *p_dest++ = p_src[2] / 6 + p_src[4];
259 *p_dest++ = p_src[3] / 6 + p_src[5];
263 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
267 static void DoWork_5_x_to_4_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) {
268 float *p_dest = (float *)p_out_buf->p_buffer;
269 const float *p_src = (const float *)p_in_buf->p_buffer;
270 for( int i = p_in_buf->i_nb_samples; i--; )
272 float ctr = p_src[4] * 0.7071f;
273 *p_dest++ = p_src[0] + ctr;
274 *p_dest++ = p_src[1] + ctr;
275 *p_dest++ = p_src[2];
276 *p_dest++ = p_src[3];
280 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
284 static void DoWork_7_x_to_5_x( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) {
285 float *p_dest = (float *)p_out_buf->p_buffer;
286 const float *p_src = (const float *)p_in_buf->p_buffer;
287 for( int i = p_in_buf->i_nb_samples; i--; )
289 *p_dest++ = p_src[0];
290 *p_dest++ = p_src[1];
291 *p_dest++ = (p_src[2] + p_src[4]) * 0.5f;
292 *p_dest++ = (p_src[3] + p_src[5]) * 0.5f;
293 *p_dest++ = p_src[6];
297 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE &&
298 p_filter->fmt_out.audio.i_physical_channels & AOUT_CHAN_LFE )
299 *p_dest++ = *p_src++;
300 else if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
304 static void DoWork_6_1_to_5_x( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) {
305 VLC_UNUSED(p_filter);
306 float *p_dest = (float *)p_out_buf->p_buffer;
307 const float *p_src = (const float *)p_in_buf->p_buffer;
308 for( int i = p_in_buf->i_nb_samples; i--; )
310 *p_dest++ = p_src[0];
311 *p_dest++ = p_src[1];
312 *p_dest++ = (p_src[2] + p_src[4]) * 0.5f;
313 *p_dest++ = (p_src[3] + p_src[4]) * 0.5f;
314 *p_dest++ = p_src[5];
318 /* We always have LFE here */
319 *p_dest++ = *p_src++;
324 /*****************************************************************************
326 *****************************************************************************/
327 static int OpenFilter( vlc_object_t *p_this )
329 filter_t *p_filter = (filter_t *)p_this;
332 audio_format_t fmt_in = p_filter->fmt_in.audio;
333 audio_format_t fmt_out = p_filter->fmt_out.audio;
335 fmt_in.i_format = p_filter->fmt_in.i_codec;
336 fmt_out.i_format = p_filter->fmt_out.i_codec;
338 if( !IsSupported( &fmt_in, &fmt_out ) )
341 p_filter->p_sys = malloc( sizeof(*p_sys) );
342 if( unlikely(!p_filter->p_sys) )
345 p_filter->pf_audio_filter = Filter;
347 const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels;
348 const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0;
349 const bool b_input_6_1 = !b_input_7_0 &&
350 i_input_physical == AOUT_CHANS_6_1_MIDDLE;
351 const bool b_input_5_0 = !b_input_7_0 && !b_input_6_1 &&
352 ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 ||
353 (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE );
354 const bool b_input_4_center_rear = !b_input_7_0 && !b_input_5_0 &&
355 (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_4_CENTER_REAR;
356 const bool b_input_3_0 = !b_input_7_0 && !b_input_5_0 && !b_input_4_center_rear &&
357 (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_3_0;
359 if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_0 )
362 p_filter->p_sys->pf_dowork = DoWork_7_x_to_2_0;
363 else if( b_input_6_1 )
364 p_filter->p_sys->pf_dowork = DoWork_6_1_to_2_0;
365 else if( b_input_5_0 )
366 p_filter->p_sys->pf_dowork = DoWork_5_x_to_2_0;
367 else if( b_input_4_center_rear )
368 p_filter->p_sys->pf_dowork = DoWork_4_0_to_2_0;
369 else if( b_input_3_0 )
370 p_filter->p_sys->pf_dowork = DoWork_3_x_to_2_0;
372 else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
375 p_filter->p_sys->pf_dowork = DoWork_7_x_to_1_0;
376 else if( b_input_5_0 )
377 p_filter->p_sys->pf_dowork = DoWork_5_x_to_1_0;
378 else if( b_input_4_center_rear )
379 p_filter->p_sys->pf_dowork = DoWork_4_0_to_1_0;
380 else if( b_input_3_0 )
381 p_filter->p_sys->pf_dowork = DoWork_3_x_to_1_0;
383 p_filter->p_sys->pf_dowork = DoWork_2_x_to_1_0;
385 else if(p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0)
388 p_filter->p_sys->pf_dowork = DoWork_7_x_to_4_0;
390 p_filter->p_sys->pf_dowork = DoWork_5_x_to_4_0;
394 assert( b_input_7_0 || b_input_6_1 );
396 p_filter->p_sys->pf_dowork = DoWork_7_x_to_5_x;
398 p_filter->p_sys->pf_dowork = DoWork_6_1_to_5_x;
404 static void CloseFilter( vlc_object_t *p_this )
406 filter_t *p_filter = (filter_t *) p_this;
407 filter_sys_t *p_sys = p_filter->p_sys;
411 /*****************************************************************************
413 *****************************************************************************/
414 static block_t *Filter( filter_t *p_filter, block_t *p_block )
416 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
417 if( !p_block || !p_block->i_nb_samples )
420 block_Release( p_block );
424 size_t i_out_size = p_block->i_nb_samples *
425 p_filter->fmt_out.audio.i_bitspersample *
426 p_filter->fmt_out.audio.i_channels / 8;
428 block_t *p_out = block_Alloc( i_out_size );
431 msg_Warn( p_filter, "can't get output buffer" );
432 block_Release( p_block );
436 p_out->i_nb_samples = p_block->i_nb_samples;
437 p_out->i_dts = p_block->i_dts;
438 p_out->i_pts = p_block->i_pts;
439 p_out->i_length = p_block->i_length;
441 int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio );
442 int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio );
443 p_out->i_nb_samples = p_block->i_nb_samples;
444 p_out->i_buffer = p_block->i_buffer * i_output_nb / i_input_nb;
446 p_sys->pf_dowork( p_filter, p_block, p_out );
448 block_Release( p_block );