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 *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;
124 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
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--; )
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]);
138 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
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--; )
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];
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--; )
159 *p_dest++ = p_src[2] + 0.5 * p_src[0];
160 *p_dest++ = p_src[2] + 0.5 * p_src[1];
164 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
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--; )
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;
177 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
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--; )
186 *p_dest++ = 0.7071 * (p_src[0] + p_src[1]) + p_src[4] + 0.5f * (p_src[2] + p_src[3]);
190 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
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--; )
200 *p_dest++ = p_src[2] + p_src[3] + p_src[0] / 4 + p_src[1] / 4;
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--; )
210 *p_dest++ = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
214 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
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--; )
224 *p_dest++ = p_src[0] / 2 + p_src[1] / 2;
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--; )
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];
242 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
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--; )
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];
258 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
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--; )
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];
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++;
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--; )
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];
295 /* We always have LFE here */
296 *p_dest++ = *p_src++;
301 /*****************************************************************************
303 *****************************************************************************/
304 static int OpenFilter( vlc_object_t *p_this )
306 filter_t *p_filter = (filter_t *)p_this;
309 audio_format_t fmt_in = p_filter->fmt_in.audio;
310 audio_format_t fmt_out = p_filter->fmt_out.audio;
312 fmt_in.i_format = p_filter->fmt_in.i_codec;
313 fmt_out.i_format = p_filter->fmt_out.i_codec;
315 if( !IsSupported( &fmt_in, &fmt_out ) )
318 p_filter->p_sys = malloc( sizeof(*p_sys) );
319 if( unlikely(!p_filter->p_sys) )
322 p_filter->pf_audio_filter = Filter;
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;
336 if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_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;
347 else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
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;
358 p_filter->p_sys->pf_dowork = DoWork_2_x_to_1_0;
360 else if(p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0)
363 p_filter->p_sys->pf_dowork = DoWork_7_x_to_4_0;
365 p_filter->p_sys->pf_dowork = DoWork_5_x_to_4_0;
369 assert( b_input_7_0 || b_input_6_1 );
371 p_filter->p_sys->pf_dowork = DoWork_7_x_to_5_x;
373 p_filter->p_sys->pf_dowork = DoWork_6_1_to_5_x;
379 static void CloseFilter( vlc_object_t *p_this )
381 filter_t *p_filter = (filter_t *) p_this;
382 filter_sys_t *p_sys = p_filter->p_sys;
386 /*****************************************************************************
388 *****************************************************************************/
389 static block_t *Filter( filter_t *p_filter, block_t *p_block )
391 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
392 if( !p_block || !p_block->i_nb_samples )
395 block_Release( p_block );
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;
403 block_t *p_out = block_Alloc( i_out_size );
406 msg_Warn( p_filter, "can't get output buffer" );
407 block_Release( p_block );
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;
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;
421 p_sys->pf_dowork( p_filter, p_block, p_out );
423 block_Release( p_block );