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