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 * );
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 )
51 /*****************************************************************************
53 *****************************************************************************/
54 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output );
56 static block_t *Filter( filter_t *, block_t * );
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 )
64 const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels;
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;
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;
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;
84 if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_0 )
87 for( i = p_in_buf->i_nb_samples; i--; )
89 *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4;
91 *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4;
96 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
98 else if( b_input_5_0 )
99 for( i = p_in_buf->i_nb_samples; i--; )
101 *p_dest = p_src[4] + 0.5 * p_src[0] + 0.33 * p_src[2];
103 *p_dest = p_src[4] + 0.5 * p_src[1] + 0.33 * p_src[3];
108 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
110 else if( b_input_3_0 )
111 for( i = p_in_buf->i_nb_samples; i--; )
113 *p_dest = p_src[2] + 0.5 * p_src[0];
115 *p_dest = p_src[2] + 0.5 * p_src[1];
120 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
122 else if (b_input_4_center_rear)
123 for( i = p_in_buf->i_nb_samples; i--; )
125 *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[0];
127 *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[1];
132 else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
135 for( i = p_in_buf->i_nb_samples; i--; )
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;
142 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
144 else if( b_input_5_0 )
145 for( i = p_in_buf->i_nb_samples; i--; )
147 *p_dest = p_src[4] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 6 + p_src[3] / 6;
152 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
154 else if( b_input_3_0 )
155 for( i = p_in_buf->i_nb_samples; i--; )
157 *p_dest = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
162 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
165 for( i = p_in_buf->i_nb_samples; i--; )
167 *p_dest = p_src[0] / 2 + p_src[1] / 2;
175 assert( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0 );
176 assert( b_input_7_0 || b_input_5_0 );
179 for( i = p_in_buf->i_nb_samples; i--; )
181 *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6;
183 *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
185 *p_dest = p_src[2] / 6 + p_src[4];
187 *p_dest = p_src[3] / 6 + p_src[5];
192 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
195 for( i = p_in_buf->i_nb_samples; i--; )
197 *p_dest = p_src[4] + 0.5 * p_src[0];
199 *p_dest = p_src[4] + 0.5 * p_src[1];
208 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
213 /*****************************************************************************
215 *****************************************************************************/
216 static int OpenFilter( vlc_object_t *p_this )
218 filter_t *p_filter = (filter_t *)p_this;
220 audio_format_t fmt_in = p_filter->fmt_in.audio;
221 audio_format_t fmt_out = p_filter->fmt_out.audio;
223 fmt_in.i_format = p_filter->fmt_in.i_codec;
224 fmt_out.i_format = p_filter->fmt_out.i_codec;
226 if( !IsSupported( &fmt_in, &fmt_out ) )
229 p_filter->pf_audio_filter = Filter;
234 /*****************************************************************************
236 *****************************************************************************/
237 static block_t *Filter( filter_t *p_filter, block_t *p_block )
239 if( !p_block || !p_block->i_nb_samples )
242 block_Release( p_block );
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;
250 block_t *p_out = filter_NewAudioBuffer( p_filter, i_out_size );
253 msg_Warn( p_filter, "can't get output buffer" );
254 block_Release( p_block );
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;
263 DoWork( p_filter, p_block, p_out );
265 block_Release( p_block );
270 /*****************************************************************************
272 *****************************************************************************/
273 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output )
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 )
280 if( p_input->i_physical_channels == p_output->i_physical_channels &&
281 p_input->i_original_channels == p_output->i_original_channels )
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 )
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 )
306 /* Only if we downmix */
307 if( aout_FormatNbChannels( p_input ) <= aout_FormatNbChannels( p_output ) )