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