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