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