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