1 /*****************************************************************************
2 * mono.c : stereo2mono downmixsimple channel mixer plug-in
3 *****************************************************************************
4 * Copyright (C) 2006 M2X
7 * Authors: Jean-Paul Saman <jpsaman at m2x dot nl>
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 <math.h> /* sqrt */
32 #include <stdint.h> /* int16_t .. */
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 #include <vlc_block.h>
41 #include <vlc_filter.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int OpenFilter ( vlc_object_t * );
48 static void CloseFilter ( vlc_object_t * );
50 static block_t *Convert( filter_t *p_filter, block_t *p_block );
52 static unsigned int stereo_to_mono( filter_t *, block_t *, block_t * );
53 static unsigned int mono( filter_t *, block_t *, block_t * );
54 static void stereo2mono_downmix( filter_t *, block_t *, block_t * );
56 /*****************************************************************************
58 *****************************************************************************/
59 struct atomic_operation_t
61 int i_source_channel_offset;
62 int i_dest_channel_offset;
63 unsigned int i_delay;/* in sample unit */
64 double d_amplitude_factor;
71 unsigned int i_nb_channels; /* number of int16_t per sample */
72 int i_channel_selected;
75 size_t i_overflow_buffer_size;/* in bytes */
76 uint8_t * p_overflow_buffer;
77 unsigned int i_nb_atomic_operations;
78 struct atomic_operation_t * p_atomic_operations;
81 #define MONO_DOWNMIX_TEXT N_("Use downmix algorithm")
82 #define MONO_DOWNMIX_LONGTEXT N_("This option selects a stereo to mono " \
83 "downmix algorithm that is used in the headphone channel mixer. It " \
84 "gives the effect of standing in a room full of speakers." )
86 #define MONO_CHANNEL_TEXT N_("Select channel to keep")
87 #define MONO_CHANNEL_LONGTEXT N_("This option silences all other channels " \
88 "except the selected channel.")
90 static const int pi_pos_values[] = { 0, 1, 4, 5, 7, 8, 2, 3, 6 };
91 static const char *const ppsz_pos_descriptions[] =
92 { N_("Left"), N_("Right"),
93 N_("Rear left"), N_("Rear right"),
94 N_("Center"), N_("Low-frequency effects"),
95 N_("Side left"), N_("Side right"), N_("Rear center") };
97 #define MONO_CFG "sout-mono-"
98 /*****************************************************************************
100 *****************************************************************************/
102 set_description( N_("Audio filter for stereo to mono conversion") )
103 set_capability( "audio filter", 0 )
104 set_category( CAT_AUDIO )
105 set_subcategory( SUBCAT_AUDIO_AFILTER )
106 set_callbacks( OpenFilter, CloseFilter )
107 set_shortname( "Mono" )
109 add_bool( MONO_CFG "downmix", true, MONO_DOWNMIX_TEXT,
110 MONO_DOWNMIX_LONGTEXT, false )
111 add_integer( MONO_CFG "channel", -1, MONO_CHANNEL_TEXT,
112 MONO_CHANNEL_LONGTEXT, false )
113 change_integer_list( pi_pos_values, ppsz_pos_descriptions )
117 /* Init() and ComputeChannelOperations() -
118 * Code taken from modules/audio_filter/channel_mixer/headphone.c
119 * converted from float into int16_t based downmix
120 * Written by Boris Dorès <babal@via.ecp.fr>
123 /*****************************************************************************
124 * Init: initialize internal data structures
125 * and computes the needed atomic operations
126 *****************************************************************************/
127 /* x and z represent the coordinates of the virtual speaker
128 * relatively to the center of the listener's head, measured in meters :
137 * rear left rear right
141 static void ComputeChannelOperations( struct filter_sys_t * p_data,
142 unsigned int i_rate, unsigned int i_next_atomic_operation,
143 int i_source_channel_offset, double d_x, double d_z,
144 double d_compensation_length, double d_channel_amplitude_factor )
146 double d_c = 340; /*sound celerity (unit: m/s)*/
147 double d_compensation_delay = (d_compensation_length-0.1) / d_c * i_rate;
150 p_data->p_atomic_operations[i_next_atomic_operation]
151 .i_source_channel_offset = i_source_channel_offset;
152 p_data->p_atomic_operations[i_next_atomic_operation]
153 .i_dest_channel_offset = 0;/* left */
154 p_data->p_atomic_operations[i_next_atomic_operation]
155 .i_delay = (int)( sqrt( (-0.1-d_x)*(-0.1-d_x) + (0-d_z)*(0-d_z) )
156 / d_c * i_rate - d_compensation_delay );
159 p_data->p_atomic_operations[i_next_atomic_operation]
160 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
164 p_data->p_atomic_operations[i_next_atomic_operation]
165 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
169 p_data->p_atomic_operations[i_next_atomic_operation]
170 .d_amplitude_factor = d_channel_amplitude_factor / 2;
174 p_data->p_atomic_operations[i_next_atomic_operation + 1]
175 .i_source_channel_offset = i_source_channel_offset;
176 p_data->p_atomic_operations[i_next_atomic_operation + 1]
177 .i_dest_channel_offset = 1;/* right */
178 p_data->p_atomic_operations[i_next_atomic_operation + 1]
179 .i_delay = (int)( sqrt( (0.1-d_x)*(0.1-d_x) + (0-d_z)*(0-d_z) )
180 / d_c * i_rate - d_compensation_delay );
183 p_data->p_atomic_operations[i_next_atomic_operation + 1]
184 .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
188 p_data->p_atomic_operations[i_next_atomic_operation + 1]
189 .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
193 p_data->p_atomic_operations[i_next_atomic_operation + 1]
194 .d_amplitude_factor = d_channel_amplitude_factor / 2;
198 static int Init( vlc_object_t *p_this, struct filter_sys_t * p_data,
199 unsigned int i_nb_channels, uint32_t i_physical_channels,
200 unsigned int i_rate )
202 double d_x = var_InheritInteger( p_this, "headphone-dim" );
204 double d_z_rear = -d_x/3;
206 unsigned int i_next_atomic_operation;
207 int i_source_channel_offset;
210 if( var_InheritBool( p_this, "headphone-compensate" ) )
212 /* minimal distance to any speaker */
213 if( i_physical_channels & AOUT_CHAN_REARCENTER )
223 /* Number of elementary operations */
224 p_data->i_nb_atomic_operations = i_nb_channels * 2;
225 if( i_physical_channels & AOUT_CHAN_CENTER )
227 p_data->i_nb_atomic_operations += 2;
229 p_data->p_atomic_operations = malloc( sizeof(struct atomic_operation_t)
230 * p_data->i_nb_atomic_operations );
231 if( p_data->p_atomic_operations == NULL )
234 /* For each virtual speaker, computes elementary wave propagation time
236 i_next_atomic_operation = 0;
237 i_source_channel_offset = 0;
238 if( i_physical_channels & AOUT_CHAN_LEFT )
240 ComputeChannelOperations( p_data , i_rate
241 , i_next_atomic_operation , i_source_channel_offset
242 , -d_x , d_z , d_min , 2.0 / i_nb_channels );
243 i_next_atomic_operation += 2;
244 i_source_channel_offset++;
246 if( i_physical_channels & AOUT_CHAN_RIGHT )
248 ComputeChannelOperations( p_data , i_rate
249 , i_next_atomic_operation , i_source_channel_offset
250 , d_x , d_z , d_min , 2.0 / i_nb_channels );
251 i_next_atomic_operation += 2;
252 i_source_channel_offset++;
254 if( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
256 ComputeChannelOperations( p_data , i_rate
257 , i_next_atomic_operation , i_source_channel_offset
258 , -d_x , 0 , d_min , 1.5 / i_nb_channels );
259 i_next_atomic_operation += 2;
260 i_source_channel_offset++;
262 if( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
264 ComputeChannelOperations( p_data , i_rate
265 , i_next_atomic_operation , i_source_channel_offset
266 , d_x , 0 , d_min , 1.5 / i_nb_channels );
267 i_next_atomic_operation += 2;
268 i_source_channel_offset++;
270 if( i_physical_channels & AOUT_CHAN_REARLEFT )
272 ComputeChannelOperations( p_data , i_rate
273 , i_next_atomic_operation , i_source_channel_offset
274 , -d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
275 i_next_atomic_operation += 2;
276 i_source_channel_offset++;
278 if( i_physical_channels & AOUT_CHAN_REARRIGHT )
280 ComputeChannelOperations( p_data , i_rate
281 , i_next_atomic_operation , i_source_channel_offset
282 , d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
283 i_next_atomic_operation += 2;
284 i_source_channel_offset++;
286 if( i_physical_channels & AOUT_CHAN_REARCENTER )
288 ComputeChannelOperations( p_data , i_rate
289 , i_next_atomic_operation , i_source_channel_offset
290 , 0 , -d_z , d_min , 1.5 / i_nb_channels );
291 i_next_atomic_operation += 2;
292 i_source_channel_offset++;
294 if( i_physical_channels & AOUT_CHAN_CENTER )
296 /* having two center channels increases the spatialization effect */
297 ComputeChannelOperations( p_data , i_rate
298 , i_next_atomic_operation , i_source_channel_offset
299 , d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
300 i_next_atomic_operation += 2;
301 ComputeChannelOperations( p_data , i_rate
302 , i_next_atomic_operation , i_source_channel_offset
303 , -d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
304 i_next_atomic_operation += 2;
305 i_source_channel_offset++;
307 if( i_physical_channels & AOUT_CHAN_LFE )
309 ComputeChannelOperations( p_data , i_rate
310 , i_next_atomic_operation , i_source_channel_offset
311 , 0 , d_z_rear , d_min , 5.0 / i_nb_channels );
312 i_next_atomic_operation += 2;
313 i_source_channel_offset++;
316 /* Initialize the overflow buffer
317 * we need it because the process induce a delay in the samples */
318 p_data->i_overflow_buffer_size = 0;
319 for( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
321 if( p_data->i_overflow_buffer_size
322 < p_data->p_atomic_operations[i].i_delay * 2 * sizeof (int16_t) )
324 p_data->i_overflow_buffer_size
325 = p_data->p_atomic_operations[i].i_delay * 2 * sizeof (int16_t);
328 p_data->p_overflow_buffer = malloc( p_data->i_overflow_buffer_size );
329 if( p_data->p_overflow_buffer == NULL )
331 free( p_data->p_atomic_operations );
334 memset( p_data->p_overflow_buffer, 0, p_data->i_overflow_buffer_size );
340 /*****************************************************************************
342 *****************************************************************************/
343 static int OpenFilter( vlc_object_t *p_this )
345 filter_t * p_filter = (filter_t *)p_this;
346 filter_sys_t *p_sys = NULL;
348 if( aout_FormatNbChannels( &(p_filter->fmt_in.audio) ) == 1 )
350 /*msg_Dbg( p_filter, "filter discarded (incompatible format)" );*/
354 if( (p_filter->fmt_in.audio.i_format != p_filter->fmt_out.audio.i_format) ||
355 (p_filter->fmt_in.audio.i_format != VLC_CODEC_S16N) ||
356 (p_filter->fmt_out.audio.i_format != VLC_CODEC_S16N) )
358 /*msg_Err( p_this, "couldn't load mono filter" );*/
362 /* Allocate the memory needed to store the module's structure */
363 p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
367 p_sys->b_downmix = var_CreateGetBool( p_this, MONO_CFG "downmix" );
368 p_sys->i_channel_selected =
369 (unsigned int) var_CreateGetInteger( p_this, MONO_CFG "channel" );
371 p_sys->i_nb_channels = aout_FormatNbChannels( &(p_filter->fmt_in.audio) );
372 p_sys->i_bitspersample = p_filter->fmt_out.audio.i_bitspersample;
374 p_sys->i_overflow_buffer_size = 0;
375 p_sys->p_overflow_buffer = NULL;
376 p_sys->i_nb_atomic_operations = 0;
377 p_sys->p_atomic_operations = NULL;
379 if( Init( VLC_OBJECT(p_filter), p_filter->p_sys,
380 aout_FormatNbChannels( &p_filter->fmt_in.audio ),
381 p_filter->fmt_in.audio.i_physical_channels,
382 p_filter->fmt_in.audio.i_rate ) < 0 )
384 var_Destroy( p_this, MONO_CFG "channel" );
385 var_Destroy( p_this, MONO_CFG "downmix" );
390 if( p_sys->b_downmix )
392 msg_Dbg( p_this, "using stereo to mono downmix" );
393 p_filter->fmt_out.audio.i_physical_channels = AOUT_CHAN_CENTER;
394 p_filter->fmt_out.audio.i_channels = 1;
398 msg_Dbg( p_this, "using pseudo mono" );
399 p_filter->fmt_out.audio.i_physical_channels = AOUT_CHANS_STEREO;
400 p_filter->fmt_out.audio.i_channels = 2;
402 p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
403 p_filter->pf_audio_filter = Convert;
405 msg_Dbg( p_this, "%4.4s->%4.4s, channels %d->%d, bits per sample: %i->%i",
406 (char *)&p_filter->fmt_in.i_codec,
407 (char *)&p_filter->fmt_out.i_codec,
408 p_filter->fmt_in.audio.i_physical_channels,
409 p_filter->fmt_out.audio.i_physical_channels,
410 p_filter->fmt_in.audio.i_bitspersample,
411 p_filter->fmt_out.audio.i_bitspersample );
416 /*****************************************************************************
418 *****************************************************************************/
419 static void CloseFilter( vlc_object_t *p_this)
421 filter_t *p_filter = (filter_t *) p_this;
422 filter_sys_t *p_sys = p_filter->p_sys;
424 var_Destroy( p_this, MONO_CFG "channel" );
425 var_Destroy( p_this, MONO_CFG "downmix" );
426 free( p_sys->p_atomic_operations );
427 free( p_sys->p_overflow_buffer );
431 /*****************************************************************************
433 *****************************************************************************/
434 static block_t *Convert( filter_t *p_filter, block_t *p_block )
439 if( !p_block || !p_block->i_nb_samples )
442 block_Release( p_block );
446 i_out_size = p_block->i_nb_samples * p_filter->p_sys->i_bitspersample/8 *
447 aout_FormatNbChannels( &(p_filter->fmt_out.audio) );
449 p_out = block_Alloc( i_out_size );
452 msg_Warn( p_filter, "can't get output buffer" );
453 block_Release( p_block );
456 p_out->i_nb_samples =
457 (p_block->i_nb_samples / p_filter->p_sys->i_nb_channels) *
458 aout_FormatNbChannels( &(p_filter->fmt_out.audio) );
461 unsigned int i_in_size = in_buf.i_nb_samples * (p_filter->p_sys->i_bitspersample/8) *
462 aout_FormatNbChannels( &(p_filter->fmt_in.audio) );
463 if( (in_buf.i_buffer != i_in_size) && ((i_in_size % 32) != 0) ) /* is it word aligned?? */
465 msg_Err( p_filter, "input buffer is not word aligned" );
466 /* Fix output buffer to be word aligned */
470 memset( p_out->p_buffer, 0, i_out_size );
471 if( p_filter->p_sys->b_downmix )
473 stereo2mono_downmix( p_filter, p_block, p_out );
474 mono( p_filter, p_out, p_block );
478 stereo_to_mono( p_filter, p_out, p_block );
481 block_Release( p_block );
485 /* stereo2mono_downmix - stereo channels into one mono channel.
486 * Code taken from modules/audio_filter/channel_mixer/headphone.c
487 * converted from float into int16_t based downmix
488 * Written by Boris Dorès <babal@via.ecp.fr>
490 static void stereo2mono_downmix( filter_t * p_filter,
491 block_t * p_in_buf, block_t * p_out_buf )
493 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
495 int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio );
496 int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio );
498 int16_t * p_in = (int16_t*) p_in_buf->p_buffer;
500 uint8_t * p_overflow;
503 size_t i_overflow_size; /* in bytes */
504 size_t i_out_size; /* in bytes */
508 int i_source_channel_offset;
509 int i_dest_channel_offset;
510 unsigned int i_delay;
511 double d_amplitude_factor;
513 /* out buffer characterisitcs */
514 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
515 p_out_buf->i_buffer = p_in_buf->i_buffer * i_output_nb / i_input_nb;
516 p_out = p_out_buf->p_buffer;
517 i_out_size = p_out_buf->i_buffer;
519 /* Slide the overflow buffer */
520 p_overflow = p_sys->p_overflow_buffer;
521 i_overflow_size = p_sys->i_overflow_buffer_size;
523 if ( i_out_size > i_overflow_size )
524 memcpy( p_out, p_overflow, i_overflow_size );
526 memcpy( p_out, p_overflow, i_out_size );
528 p_slide = p_sys->p_overflow_buffer;
529 while( p_slide < p_overflow + i_overflow_size )
531 if( p_slide + i_out_size < p_overflow + i_overflow_size )
533 memset( p_slide, 0, i_out_size );
534 if( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
535 memcpy( p_slide, p_slide + i_out_size, i_out_size );
537 memcpy( p_slide, p_slide + i_out_size,
538 p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
542 memset( p_slide, 0, p_overflow + i_overflow_size - p_slide );
544 p_slide += i_out_size;
547 /* apply the atomic operations */
548 for( i = 0; i < p_sys->i_nb_atomic_operations; i++ )
550 /* shorter variable names */
551 i_source_channel_offset
552 = p_sys->p_atomic_operations[i].i_source_channel_offset;
553 i_dest_channel_offset
554 = p_sys->p_atomic_operations[i].i_dest_channel_offset;
555 i_delay = p_sys->p_atomic_operations[i].i_delay;
557 = p_sys->p_atomic_operations[i].d_amplitude_factor;
559 if( p_out_buf->i_nb_samples > i_delay )
561 /* current buffer coefficients */
562 for( j = 0; j < p_out_buf->i_nb_samples - i_delay; j++ )
564 ((int16_t*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
565 += p_in[ j * i_input_nb + i_source_channel_offset ]
566 * d_amplitude_factor;
569 /* overflow buffer coefficients */
570 for( j = 0; j < i_delay; j++ )
572 ((int16_t*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
573 += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
574 * i_input_nb + i_source_channel_offset ]
575 * d_amplitude_factor;
580 /* overflow buffer coefficients only */
581 for( j = 0; j < p_out_buf->i_nb_samples; j++ )
583 ((int16_t*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
584 * i_output_nb + i_dest_channel_offset ]
585 += p_in[ j * i_input_nb + i_source_channel_offset ]
586 * d_amplitude_factor;
592 /* Simple stereo to mono mixing. */
593 static unsigned int mono( filter_t *p_filter,
594 block_t *p_output, block_t *p_input )
596 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
597 int16_t *p_in, *p_out;
598 unsigned int n = 0, r = 0;
600 p_in = (int16_t *) p_input->p_buffer;
601 p_out = (int16_t *) p_output->p_buffer;
603 while( n < (p_input->i_nb_samples * p_sys->i_nb_channels) )
605 p_out[r] = (p_in[n] + p_in[n+1]) >> 1;
612 /* Simple stereo to mono mixing. */
613 static unsigned int stereo_to_mono( filter_t *p_filter,
614 block_t *p_output, block_t *p_input )
616 filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
617 int16_t *p_in, *p_out;
620 p_in = (int16_t *) p_input->p_buffer;
621 p_out = (int16_t *) p_output->p_buffer;
623 for( n = 0; n < (p_input->i_nb_samples * p_sys->i_nb_channels); n++ )
625 /* Fake real mono. */
626 if( p_sys->i_channel_selected == -1)
628 p_out[n] = p_out[n+1] = (p_in[n] + p_in[n+1]) >> 1;
631 else if( (n % p_sys->i_nb_channels) == (unsigned int) p_sys->i_channel_selected )
633 p_out[n] = p_out[n+1] = p_in[n];