1 /*****************************************************************************
2 * simple.c : simple channel mixer plug-in
3 *****************************************************************************
4 * Copyright (C) 2002, 2004, 2006-2009 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 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 filter", 10 )
48 set_callbacks( OpenFilter, NULL )
51 /*****************************************************************************
53 *****************************************************************************/
54 #define AOUT_CHANS_STEREO_FRONT ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
55 #define AOUT_CHANS_STEREO_REAR ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT )
56 #define AOUT_CHANS_STEREO_MIDDLE (AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT )
58 #define AOUT_CHANS_2_0 AOUT_CHANS_STEREO_FRONT
59 #define AOUT_CHANS_3_0 ( AOUT_CHANS_STEREO_FRONT | AOUT_CHAN_CENTER )
60 #define AOUT_CHANS_4_0 ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR )
61 #define AOUT_CHANS_4_0_MIDDLE ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_MIDDLE )
62 #define AOUT_CHANS_4_CENTER_REAR (AOUT_CHANS_STEREO_FRONT | AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER)
63 #define AOUT_CHANS_5_0 ( AOUT_CHANS_4_0 | AOUT_CHAN_CENTER )
64 #define AOUT_CHANS_5_0_MIDDLE ( AOUT_CHANS_4_0_MIDDLE | AOUT_CHAN_CENTER )
65 #define AOUT_CHANS_6_0 ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR | AOUT_CHANS_STEREO_MIDDLE )
66 #define AOUT_CHANS_7_0 ( AOUT_CHANS_6_0 | AOUT_CHAN_CENTER )
68 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output );
70 static block_t *Filter( filter_t *, block_t * );
72 /*****************************************************************************
73 * DoWork: convert a buffer
74 *****************************************************************************/
75 static void DoWork( filter_t * p_filter,
76 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
78 const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels;
80 const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0;
81 const bool b_input_5_0 = !b_input_7_0 &&
82 ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 ||
83 (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE );
84 const bool b_input_4_center_rear = !b_input_7_0 && !b_input_5_0 &&
85 (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_4_CENTER_REAR;
86 const bool b_input_3_0 = !b_input_7_0 && !b_input_5_0 && !b_input_4_center_rear &&
87 (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_3_0;
89 int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio );
90 int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio );
91 float *p_dest = (float *)p_out_buf->p_buffer;
92 const float *p_src = (const float *)p_in_buf->p_buffer;
95 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
96 p_out_buf->i_buffer = p_in_buf->i_buffer * i_output_nb / i_input_nb;
98 if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_0 )
101 for( i = p_in_buf->i_nb_samples; i--; )
103 *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4;
105 *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4;
110 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
112 else if( b_input_5_0 )
113 for( i = p_in_buf->i_nb_samples; i--; )
115 *p_dest = p_src[4] + 0.5 * p_src[0] + 0.33 * p_src[2];
117 *p_dest = p_src[4] + 0.5 * p_src[1] + 0.33 * p_src[3];
122 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
124 else if( b_input_3_0 )
125 for( i = p_in_buf->i_nb_samples; i--; )
127 *p_dest = p_src[2] + 0.5 * p_src[0];
129 *p_dest = p_src[2] + 0.5 * p_src[1];
134 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
136 else if (b_input_4_center_rear)
137 for( i = p_in_buf->i_nb_samples; i--; )
139 *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[0];
141 *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[1];
146 else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER )
149 for( i = p_in_buf->i_nb_samples; i--; )
151 *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;
156 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
158 else if( b_input_5_0 )
159 for( i = p_in_buf->i_nb_samples; i--; )
161 *p_dest = p_src[4] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 6 + p_src[3] / 6;
166 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
168 else if( b_input_3_0 )
169 for( i = p_in_buf->i_nb_samples; i--; )
171 *p_dest = p_src[2] + p_src[0] / 4 + p_src[1] / 4;
176 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
179 for( i = p_in_buf->i_nb_samples; i--; )
181 *p_dest = p_src[0] / 2 + p_src[1] / 2;
189 assert( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0 );
190 assert( b_input_7_0 || b_input_5_0 );
193 for( i = p_in_buf->i_nb_samples; i--; )
195 *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6;
197 *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
199 *p_dest = p_src[2] / 6 + p_src[4];
201 *p_dest = p_src[3] / 6 + p_src[5];
206 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
209 for( i = p_in_buf->i_nb_samples; i--; )
211 *p_dest = p_src[4] + 0.5 * p_src[0];
213 *p_dest = p_src[4] + 0.5 * p_src[1];
222 if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
227 /*****************************************************************************
229 *****************************************************************************/
230 static int OpenFilter( vlc_object_t *p_this )
232 filter_t *p_filter = (filter_t *)p_this;
234 audio_format_t fmt_in = p_filter->fmt_in.audio;
235 audio_format_t fmt_out = p_filter->fmt_out.audio;
237 fmt_in.i_format = p_filter->fmt_in.i_codec;
238 fmt_out.i_format = p_filter->fmt_out.i_codec;
240 if( !IsSupported( &fmt_in, &fmt_out ) )
243 p_filter->pf_audio_filter = Filter;
248 /*****************************************************************************
250 *****************************************************************************/
251 static block_t *Filter( filter_t *p_filter, block_t *p_block )
253 if( !p_block || !p_block->i_nb_samples )
256 block_Release( p_block );
260 size_t i_out_size = p_block->i_nb_samples *
261 p_filter->fmt_out.audio.i_bitspersample *
262 p_filter->fmt_out.audio.i_channels / 8;
264 block_t *p_out = filter_NewAudioBuffer( p_filter, i_out_size );
267 msg_Warn( p_filter, "can't get output buffer" );
268 block_Release( p_block );
272 p_out->i_nb_samples = p_block->i_nb_samples;
273 p_out->i_dts = p_block->i_dts;
274 p_out->i_pts = p_block->i_pts;
275 p_out->i_length = p_block->i_length;
277 DoWork( p_filter, p_block, p_out );
279 block_Release( p_block );
284 /*****************************************************************************
286 *****************************************************************************/
287 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output )
289 if( p_input->i_format != VLC_CODEC_FL32 ||
290 p_input->i_format != p_output->i_format ||
291 p_input->i_rate != p_output->i_rate )
294 if( p_input->i_physical_channels == p_output->i_physical_channels &&
295 p_input->i_original_channels == p_output->i_original_channels )
300 /* Only conversion to Mono, Stereo and 4.0 right now */
301 if( p_output->i_physical_channels != AOUT_CHAN_CENTER &&
302 p_output->i_physical_channels != AOUT_CHANS_2_0 &&
303 p_output->i_physical_channels != AOUT_CHANS_4_0 )
308 /* Only from 7/7.1/5/5.1/3/3.1/2.0
309 * XXX 5.X rear and middle are handled the same way */
310 if( (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_7_0 &&
311 (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0 &&
312 (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0_MIDDLE &&
313 (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_3_0 &&
314 (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_4_CENTER_REAR &&
315 p_input->i_physical_channels != AOUT_CHANS_2_0 )
320 /* Only if we downmix */
321 if( aout_FormatNbChannels( p_input ) <= aout_FormatNbChannels( p_output ) )