1 /*****************************************************************************
2 * simple.c : simple channel mixer plug-in (only 7/7.1/5/5.1 -> Stereo for now)
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>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Create ( vlc_object_t * );
41 static int OpenFilter( vlc_object_t * );
44 set_description( N_("Audio filter for simple channel mixing") );
45 set_capability( "audio filter", 10 );
46 set_category( CAT_AUDIO );
47 set_subcategory( SUBCAT_AUDIO_MISC );
48 set_callbacks( Create, NULL );
51 set_description( N_("audio filter for simple channel mixing") );
52 set_capability( "audio filter2", 10 );
53 set_callbacks( OpenFilter, NULL );
56 /*****************************************************************************
58 *****************************************************************************/
59 #define AOUT_CHANS_STEREO_FRONT ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT )
60 #define AOUT_CHANS_STEREO_REAR ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT )
61 #define AOUT_CHANS_STEREO_MIDDLE (AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT )
63 #define AOUT_CHANS_2_0 AOUT_CHANS_STEREO_FRONT
64 #define AOUT_CHANS_4_0 (AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR )
65 #define AOUT_CHANS_5_0 ( AOUT_CHANS_4_0 | AOUT_CHAN_CENTER )
66 #define AOUT_CHANS_6_0 (AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR | AOUT_CHANS_STEREO_MIDDLE )
67 #define AOUT_CHANS_7_0 ( AOUT_CHANS_6_0 | AOUT_CHAN_CENTER )
69 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output );
71 static void DoWork ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
74 static block_t *Filter( filter_t *, block_t * );
76 /*****************************************************************************
77 * Create: allocate trivial channel mixer
78 *****************************************************************************/
79 static int Create( vlc_object_t *p_this )
81 aout_filter_t * p_filter = (aout_filter_t *)p_this;
83 if( !IsSupported( &p_filter->input, &p_filter->output ) )
86 p_filter->pf_do_work = DoWork;
87 p_filter->b_in_place = 0;
92 /*****************************************************************************
93 * DoWork: convert a buffer
94 *****************************************************************************/
95 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
96 aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
99 int i_input_nb = aout_FormatNbChannels( &p_filter->input );
100 int i_output_nb = aout_FormatNbChannels( &p_filter->output );
101 float *p_dest = (float *)p_out_buf->p_buffer;
102 const float *p_src = (const float *)p_in_buf->p_buffer;
105 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
106 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
108 if( p_filter->output.i_physical_channels == AOUT_CHANS_2_0 )
110 if( p_filter->input.i_physical_channels & AOUT_CHAN_MIDDLELEFT )
111 for( i = p_in_buf->i_nb_samples; i--; )
113 *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4;
115 *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4;
120 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
123 for( i = p_in_buf->i_nb_samples; i--; )
125 *p_dest = p_src[4] + 0.5 * p_src[0] + 0.33 * p_src[2];
127 *p_dest = p_src[4] + 0.5 * p_src[1] + 0.33 * p_src[3];
132 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
135 else if( p_filter->output.i_physical_channels == AOUT_CHAN_CENTER )
137 if( p_filter->input.i_physical_channels & AOUT_CHAN_MIDDLELEFT )
138 for( i = p_in_buf->i_nb_samples; i--; )
140 *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;
145 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
147 else if( p_filter->input.i_physical_channels & AOUT_CHAN_REARLEFT )
148 for( i = p_in_buf->i_nb_samples; i--; )
150 *p_dest = p_src[4] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 6 + p_src[3] / 6;
155 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
158 for( i = p_in_buf->i_nb_samples; i--; )
160 *p_dest = p_src[0] / 2 + p_src[1] / 2;
168 if( p_filter->input.i_physical_channels & AOUT_CHAN_MIDDLELEFT )
169 for( i = p_in_buf->i_nb_samples; i--; )
171 *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6;
173 *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6;
175 *p_dest = p_src[2] / 6 + p_src[4];
177 *p_dest = p_src[3] / 6 + p_src[5];
182 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
185 for( i = p_in_buf->i_nb_samples; i--; )
187 *p_dest = p_src[4] + 0.5 * p_src[0];
189 *p_dest = p_src[4] + 0.5 * p_src[1];
198 if( p_filter->input.i_physical_channels & AOUT_CHAN_LFE ) p_src++;
203 /*****************************************************************************
205 *****************************************************************************/
206 static int OpenFilter( vlc_object_t *p_this )
208 filter_t *p_filter = (filter_t *)p_this;
210 audio_format_t fmt_in = p_filter->fmt_in.audio;
211 audio_format_t fmt_out = p_filter->fmt_out.audio;
213 fmt_in.i_format = p_filter->fmt_in.i_codec;
214 fmt_out.i_format = p_filter->fmt_out.i_codec;
216 if( !IsSupported( &fmt_in, &fmt_out ) )
219 p_filter->pf_audio_filter = Filter;
224 /*****************************************************************************
226 *****************************************************************************/
227 static block_t *Filter( filter_t *p_filter, block_t *p_block )
229 aout_filter_t aout_filter;
230 aout_buffer_t in_buf, out_buf;
234 if( !p_block || !p_block->i_samples )
236 if( p_block ) p_block->pf_release( p_block );
240 i_out_size = p_block->i_samples *
241 p_filter->fmt_out.audio.i_bitspersample *
242 p_filter->fmt_out.audio.i_channels / 8;
244 p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
247 msg_Warn( p_filter, "can't get output buffer" );
248 p_block->pf_release( p_block );
252 p_out->i_samples = p_block->i_samples;
253 p_out->i_dts = p_block->i_dts;
254 p_out->i_pts = p_block->i_pts;
255 p_out->i_length = p_block->i_length;
257 aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
258 aout_filter.input = p_filter->fmt_in.audio;
259 aout_filter.input.i_format = p_filter->fmt_in.i_codec;
260 aout_filter.output = p_filter->fmt_out.audio;
261 aout_filter.output.i_format = p_filter->fmt_out.i_codec;
263 in_buf.p_buffer = p_block->p_buffer;
264 in_buf.i_nb_bytes = p_block->i_buffer;
265 in_buf.i_nb_samples = p_block->i_samples;
266 out_buf.p_buffer = p_out->p_buffer;
267 out_buf.i_nb_bytes = p_out->i_buffer;
268 out_buf.i_nb_samples = p_out->i_samples;
270 DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
272 p_block->pf_release( p_block );
274 p_out->i_buffer = out_buf.i_nb_bytes;
275 p_out->i_samples = out_buf.i_nb_samples;
280 /*****************************************************************************
282 *****************************************************************************/
283 static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output )
285 if ( (p_input->i_physical_channels
286 == p_output->i_physical_channels
287 && p_input->i_original_channels
288 == p_output->i_original_channels)
289 || p_input->i_format != p_output->i_format
290 || p_input->i_rate != p_output->i_rate
291 || p_input->i_format != VLC_FOURCC('f','l','3','2') )
296 /* Only conversion to Mono, Stereo and 4.0 right now */
297 if( p_output->i_physical_channels != AOUT_CHAN_CENTER &&
298 p_output->i_physical_channels != AOUT_CHANS_2_0 &&
299 p_output->i_physical_channels != AOUT_CHANS_4_0 )
304 /* Only from 7/7.1/5/5.1/2.0 */
305 if( (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_7_0 &&
306 (p_input->i_physical_channels & ~AOUT_CHAN_LFE) != AOUT_CHANS_5_0 &&
307 p_input->i_physical_channels != AOUT_CHANS_2_0 )