1 /*****************************************************************************
2 * simple.c : simple channel mixer plug-in
3 *****************************************************************************
4 * Copyright (C) 2002, 2006 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, 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 Create ( vlc_object_t * );
42 static int OpenFilter( vlc_object_t * );
45 set_description( N_("Audio filter for simple channel mixing") )
46 set_capability( "audio filter", 10 )
47 set_category( CAT_AUDIO )
48 set_subcategory( SUBCAT_AUDIO_MISC )
49 set_callbacks( Create, NULL )
52 set_description( N_("audio filter for simple channel mixing") )
53 set_capability( "audio filter2", 10 )
54 set_callbacks( OpenFilter, NULL )
57 /*****************************************************************************
59 *****************************************************************************/
60 #define AOUT_CHANS_STEREO_FRONT ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
61 #define AOUT_CHANS_STEREO_REAR ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT )
62 #define AOUT_CHANS_STEREO_MIDDLE (AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT )
64 #define AOUT_CHANS_2_0 AOUT_CHANS_STEREO_FRONT
65 #define AOUT_CHANS_3_0 ( AOUT_CHANS_STEREO_FRONT | AOUT_CHAN_CENTER )
66 #define AOUT_CHANS_4_0 ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR )
67 #define AOUT_CHANS_4_0_MIDDLE ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_MIDDLE )
68 #define AOUT_CHANS_4_CENTER_REAR (AOUT_CHANS_STEREO_FRONT | AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER)
69 #define AOUT_CHANS_5_0 ( AOUT_CHANS_4_0 | AOUT_CHAN_CENTER )
70 #define AOUT_CHANS_5_0_MIDDLE ( AOUT_CHANS_4_0_MIDDLE | AOUT_CHAN_CENTER )
71 #define AOUT_CHANS_6_0 ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR | AOUT_CHANS_STEREO_MIDDLE )
72 #define AOUT_CHANS_7_0 ( AOUT_CHANS_6_0 | AOUT_CHAN_CENTER )
74 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output );
76 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
79 static block_t *Filter( filter_t *, block_t * );
81 /*****************************************************************************
82 * Create: allocate trivial channel mixer
83 *****************************************************************************/
84 static int Create( vlc_object_t *p_this )
86 aout_filter_t * p_filter = (aout_filter_t *)p_this;
88 if( !IsSupported( &p_filter->input, &p_filter->output ) )
91 p_filter->pf_do_work = DoWork;
92 p_filter->b_in_place = 0;
97 /*****************************************************************************
98 * DoWork: convert a buffer
99 *****************************************************************************/
100 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
101 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
104 const unsigned i_input_physical = p_filter->input.i_physical_channels;
106 const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0;
107 const bool b_input_5_0 = !b_input_7_0 &&
108 ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 ||
109 (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE );
110 const bool b_input_4_center_rear = !b_input_7_0 && !b_input_5_0 &&
111 (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_4_CENTER_REAR;
112 const bool b_input_3_0 = !b_input_7_0 && !b_input_5_0 && !b_input_4_center_rear &&
113 (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_3_0;
115 int i_input_nb = aout_FormatNbChannels( &p_filter->input );
116 int i_output_nb = aout_FormatNbChannels( &p_filter->output );
117 float *p_dest = (float *)p_out_buf->p_buffer;
118 const float *p_src = (const float *)p_in_buf->p_buffer;
121 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
122 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
124 if( p_filter->output.i_physical_channels == AOUT_CHANS_2_0 )
127 for( i = p_in_buf->i_nb_samples; i--; )
129 *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4;
131 *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4;
136 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
138 else if( b_input_5_0 )
139 for( i = p_in_buf->i_nb_samples; i--; )
141 *p_dest = p_src[4] + 0.5 * p_src[0] + 0.33 * p_src[2];
143 *p_dest = p_src[4] + 0.5 * p_src[1] + 0.33 * p_src[3];
148 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
150 else if( b_input_3_0 )
151 for( i = p_in_buf->i_nb_samples; i--; )
153 *p_dest = p_src[2] + 0.5 * p_src[0];
155 *p_dest = p_src[2] + 0.5 * p_src[1];
160 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
162 else if (b_input_4_center_rear)
163 for( i = p_in_buf->i_nb_samples; i--; )
165 *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[0];
167 *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[1];
172 else if( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
175 for( i = p_in_buf->i_nb_samples; i--; )
177 *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;
182 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
184 else if( b_input_5_0 )
185 for( i = p_in_buf->i_nb_samples; i--; )
187 *p_dest = p_src[4] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 6 + p_src[3] / 6;
192 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
194 else if( b_input_3_0 )
195 for( i = p_in_buf->i_nb_samples; i--; )
197 *p_dest = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
202 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
205 for( i = p_in_buf->i_nb_samples; i--; )
207 *p_dest = p_src[0] / 2 + p_src[1] / 2;
215 assert( p_filter->output.i_physical_channels == AOUT_CHANS_4_0 );
216 assert( b_input_7_0 || b_input_5_0 );
219 for( i = p_in_buf->i_nb_samples; i--; )
221 *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6;
223 *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
225 *p_dest = p_src[2] / 6 + p_src[4];
227 *p_dest = p_src[3] / 6 + p_src[5];
232 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
235 for( i = p_in_buf->i_nb_samples; i--; )
237 *p_dest = p_src[4] + 0.5 * p_src[0];
239 *p_dest = p_src[4] + 0.5 * p_src[1];
248 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
253 /*****************************************************************************
255 *****************************************************************************/
256 static int OpenFilter( vlc_object_t *p_this )
258 filter_t *p_filter = (filter_t *)p_this;
260 audio_format_t fmt_in = p_filter->fmt_in.audio;
261 audio_format_t fmt_out = p_filter->fmt_out.audio;
263 fmt_in.i_format = p_filter->fmt_in.i_codec;
264 fmt_out.i_format = p_filter->fmt_out.i_codec;
266 if( !IsSupported( &fmt_in, &fmt_out ) )
269 p_filter->pf_audio_filter = Filter;
274 /*****************************************************************************
276 *****************************************************************************/
277 static block_t *Filter( filter_t *p_filter, block_t *p_block )
279 aout_filter_t aout_filter;
280 aout_buffer_t in_buf, out_buf;
284 if( !p_block || !p_block->i_nb_samples )
287 block_Release( p_block );
291 i_out_size = p_block->i_nb_samples *
292 p_filter->fmt_out.audio.i_bitspersample *
293 p_filter->fmt_out.audio.i_channels / 8;
295 p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
298 msg_Warn( p_filter, "can't get output buffer" );
299 block_Release( p_block );
303 p_out->i_nb_samples = p_block->i_nb_samples;
304 p_out->i_dts = p_block->i_dts;
305 p_out->i_pts = p_block->i_pts;
306 p_out->i_length = p_block->i_length;
308 aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
309 aout_filter.input = p_filter->fmt_in.audio;
310 aout_filter.input.i_format = p_filter->fmt_in.i_codec;
311 aout_filter.output = p_filter->fmt_out.audio;
312 aout_filter.output.i_format = p_filter->fmt_out.i_codec;
314 in_buf.p_buffer = p_block->p_buffer;
315 in_buf.i_nb_bytes = p_block->i_buffer;
316 in_buf.i_nb_samples = p_block->i_nb_samples;
317 out_buf.p_buffer = p_out->p_buffer;
318 out_buf.i_nb_bytes = p_out->i_buffer;
319 out_buf.i_nb_samples = p_out->i_nb_samples;
321 DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
323 block_Release( p_block );
325 p_out->i_buffer = out_buf.i_nb_bytes;
326 p_out->i_nb_samples = out_buf.i_nb_samples;
331 /*****************************************************************************
333 *****************************************************************************/
334 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output )
336 if( p_input->i_format != VLC_CODEC_FL32 ||
337 p_input->i_format != p_output->i_format ||
338 p_input->i_rate != p_output->i_rate )
341 if( p_input->i_physical_channels == p_output->i_physical_channels &&
342 p_input->i_original_channels == p_output->i_original_channels )
347 /* Only conversion to Mono, Stereo and 4.0 right now */
348 if( p_output->i_physical_channels != AOUT_CHAN_CENTER &&
349 p_output->i_physical_channels != AOUT_CHANS_2_0 &&
350 p_output->i_physical_channels != AOUT_CHANS_4_0 )
355 /* Only from 7/7.1/5/5.1/3/3.1/2.0
356 * XXX 5.X rear and middle are handled the same way */
357 if( (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_7_0 &&
358 (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0 &&
359 (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0_MIDDLE &&
360 (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_3_0 &&
361 (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_4_CENTER_REAR &&
362 p_input->i_physical_channels != AOUT_CHANS_2_0 )
367 /* Only if we downmix */
368 if( aout_FormatNbChannels( p_input ) <= aout_FormatNbChannels( p_output ) )