]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/mono.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / audio_filter / channel_mixer / mono.c
1 /*****************************************************************************
2  * mono.c : stereo2mono downmixsimple channel mixer plug-in
3  *****************************************************************************
4  * Copyright (C) 2006 M2X
5  * $Id$
6  *
7  * Authors: Jean-Paul Saman <jpsaman at m2x dot nl>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <math.h>                                        /* sqrt */
28
29 #ifdef HAVE_STDINT_H
30 #   include <stdint.h>                                         /* int16_t .. */
31 #elif defined(HAVE_INTTYPES_H)
32 #   include <inttypes.h>                                       /* int16_t .. */
33 #endif
34
35 #ifdef HAVE_UNISTD_H
36 #   include <unistd.h>
37 #endif
38
39 #include <vlc/vlc.h>
40 #include <vlc_es.h>
41 #include <vlc_block.h>
42 #include <vlc_filter.h>
43 #include <vlc_aout.h>
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int  OpenFilter    ( vlc_object_t * );
49 static void CloseFilter   ( vlc_object_t * );
50
51 static block_t *Convert( filter_t *p_filter, block_t *p_block );
52
53 static unsigned int stereo_to_mono( aout_instance_t *, aout_filter_t *,
54                                     aout_buffer_t *, aout_buffer_t * );
55 static unsigned int mono( aout_instance_t *, aout_filter_t *,
56                                     aout_buffer_t *, aout_buffer_t * );
57 static void stereo2mono_downmix( aout_instance_t *, aout_filter_t *,
58                                  aout_buffer_t *, aout_buffer_t * );
59
60 /*****************************************************************************
61  * Local structures
62  *****************************************************************************/
63 struct atomic_operation_t
64 {
65     int i_source_channel_offset;
66     int i_dest_channel_offset;
67     unsigned int i_delay;/* in sample unit */
68     double d_amplitude_factor;
69 };
70
71 struct filter_sys_t
72 {
73     vlc_bool_t b_downmix;
74
75     unsigned int i_nb_channels; /* number of int16_t per sample */
76     int i_channel_selected;
77     int i_bitspersample;
78
79     size_t i_overflow_buffer_size;/* in bytes */
80     byte_t * p_overflow_buffer;
81     unsigned int i_nb_atomic_operations;
82     struct atomic_operation_t * p_atomic_operations;
83 };
84
85 #define MONO_DOWNMIX_TEXT N_("Use downmix algorithm")
86 #define MONO_DOWNMIX_LONGTEXT N_("This option selects a stereo to mono " \
87     "downmix algorithm that is used in the headphone channel mixer. It" \
88     "gives the effect of standing in a room full of speakers." )
89
90 #define MONO_CHANNEL_TEXT N_("Select channel to keep")
91 #define MONO_CHANNEL_LONGTEXT N_("This option silences all other channels " \
92     "except the selected channel. Choose one from (0=left, 1=right, " \
93     "2=rear left, 3=rear right, 4=center, 5=left front)")
94
95 static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5 };
96 static const char *ppsz_pos_descriptions[] =
97 { N_("Left"), N_("Right"), N_("Left rear"), N_("Right rear"), N_("Center"),
98   N_("Left front") };
99
100 /* our internal channel order (WG-4 order) */
101 static const uint32_t pi_channels_out[] =
102 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
103   AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
104
105 #define MONO_CFG "sout-mono-"
106 /*****************************************************************************
107  * Module descriptor
108  *****************************************************************************/
109 vlc_module_begin();
110     set_description( _("Audio filter for stereo to mono conversion") );
111     set_capability( "audio filter2", 0 );
112
113     add_bool( MONO_CFG "downmix", VLC_FALSE, NULL, MONO_DOWNMIX_TEXT,
114               MONO_DOWNMIX_LONGTEXT, VLC_FALSE );
115         change_safe();
116
117     add_integer( MONO_CFG "channel", -1, NULL, MONO_CHANNEL_TEXT,
118         MONO_CHANNEL_LONGTEXT, VLC_FALSE );
119         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
120         change_safe();
121
122     set_category( CAT_AUDIO );
123     set_subcategory( SUBCAT_AUDIO_MISC );
124     set_callbacks( OpenFilter, CloseFilter );
125     set_shortname( "Mono" );
126 vlc_module_end();
127
128 /* Init() and ComputeChannelOperations() -
129  * Code taken from modules/audio_filter/channel_mixer/headphone.c
130  * converted from float into int16_t based downmix
131  * Written by Boris Dorès <babal@via.ecp.fr>
132  */
133
134 /*****************************************************************************
135  * Init: initialize internal data structures
136  * and computes the needed atomic operations
137  *****************************************************************************/
138 /* x and z represent the coordinates of the virtual speaker
139  *  relatively to the center of the listener's head, measured in meters :
140  *
141  *  left              right
142  *Z
143  *-
144  *a          head
145  *x
146  *i
147  *s
148  *  rear left    rear right
149  *
150  *          x-axis
151  *  */
152 static void ComputeChannelOperations( struct filter_sys_t * p_data,
153         unsigned int i_rate, unsigned int i_next_atomic_operation,
154         int i_source_channel_offset, double d_x, double d_z,
155         double d_compensation_length, double d_channel_amplitude_factor )
156 {
157     double d_c = 340; /*sound celerity (unit: m/s)*/
158     double d_compensation_delay = (d_compensation_length-0.1) / d_c * i_rate;
159
160     /* Left ear */
161     p_data->p_atomic_operations[i_next_atomic_operation]
162         .i_source_channel_offset = i_source_channel_offset;
163     p_data->p_atomic_operations[i_next_atomic_operation]
164         .i_dest_channel_offset = 0;/* left */
165     p_data->p_atomic_operations[i_next_atomic_operation]
166         .i_delay = (int)( sqrt( (-0.1-d_x)*(-0.1-d_x) + (0-d_z)*(0-d_z) )
167                           / d_c * i_rate - d_compensation_delay );
168     if( d_x < 0 )
169     {
170         p_data->p_atomic_operations[i_next_atomic_operation]
171             .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
172     }
173     else if( d_x > 0 )
174     {
175         p_data->p_atomic_operations[i_next_atomic_operation]
176             .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
177     }
178     else
179     {
180         p_data->p_atomic_operations[i_next_atomic_operation]
181             .d_amplitude_factor = d_channel_amplitude_factor / 2;
182     }
183
184     /* Right ear */
185     p_data->p_atomic_operations[i_next_atomic_operation + 1]
186         .i_source_channel_offset = i_source_channel_offset;
187     p_data->p_atomic_operations[i_next_atomic_operation + 1]
188         .i_dest_channel_offset = 1;/* right */
189     p_data->p_atomic_operations[i_next_atomic_operation + 1]
190         .i_delay = (int)( sqrt( (0.1-d_x)*(0.1-d_x) + (0-d_z)*(0-d_z) )
191                           / d_c * i_rate - d_compensation_delay );
192     if( d_x < 0 )
193     {
194         p_data->p_atomic_operations[i_next_atomic_operation + 1]
195             .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
196     }
197     else if( d_x > 0 )
198     {
199         p_data->p_atomic_operations[i_next_atomic_operation + 1]
200             .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
201     }
202     else
203     {
204         p_data->p_atomic_operations[i_next_atomic_operation + 1]
205             .d_amplitude_factor = d_channel_amplitude_factor / 2;
206     }
207 }
208
209 static int Init( vlc_object_t *p_this, struct filter_sys_t * p_data,
210                  unsigned int i_nb_channels, uint32_t i_physical_channels,
211                  unsigned int i_rate )
212 {
213     double d_x = config_GetInt( p_this, "headphone-dim" );
214     double d_z = d_x;
215     double d_z_rear = -d_x/3;
216     double d_min = 0;
217     unsigned int i_next_atomic_operation;
218     int i_source_channel_offset;
219     unsigned int i;
220
221     if( p_data == NULL )
222     {
223         msg_Dbg( p_this, "passing a null pointer as argument" );
224         return 0;
225     }
226
227     if( config_GetInt( p_this, "headphone-compensate" ) )
228     {
229         /* minimal distance to any speaker */
230         if( i_physical_channels & AOUT_CHAN_REARCENTER )
231         {
232             d_min = d_z_rear;
233         }
234         else
235         {
236             d_min = d_z;
237         }
238     }
239
240     /* Number of elementary operations */
241     p_data->i_nb_atomic_operations = i_nb_channels * 2;
242     if( i_physical_channels & AOUT_CHAN_CENTER )
243     {
244         p_data->i_nb_atomic_operations += 2;
245     }
246     p_data->p_atomic_operations = malloc( sizeof(struct atomic_operation_t)
247             * p_data->i_nb_atomic_operations );
248     if( p_data->p_atomic_operations == NULL )
249     {
250         msg_Err( p_this, "out of memory" );
251         return -1;
252     }
253
254     /* For each virtual speaker, computes elementary wave propagation time
255      * to each ear */
256     i_next_atomic_operation = 0;
257     i_source_channel_offset = 0;
258     if( i_physical_channels & AOUT_CHAN_LEFT )
259     {
260         ComputeChannelOperations( p_data , i_rate
261                 , i_next_atomic_operation , i_source_channel_offset
262                 , -d_x , d_z , d_min , 2.0 / i_nb_channels );
263         i_next_atomic_operation += 2;
264         i_source_channel_offset++;
265     }
266     if( i_physical_channels & AOUT_CHAN_RIGHT )
267     {
268         ComputeChannelOperations( p_data , i_rate
269                 , i_next_atomic_operation , i_source_channel_offset
270                 , d_x , d_z , d_min , 2.0 / i_nb_channels );
271         i_next_atomic_operation += 2;
272         i_source_channel_offset++;
273     }
274     if( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
275     {
276         ComputeChannelOperations( p_data , i_rate
277                 , i_next_atomic_operation , i_source_channel_offset
278                 , -d_x , 0 , d_min , 1.5 / i_nb_channels );
279         i_next_atomic_operation += 2;
280         i_source_channel_offset++;
281     }
282     if( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
283     {
284         ComputeChannelOperations( p_data , i_rate
285                 , i_next_atomic_operation , i_source_channel_offset
286                 , d_x , 0 , d_min , 1.5 / i_nb_channels );
287         i_next_atomic_operation += 2;
288         i_source_channel_offset++;
289     }
290     if( i_physical_channels & AOUT_CHAN_REARLEFT )
291     {
292         ComputeChannelOperations( p_data , i_rate
293                 , i_next_atomic_operation , i_source_channel_offset
294                 , -d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
295         i_next_atomic_operation += 2;
296         i_source_channel_offset++;
297     }
298     if( i_physical_channels & AOUT_CHAN_REARRIGHT )
299     {
300         ComputeChannelOperations( p_data , i_rate
301                 , i_next_atomic_operation , i_source_channel_offset
302                 , d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
303         i_next_atomic_operation += 2;
304         i_source_channel_offset++;
305     }
306     if( i_physical_channels & AOUT_CHAN_REARCENTER )
307     {
308         ComputeChannelOperations( p_data , i_rate
309                 , i_next_atomic_operation , i_source_channel_offset
310                 , 0 , -d_z , d_min , 1.5 / i_nb_channels );
311         i_next_atomic_operation += 2;
312         i_source_channel_offset++;
313     }
314     if( i_physical_channels & AOUT_CHAN_CENTER )
315     {
316         /* having two center channels increases the spatialization effect */
317         ComputeChannelOperations( p_data , i_rate
318                 , i_next_atomic_operation , i_source_channel_offset
319                 , d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
320         i_next_atomic_operation += 2;
321         ComputeChannelOperations( p_data , i_rate
322                 , i_next_atomic_operation , i_source_channel_offset
323                 , -d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
324         i_next_atomic_operation += 2;
325         i_source_channel_offset++;
326     }
327     if( i_physical_channels & AOUT_CHAN_LFE )
328     {
329         ComputeChannelOperations( p_data , i_rate
330                 , i_next_atomic_operation , i_source_channel_offset
331                 , 0 , d_z_rear , d_min , 5.0 / i_nb_channels );
332         i_next_atomic_operation += 2;
333         i_source_channel_offset++;
334     }
335
336     /* Initialize the overflow buffer
337      * we need it because the process induce a delay in the samples */
338     p_data->i_overflow_buffer_size = 0;
339     for( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
340     {
341         if( p_data->i_overflow_buffer_size
342                 < p_data->p_atomic_operations[i].i_delay * 2 * sizeof (int16_t) )
343         {
344             p_data->i_overflow_buffer_size
345                 = p_data->p_atomic_operations[i].i_delay * 2 * sizeof (int16_t);
346         }
347     }
348     p_data->p_overflow_buffer = malloc( p_data->i_overflow_buffer_size );
349     if( p_data->p_atomic_operations == NULL )
350     {
351         msg_Err( p_this, "out of memory" );
352         return -1;
353     }
354     memset( p_data->p_overflow_buffer, 0, p_data->i_overflow_buffer_size );
355
356     /* end */
357     return 0;
358 }
359
360 /*****************************************************************************
361  * OpenFilter
362  *****************************************************************************/
363 static int OpenFilter( vlc_object_t *p_this )
364 {
365     filter_t * p_filter = (filter_t *)p_this;
366     filter_sys_t *p_sys = NULL;
367
368     if( aout_FormatNbChannels( &(p_filter->fmt_in.audio) ) == 1 )
369     {
370         msg_Dbg( p_filter, "filter discarded (incompatible format)" );
371         return VLC_EGENERIC;
372     }
373
374     if( (p_filter->fmt_in.i_codec != AOUT_FMT_S16_NE) ||
375         (p_filter->fmt_out.i_codec != AOUT_FMT_S16_NE) )
376     {
377         msg_Err( p_this, "filter discarded (invalid format)" );
378         return -1;
379     }
380
381     if( (p_filter->fmt_in.audio.i_format != p_filter->fmt_out.audio.i_format) &&
382         (p_filter->fmt_in.audio.i_rate != p_filter->fmt_out.audio.i_rate) &&
383         (p_filter->fmt_in.audio.i_format != AOUT_FMT_S16_NE) &&
384         (p_filter->fmt_out.audio.i_format != AOUT_FMT_S16_NE) &&
385         (p_filter->fmt_in.audio.i_bitspersample !=
386                                     p_filter->fmt_out.audio.i_bitspersample))
387     {
388         msg_Err( p_this, "couldn't load mono filter" );
389         return VLC_EGENERIC;
390     }
391
392     /* Allocate the memory needed to store the module's structure */
393     p_sys = p_filter->p_sys = malloc( sizeof(filter_sys_t) );
394     if( p_sys == NULL )
395     {
396         msg_Err( p_filter, "out of memory" );
397         return VLC_EGENERIC;
398     }
399
400     var_Create( p_this, MONO_CFG "downmix",
401                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
402     p_sys->b_downmix = var_GetBool( p_this, MONO_CFG "downmix" );
403
404     var_Create( p_this, MONO_CFG "channel",
405                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
406     p_sys->i_channel_selected =
407             (unsigned int) var_GetInteger( p_this, MONO_CFG "channel" );
408
409     if( p_sys->b_downmix )
410     {
411         msg_Dbg( p_this, "using stereo to mono downmix" );
412         p_filter->fmt_out.audio.i_physical_channels = AOUT_CHAN_CENTER;
413         p_filter->fmt_out.audio.i_channels = 1;
414     }
415     else
416     {
417         msg_Dbg( p_this, "using pseudo mono" );
418         p_filter->fmt_out.audio.i_physical_channels =
419                             (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT);
420         p_filter->fmt_out.audio.i_channels = 2;
421     }
422
423     p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
424     p_filter->fmt_out.audio.i_format = p_filter->fmt_out.i_codec;
425
426     p_sys->i_nb_channels = aout_FormatNbChannels( &(p_filter->fmt_in.audio) );
427     p_sys->i_bitspersample = p_filter->fmt_out.audio.i_bitspersample;
428
429     p_sys->i_overflow_buffer_size = 0;
430     p_sys->p_overflow_buffer = NULL;
431     p_sys->i_nb_atomic_operations = 0;
432     p_sys->p_atomic_operations = NULL;
433
434     if( Init( VLC_OBJECT(p_filter), p_filter->p_sys,
435               aout_FormatNbChannels( &p_filter->fmt_in.audio ),
436               p_filter->fmt_in.audio.i_physical_channels,
437               p_filter->fmt_in.audio.i_rate ) < 0 )
438     {
439         return VLC_EGENERIC;
440     }
441
442     p_filter->pf_audio_filter = Convert;
443
444     msg_Dbg( p_this, "%4.4s->%4.4s, channels %d->%d, bits per sample: %i->%i",
445              (char *)&p_filter->fmt_in.i_codec,
446              (char *)&p_filter->fmt_out.i_codec,
447              p_filter->fmt_in.audio.i_physical_channels,
448              p_filter->fmt_out.audio.i_physical_channels,
449              p_filter->fmt_in.audio.i_bitspersample,
450              p_filter->fmt_out.audio.i_bitspersample );
451
452     return VLC_SUCCESS;
453 }
454
455 /*****************************************************************************
456  * CloseFilter
457  *****************************************************************************/
458 static void CloseFilter( vlc_object_t *p_this)
459 {
460     filter_t *p_filter = (filter_t *) p_this;
461     filter_sys_t *p_sys = p_filter->p_sys;
462
463     var_Destroy( p_this, MONO_CFG "channel" );
464     var_Destroy( p_this, MONO_CFG "downmix" );
465     free( p_sys );
466 }
467
468 /*****************************************************************************
469  * Convert
470  *****************************************************************************/
471 static block_t *Convert( filter_t *p_filter, block_t *p_block )
472 {
473     aout_filter_t aout_filter;
474     aout_buffer_t in_buf, out_buf;
475     block_t *p_out = NULL;
476     unsigned int i_samples;
477     int i_out_size;
478
479     if( !p_block || !p_block->i_samples )
480     {
481         if( p_block )
482             p_block->pf_release( p_block );
483         return NULL;
484     }
485
486     i_out_size = p_block->i_samples * p_filter->p_sys->i_bitspersample/8 *
487                  aout_FormatNbChannels( &(p_filter->fmt_out.audio) );
488
489     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
490     if( !p_out )
491     {
492         msg_Warn( p_filter, "can't get output buffer" );
493         p_block->pf_release( p_block );
494         return NULL;
495     }
496     p_out->i_samples = (p_block->i_samples / p_filter->p_sys->i_nb_channels) *
497                        aout_FormatNbChannels( &(p_filter->fmt_out.audio) );
498     p_out->i_dts = p_block->i_dts;
499     p_out->i_pts = p_block->i_pts;
500     p_out->i_length = p_block->i_length;
501
502     aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
503     aout_filter.input = p_filter->fmt_in.audio;
504     aout_filter.input.i_format = p_filter->fmt_in.i_codec;
505     aout_filter.output = p_filter->fmt_out.audio;
506     aout_filter.output.i_format = p_filter->fmt_out.i_codec;
507
508     in_buf.p_buffer = p_block->p_buffer;
509     in_buf.i_nb_bytes = p_block->i_buffer;
510     in_buf.i_nb_samples = p_block->i_samples;
511
512 #if 0
513     unsigned int i_in_size = in_buf.i_nb_samples  * (p_filter->p_sys->i_bitspersample/8) *
514                              aout_FormatNbChannels( &(p_filter->fmt_in.audio) );
515     if( (in_buf.i_nb_bytes != i_in_size) && ((i_in_size % 32) != 0) ) /* is it word aligned?? */
516     {
517         msg_Err( p_filter, "input buffer is not word aligned" );
518         /* Fix output buffer to be word aligned */
519     }
520 #endif
521
522     out_buf.p_buffer = p_out->p_buffer;
523     out_buf.i_nb_bytes = p_out->i_buffer;
524     out_buf.i_nb_samples = p_out->i_samples;
525
526     memset( p_out->p_buffer, 0, i_out_size );
527     if( p_filter->p_sys->b_downmix )
528     {
529         stereo2mono_downmix( (aout_instance_t *)p_filter, &aout_filter,
530                              &in_buf, &out_buf );
531         i_samples = mono( (aout_instance_t *)p_filter, &aout_filter,
532                            &out_buf, &in_buf );
533     }
534     else
535     {
536         i_samples = stereo_to_mono( (aout_instance_t *)p_filter, &aout_filter,
537                                     &out_buf, &in_buf );
538     }
539
540     p_out->i_buffer = out_buf.i_nb_bytes;
541     p_out->i_samples = out_buf.i_nb_samples;
542
543     p_block->pf_release( p_block );
544     return p_out;
545 }
546
547 /* stereo2mono_downmix - stereo channels into one mono channel.
548  * Code taken from modules/audio_filter/channel_mixer/headphone.c
549  * converted from float into int16_t based downmix
550  * Written by Boris Dorès <babal@via.ecp.fr>
551  */
552 static void stereo2mono_downmix( aout_instance_t * p_aout, aout_filter_t * p_filter,
553                             aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
554 {
555     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
556
557     int i_input_nb = aout_FormatNbChannels( &p_filter->input );
558     int i_output_nb = aout_FormatNbChannels( &p_filter->output );
559
560     int16_t * p_in = (int16_t*) p_in_buf->p_buffer;
561     byte_t * p_out;
562     byte_t * p_overflow;
563     byte_t * p_slide;
564
565     size_t i_overflow_size;     /* in bytes */
566     size_t i_out_size;          /* in bytes */
567
568     unsigned int i, j;
569
570     int i_source_channel_offset;
571     int i_dest_channel_offset;
572     unsigned int i_delay;
573     double d_amplitude_factor;
574
575     /* out buffer characterisitcs */
576     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
577     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
578     p_out = p_out_buf->p_buffer;
579     i_out_size = p_out_buf->i_nb_bytes;
580
581     if( p_sys != NULL )
582     {
583         /* Slide the overflow buffer */
584         p_overflow = p_sys->p_overflow_buffer;
585         i_overflow_size = p_sys->i_overflow_buffer_size;
586
587         if ( i_out_size > i_overflow_size )
588             memcpy( p_out, p_overflow, i_overflow_size );
589         else
590             memcpy( p_out, p_overflow, i_out_size );
591
592         p_slide = p_sys->p_overflow_buffer;
593         while( p_slide < p_overflow + i_overflow_size )
594         {
595             if( p_slide + i_out_size < p_overflow + i_overflow_size )
596             {
597                 memset( p_slide, 0, i_out_size );
598                 if( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
599                     memcpy( p_slide, p_slide + i_out_size, i_out_size );
600                 else
601                     memcpy( p_slide, p_slide + i_out_size,
602                             p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
603             }
604             else
605             {
606                 memset( p_slide, 0, p_overflow + i_overflow_size - p_slide );
607             }
608             p_slide += i_out_size;
609         }
610
611         /* apply the atomic operations */
612         for( i = 0; i < p_sys->i_nb_atomic_operations; i++ )
613         {
614             /* shorter variable names */
615             i_source_channel_offset
616                 = p_sys->p_atomic_operations[i].i_source_channel_offset;
617             i_dest_channel_offset
618                 = p_sys->p_atomic_operations[i].i_dest_channel_offset;
619             i_delay = p_sys->p_atomic_operations[i].i_delay;
620             d_amplitude_factor
621                 = p_sys->p_atomic_operations[i].d_amplitude_factor;
622
623             if( p_out_buf->i_nb_samples > i_delay )
624             {
625                 /* current buffer coefficients */
626                 for( j = 0; j < p_out_buf->i_nb_samples - i_delay; j++ )
627                 {
628                     ((int16_t*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
629                         += p_in[ j * i_input_nb + i_source_channel_offset ]
630                            * d_amplitude_factor;
631                 }
632
633                 /* overflow buffer coefficients */
634                 for( j = 0; j < i_delay; j++ )
635                 {
636                     ((int16_t*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
637                         += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
638                            * i_input_nb + i_source_channel_offset ]
639                            * d_amplitude_factor;
640                 }
641             }
642             else
643             {
644                 /* overflow buffer coefficients only */
645                 for( j = 0; j < p_out_buf->i_nb_samples; j++ )
646                 {
647                     ((int16_t*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
648                         * i_output_nb + i_dest_channel_offset ]
649                         += p_in[ j * i_input_nb + i_source_channel_offset ]
650                            * d_amplitude_factor;
651                 }
652             }
653         }
654     }
655     else
656     {
657         memset( p_out, 0, i_out_size );
658     }
659 }
660
661 /* Simple stereo to mono mixing. */
662 static unsigned int mono( aout_instance_t * p_aout, aout_filter_t *p_filter,
663                           aout_buffer_t *p_output, aout_buffer_t *p_input )
664 {
665     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
666     int16_t *p_in, *p_out;
667     unsigned int n = 0, r = 0;
668
669     p_in = (int16_t *) p_input->p_buffer;
670     p_out = (int16_t *) p_output->p_buffer;
671
672     while( n < (p_input->i_nb_samples * p_sys->i_nb_channels) )
673     {
674         p_out[r] = (p_in[n] + p_in[n+1]) >> 1;
675         r++;
676         n += 2;
677     }
678     return r;
679 }
680
681 /* Simple stereo to mono mixing. */
682 static unsigned int stereo_to_mono( aout_instance_t * p_aout, aout_filter_t *p_filter,
683                                     aout_buffer_t *p_output, aout_buffer_t *p_input )
684 {
685     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
686     int16_t *p_in, *p_out;
687     unsigned int n;
688
689     p_in = (int16_t *) p_input->p_buffer;
690     p_out = (int16_t *) p_output->p_buffer;
691
692     for( n = 0; n < (p_input->i_nb_samples * p_sys->i_nb_channels); n++ )
693     {
694         /* Fake real mono. */
695         if( p_sys->i_channel_selected == -1)
696         {
697             p_out[n] = p_out[n+1] = (p_in[n] + p_in[n+1]) >> 1;
698             n++;
699         }
700         else if( (n % p_sys->i_nb_channels) == (unsigned int) p_sys->i_channel_selected )
701         {
702             p_out[n] = p_out[n+1] = p_in[n];
703         }
704     }
705     return n;
706 }