Moves volume icon update in the timer
[vlc.git] / modules / audio_filter / channel_mixer / headphone.c
1 /*****************************************************************************
2  * headphone.c : headphone virtual spatialization channel mixer module
3  *               -> gives the feeling of a real room with a simple headphone
4  *****************************************************************************
5  * Copyright (C) 2002-2006 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Boris Dorès <babal@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <math.h>                                        /* sqrt */
29
30 #include <vlc/vlc.h>
31 #include <vlc_aout.h>
32 #include <vlc_filter.h>
33 #include <vlc_block.h>
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Create    ( vlc_object_t * );
39 static void Destroy   ( vlc_object_t * );
40
41 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
42                         aout_buffer_t * );
43
44 /* Audio filter2 */
45 static int  OpenFilter ( vlc_object_t * );
46 static void CloseFilter( vlc_object_t * );
47 static block_t *Convert( filter_t *, block_t * );
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 #define MODULE_DESCRIPTION N_ ( \
53      "This effect gives you the feeling that you are standing in a room " \
54      "with a complete 7.1 speaker set when using only a headphone, " \
55      "providing a more realistic sound experience. It should also be " \
56      "more comfortable and less tiring when listening to music for " \
57      "long periods of time.\nIt works with any source format from mono " \
58      "to 7.1.")
59
60 #define HEADPHONE_DIM_TEXT N_("Characteristic dimension")
61 #define HEADPHONE_DIM_LONGTEXT N_( \
62      "Distance between front left speaker and listener in meters.")
63
64 #define HEADPHONE_COMPENSATE_TEXT N_("Compensate delay")
65 #define HEADPHONE_COMPENSATE_LONGTEXT N_( \
66      "The delay which is introduced by the physical algorithm may "\
67      "sometimes be disturbing for the synchronization between lips-movement "\
68      "and speech. In case, turn this on to compensate.")
69
70 #define HEADPHONE_DOLBY_TEXT N_("No decoding of Dolby Surround")
71 #define HEADPHONE_DOLBY_LONGTEXT N_( \
72      "Dolby Surround encoded streams won't be decoded before being " \
73      "processed by this filter. Enabling this setting is not recommended.")
74
75 vlc_module_begin();
76     set_description( _("Headphone virtual spatialization effect") );
77     set_shortname( _("Headphone effect") );
78     set_help( MODULE_DESCRIPTION );
79     set_category( CAT_AUDIO );
80     set_subcategory( SUBCAT_AUDIO_AFILTER );
81
82     add_integer( "headphone-dim", 10, NULL, HEADPHONE_DIM_TEXT,
83                  HEADPHONE_DIM_LONGTEXT, VLC_FALSE );
84     add_bool( "headphone-compensate", 0, NULL, HEADPHONE_COMPENSATE_TEXT,
85               HEADPHONE_COMPENSATE_LONGTEXT, VLC_TRUE );
86     add_bool( "headphone-dolby", 0, NULL, HEADPHONE_DOLBY_TEXT,
87               HEADPHONE_DOLBY_LONGTEXT, VLC_TRUE );
88
89     set_capability( "audio filter", 0 );
90     set_callbacks( Create, Destroy );
91     add_shortcut( "headphone" );
92
93     /* Audio filter 2 */
94     add_submodule();
95     set_description( _("Headphone virtual spatialization effect") );
96     set_capability( "audio filter2", 0 );
97     set_callbacks( OpenFilter, CloseFilter );
98 vlc_module_end();
99
100
101 /*****************************************************************************
102  * Internal data structures
103  *****************************************************************************/
104 struct atomic_operation_t
105 {
106     int i_source_channel_offset;
107     int i_dest_channel_offset;
108     unsigned int i_delay;/* in sample unit */
109     double d_amplitude_factor;
110 };
111
112 struct aout_filter_sys_t
113 {
114     size_t i_overflow_buffer_size;/* in bytes */
115     byte_t * p_overflow_buffer;
116     unsigned int i_nb_atomic_operations;
117     struct atomic_operation_t * p_atomic_operations;
118 };
119
120 struct filter_sys_t
121 {
122     size_t i_overflow_buffer_size;/* in bytes */
123     byte_t * p_overflow_buffer;
124     unsigned int i_nb_atomic_operations;
125     struct atomic_operation_t * p_atomic_operations;
126 };
127
128 /*****************************************************************************
129  * Init: initialize internal data structures
130  * and computes the needed atomic operations
131  *****************************************************************************/
132 /* x and z represent the coordinates of the virtual speaker
133  *  relatively to the center of the listener's head, measured in meters :
134  *
135  *  left              right
136  *Z
137  *-
138  *a          head
139  *x
140  *i
141  *s
142  *  rear left    rear right
143  *
144  *          x-axis
145  *  */
146 static void ComputeChannelOperations( struct aout_filter_sys_t * p_data
147         , unsigned int i_rate, unsigned int i_next_atomic_operation
148         , int i_source_channel_offset, double d_x, double d_z
149         , double d_compensation_length, double d_channel_amplitude_factor )
150 {
151     double d_c = 340; /*sound celerity (unit: m/s)*/
152     double d_compensation_delay = (d_compensation_length-0.1) / d_c * i_rate;
153
154     /* Left ear */
155     p_data->p_atomic_operations[i_next_atomic_operation]
156         .i_source_channel_offset = i_source_channel_offset;
157     p_data->p_atomic_operations[i_next_atomic_operation]
158         .i_dest_channel_offset = 0;/* left */
159     p_data->p_atomic_operations[i_next_atomic_operation]
160         .i_delay = (int)( sqrt( (-0.1-d_x)*(-0.1-d_x) + (0-d_z)*(0-d_z) )
161                           / d_c * i_rate - d_compensation_delay );
162     if( d_x < 0 )
163     {
164         p_data->p_atomic_operations[i_next_atomic_operation]
165             .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
166     }
167     else if( d_x > 0 )
168     {
169         p_data->p_atomic_operations[i_next_atomic_operation]
170             .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
171     }
172     else
173     {
174         p_data->p_atomic_operations[i_next_atomic_operation]
175             .d_amplitude_factor = d_channel_amplitude_factor / 2;
176     }
177
178     /* Right ear */
179     p_data->p_atomic_operations[i_next_atomic_operation + 1]
180         .i_source_channel_offset = i_source_channel_offset;
181     p_data->p_atomic_operations[i_next_atomic_operation + 1]
182         .i_dest_channel_offset = 1;/* right */
183     p_data->p_atomic_operations[i_next_atomic_operation + 1]
184         .i_delay = (int)( sqrt( (0.1-d_x)*(0.1-d_x) + (0-d_z)*(0-d_z) )
185                           / d_c * i_rate - d_compensation_delay );
186     if( d_x < 0 )
187     {
188         p_data->p_atomic_operations[i_next_atomic_operation + 1]
189             .d_amplitude_factor = d_channel_amplitude_factor * 0.9 / 2;
190     }
191     else if( d_x > 0 )
192     {
193         p_data->p_atomic_operations[i_next_atomic_operation + 1]
194             .d_amplitude_factor = d_channel_amplitude_factor * 1.1 / 2;
195     }
196     else
197     {
198         p_data->p_atomic_operations[i_next_atomic_operation + 1]
199             .d_amplitude_factor = d_channel_amplitude_factor / 2;
200     }
201 }
202
203 static int Init( vlc_object_t *p_this, struct aout_filter_sys_t * p_data
204         , unsigned int i_nb_channels, uint32_t i_physical_channels
205         , unsigned int i_rate )
206 {
207     double d_x = config_GetInt( p_this, "headphone-dim" );
208     double d_z = d_x;
209     double d_z_rear = -d_x/3;
210     double d_min = 0;
211     unsigned int i_next_atomic_operation;
212     int i_source_channel_offset;
213     unsigned int i;
214
215     if( p_data == NULL )
216     {
217         msg_Dbg( p_this, "passing a null pointer as argument" );
218         return 0;
219     }
220
221     if( config_GetInt( p_this, "headphone-compensate" ) )
222     {
223         /* minimal distance to any speaker */
224         if( i_physical_channels & AOUT_CHAN_REARCENTER )
225         {
226             d_min = d_z_rear;
227         }
228         else
229         {
230             d_min = d_z;
231         }
232     }
233
234     /* Number of elementary operations */
235     p_data->i_nb_atomic_operations = i_nb_channels * 2;
236     if( i_physical_channels & AOUT_CHAN_CENTER )
237     {
238         p_data->i_nb_atomic_operations += 2;
239     }
240     p_data->p_atomic_operations = malloc( sizeof(struct atomic_operation_t)
241             * p_data->i_nb_atomic_operations );
242     if( p_data->p_atomic_operations == NULL )
243     {
244         msg_Err( p_this, "out of memory" );
245         return -1;
246     }
247
248     /* For each virtual speaker, computes elementary wave propagation time
249      * to each ear */
250     i_next_atomic_operation = 0;
251     i_source_channel_offset = 0;
252     if( i_physical_channels & AOUT_CHAN_LEFT )
253     {
254         ComputeChannelOperations( p_data , i_rate
255                 , i_next_atomic_operation , i_source_channel_offset
256                 , -d_x , d_z , d_min , 2.0 / i_nb_channels );
257         i_next_atomic_operation += 2;
258         i_source_channel_offset++;
259     }
260     if( i_physical_channels & AOUT_CHAN_RIGHT )
261     {
262         ComputeChannelOperations( p_data , i_rate
263                 , i_next_atomic_operation , i_source_channel_offset
264                 , d_x , d_z , d_min , 2.0 / i_nb_channels );
265         i_next_atomic_operation += 2;
266         i_source_channel_offset++;
267     }
268     if( i_physical_channels & AOUT_CHAN_MIDDLELEFT )
269     {
270         ComputeChannelOperations( p_data , i_rate
271                 , i_next_atomic_operation , i_source_channel_offset
272                 , -d_x , 0 , d_min , 1.5 / i_nb_channels );
273         i_next_atomic_operation += 2;
274         i_source_channel_offset++;
275     }
276     if( i_physical_channels & AOUT_CHAN_MIDDLERIGHT )
277     {
278         ComputeChannelOperations( p_data , i_rate
279                 , i_next_atomic_operation , i_source_channel_offset
280                 , d_x , 0 , d_min , 1.5 / i_nb_channels );
281         i_next_atomic_operation += 2;
282         i_source_channel_offset++;
283     }
284     if( i_physical_channels & AOUT_CHAN_REARLEFT )
285     {
286         ComputeChannelOperations( p_data , i_rate
287                 , i_next_atomic_operation , i_source_channel_offset
288                 , -d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
289         i_next_atomic_operation += 2;
290         i_source_channel_offset++;
291     }
292     if( i_physical_channels & AOUT_CHAN_REARRIGHT )
293     {
294         ComputeChannelOperations( p_data , i_rate
295                 , i_next_atomic_operation , i_source_channel_offset
296                 , d_x , d_z_rear , d_min , 1.5 / i_nb_channels );
297         i_next_atomic_operation += 2;
298         i_source_channel_offset++;
299     }
300     if( i_physical_channels & AOUT_CHAN_REARCENTER )
301     {
302         ComputeChannelOperations( p_data , i_rate
303                 , i_next_atomic_operation , i_source_channel_offset
304                 , 0 , -d_z , d_min , 1.5 / i_nb_channels );
305         i_next_atomic_operation += 2;
306         i_source_channel_offset++;
307     }
308     if( i_physical_channels & AOUT_CHAN_CENTER )
309     {
310         /* having two center channels increases the spatialization effect */
311         ComputeChannelOperations( p_data , i_rate
312                 , i_next_atomic_operation , i_source_channel_offset
313                 , d_x / 5.0 , d_z , d_min , 0.75 / i_nb_channels );
314         i_next_atomic_operation += 2;
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         i_source_channel_offset++;
320     }
321     if( i_physical_channels & AOUT_CHAN_LFE )
322     {
323         ComputeChannelOperations( p_data , i_rate
324                 , i_next_atomic_operation , i_source_channel_offset
325                 , 0 , d_z_rear , d_min , 5.0 / i_nb_channels );
326         i_next_atomic_operation += 2;
327         i_source_channel_offset++;
328     }
329
330     /* Initialize the overflow buffer
331      * we need it because the process induce a delay in the samples */
332     p_data->i_overflow_buffer_size = 0;
333     for( i = 0 ; i < p_data->i_nb_atomic_operations ; i++ )
334     {
335         if( p_data->i_overflow_buffer_size
336                 < p_data->p_atomic_operations[i].i_delay * 2 * sizeof (float) )
337         {
338             p_data->i_overflow_buffer_size
339                 = p_data->p_atomic_operations[i].i_delay * 2 * sizeof (float);
340         }
341     }
342     p_data->p_overflow_buffer = malloc( p_data->i_overflow_buffer_size );
343     if( p_data->p_atomic_operations == NULL )
344     {
345         msg_Err( p_this, "out of memory" );
346         return -1;
347     }
348     memset( p_data->p_overflow_buffer, 0 , p_data->i_overflow_buffer_size );
349
350     /* end */
351     return 0;
352 }
353
354 /*****************************************************************************
355  * Create: allocate headphone downmixer
356  *****************************************************************************/
357 static int Create( vlc_object_t *p_this )
358 {
359     aout_filter_t * p_filter = (aout_filter_t *)p_this;
360     vlc_bool_t b_fit = VLC_TRUE;
361
362     /* Activate this filter only with stereo devices */
363     if( p_filter->output.i_physical_channels
364             != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
365     {
366         msg_Dbg( p_filter, "filter discarded (incompatible format)" );
367         return VLC_EGENERIC;
368     }
369
370     /* Request a specific format if not already compatible */
371     if( p_filter->input.i_original_channels
372             != p_filter->output.i_original_channels )
373     {
374         b_fit = VLC_FALSE;
375         p_filter->input.i_original_channels =
376                                         p_filter->output.i_original_channels;
377     }
378     if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
379           || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
380     {
381         b_fit = VLC_FALSE;
382         p_filter->input.i_format = VLC_FOURCC('f','l','3','2');
383         p_filter->output.i_format = VLC_FOURCC('f','l','3','2');
384     }
385     if( p_filter->input.i_rate != p_filter->output.i_rate )
386     {
387         b_fit = VLC_FALSE;
388         p_filter->input.i_rate = p_filter->output.i_rate;
389     }
390     if( p_filter->input.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
391           && ( p_filter->input.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
392           && ! config_GetInt ( p_filter , "headphone-dolby" ) )
393     {
394         b_fit = VLC_FALSE;
395         p_filter->input.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
396                                               AOUT_CHAN_CENTER |
397                                               AOUT_CHAN_REARLEFT |
398                                               AOUT_CHAN_REARRIGHT;
399     }
400     if( ! b_fit )
401     {
402         msg_Dbg( p_filter, "requesting specific format" );
403         return VLC_EGENERIC;
404     }
405
406     /* Allocate the memory needed to store the module's structure */
407     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
408     if( p_filter->p_sys == NULL )
409     {
410         msg_Err( p_filter, "out of memory" );
411         return VLC_EGENERIC;
412     }
413     p_filter->p_sys->i_overflow_buffer_size = 0;
414     p_filter->p_sys->p_overflow_buffer = NULL;
415     p_filter->p_sys->i_nb_atomic_operations = 0;
416     p_filter->p_sys->p_atomic_operations = NULL;
417
418     if( Init( VLC_OBJECT(p_filter), p_filter->p_sys
419                 , aout_FormatNbChannels ( &p_filter->input )
420                 , p_filter->input.i_physical_channels
421                 , p_filter->input.i_rate ) < 0 )
422     {
423         return VLC_EGENERIC;
424     }
425
426     p_filter->pf_do_work = DoWork;
427     p_filter->b_in_place = 0;
428
429     return VLC_SUCCESS;
430 }
431
432 /*****************************************************************************
433  * Destroy: deallocate resources associated with headphone downmixer
434  *****************************************************************************/
435 static void Destroy( vlc_object_t *p_this )
436 {
437     aout_filter_t * p_filter = (aout_filter_t *)p_this;
438
439     if( p_filter->p_sys != NULL )
440     {
441         if( p_filter->p_sys->p_overflow_buffer != NULL )
442         {
443             free( p_filter->p_sys->p_overflow_buffer );
444         }
445         if( p_filter->p_sys->p_atomic_operations != NULL )
446         {
447             free( p_filter->p_sys->p_atomic_operations );
448         }
449         free( p_filter->p_sys );
450         p_filter->p_sys = NULL;
451     }
452 }
453
454 /*****************************************************************************
455  * DoWork: convert a buffer
456  *****************************************************************************/
457 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
458                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
459 {
460     int i_input_nb = aout_FormatNbChannels( &p_filter->input );
461     int i_output_nb = aout_FormatNbChannels( &p_filter->output );
462
463     float * p_in = (float*) p_in_buf->p_buffer;
464     byte_t * p_out;
465     byte_t * p_overflow;
466     byte_t * p_slide;
467
468     size_t i_overflow_size;     /* in bytes */
469     size_t i_out_size;          /* in bytes */
470
471     unsigned int i, j;
472
473     int i_source_channel_offset;
474     int i_dest_channel_offset;
475     unsigned int i_delay;
476     double d_amplitude_factor;
477
478     /* out buffer characterisitcs */
479     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
480     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
481     p_out = p_out_buf->p_buffer;
482     i_out_size = p_out_buf->i_nb_bytes;
483
484     if( p_filter->p_sys != NULL )
485     {
486         /* Slide the overflow buffer */
487         p_overflow = p_filter->p_sys->p_overflow_buffer;
488         i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
489
490         memset( p_out, 0, i_out_size );
491         if ( i_out_size > i_overflow_size )
492             memcpy( p_out, p_overflow, i_overflow_size );
493         else
494             memcpy( p_out, p_overflow, i_out_size );
495
496         p_slide = p_filter->p_sys->p_overflow_buffer;
497         while( p_slide < p_overflow + i_overflow_size )
498         {
499             if( p_slide + i_out_size < p_overflow + i_overflow_size )
500             {
501                 memset( p_slide, 0, i_out_size );
502                 if( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
503                     memcpy( p_slide, p_slide + i_out_size, i_out_size );
504                 else
505                     memcpy( p_slide, p_slide + i_out_size,
506                             p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
507             }
508             else
509             {
510                 memset( p_slide, 0, p_overflow + i_overflow_size - p_slide );
511             }
512             p_slide += i_out_size;
513         }
514
515         /* apply the atomic operations */
516         for( i = 0; i < p_filter->p_sys->i_nb_atomic_operations; i++ )
517         {
518             /* shorter variable names */
519             i_source_channel_offset
520                 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
521             i_dest_channel_offset
522                 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
523             i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
524             d_amplitude_factor
525                 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
526
527             if( p_out_buf->i_nb_samples > i_delay )
528             {
529                 /* current buffer coefficients */
530                 for( j = 0; j < p_out_buf->i_nb_samples - i_delay; j++ )
531                 {
532                     ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
533                         += p_in[ j * i_input_nb + i_source_channel_offset ]
534                            * d_amplitude_factor;
535                 }
536
537                 /* overflow buffer coefficients */
538                 for( j = 0; j < i_delay; j++ )
539                 {
540                     ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
541                         += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
542                            * i_input_nb + i_source_channel_offset ]
543                            * d_amplitude_factor;
544                 }
545             }
546             else
547             {
548                 /* overflow buffer coefficients only */
549                 for( j = 0; j < p_out_buf->i_nb_samples; j++ )
550                 {
551                     ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
552                         * i_output_nb + i_dest_channel_offset ]
553                         += p_in[ j * i_input_nb + i_source_channel_offset ]
554                            * d_amplitude_factor;
555                 }
556             }
557         }
558     }
559     else
560     {
561         memset( p_out, 0, i_out_size );
562     }
563 }
564
565 /*
566  * Audio filter 2
567  */
568 /*****************************************************************************
569  * OpenFilter:
570  *****************************************************************************/
571 static int OpenFilter( vlc_object_t *p_this )
572 {
573     filter_t *p_filter = (filter_t *)p_this;
574     vlc_bool_t b_fit = VLC_TRUE;
575
576     /* Activate this filter only with stereo devices */
577     if( p_filter->fmt_out.audio.i_physical_channels
578             != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
579     {
580         msg_Dbg( p_filter, "filter discarded (incompatible format)" );
581         return VLC_EGENERIC;
582     }
583
584     /* Request a specific format if not already compatible */
585     if( p_filter->fmt_in.audio.i_original_channels
586             != p_filter->fmt_out.audio.i_original_channels )
587     {
588         b_fit = VLC_FALSE;
589         p_filter->fmt_in.audio.i_original_channels =
590                                         p_filter->fmt_out.audio.i_original_channels;
591     }
592     if( p_filter->fmt_in.audio.i_format != VLC_FOURCC('f','l','3','2')
593           || p_filter->fmt_out.audio.i_format != VLC_FOURCC('f','l','3','2') )
594     {
595         b_fit = VLC_FALSE;
596         p_filter->fmt_in.audio.i_format = VLC_FOURCC('f','l','3','2');
597         p_filter->fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
598     }
599     if( p_filter->fmt_in.audio.i_rate != p_filter->fmt_out.audio.i_rate )
600     {
601         b_fit = VLC_FALSE;
602         p_filter->fmt_in.audio.i_rate = p_filter->fmt_out.audio.i_rate;
603     }
604     if( p_filter->fmt_in.audio.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
605           && ( p_filter->fmt_in.audio.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
606           && !config_GetInt( p_filter, "headphone-dolby" ) )
607     {
608         b_fit = VLC_FALSE;
609         p_filter->fmt_in.audio.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
610                                               AOUT_CHAN_CENTER |
611                                               AOUT_CHAN_REARLEFT |
612                                               AOUT_CHAN_REARRIGHT;
613     }
614     if( !b_fit )
615     {
616         msg_Dbg( p_filter, "requesting specific format" );
617         return VLC_EGENERIC;
618     }
619
620     /* Allocate the memory needed to store the module's structure */
621     p_filter->p_sys = malloc( sizeof(struct filter_sys_t) );
622     if( p_filter->p_sys == NULL )
623     {
624         msg_Err( p_filter, "out of memory" );
625         return VLC_EGENERIC;
626     }
627     p_filter->p_sys->i_overflow_buffer_size = 0;
628     p_filter->p_sys->p_overflow_buffer = NULL;
629     p_filter->p_sys->i_nb_atomic_operations = 0;
630     p_filter->p_sys->p_atomic_operations = NULL;
631
632     if( Init( VLC_OBJECT(p_filter), (struct aout_filter_sys_t *)p_filter->p_sys
633                 , aout_FormatNbChannels ( &(p_filter->fmt_in.audio) )
634                 , p_filter->fmt_in.audio.i_physical_channels
635                 , p_filter->fmt_in.audio.i_rate ) < 0 )
636     {
637         return VLC_EGENERIC;
638     }
639
640     p_filter->pf_audio_filter = Convert;
641     p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
642
643     return VLC_SUCCESS;
644 }
645
646 /*****************************************************************************
647  * CloseFilter : deallocate data structures
648  *****************************************************************************/
649 static void CloseFilter( vlc_object_t *p_this )
650 {
651     filter_t *p_filter = (filter_t *)p_this;
652
653     if( p_filter->p_sys != NULL )
654     {
655         if( p_filter->p_sys->p_overflow_buffer != NULL )
656         {
657             free ( p_filter->p_sys->p_overflow_buffer );
658         }
659         if( p_filter->p_sys->p_atomic_operations != NULL )
660         {
661             free ( p_filter->p_sys->p_atomic_operations );
662         }
663         free( p_filter->p_sys );
664         p_filter->p_sys = NULL;
665     }
666 }
667
668 static block_t *Convert( filter_t *p_filter, block_t *p_block )
669 {
670     aout_filter_t aout_filter;
671     aout_buffer_t in_buf, out_buf;
672     block_t *p_out;
673     int i_out_size;
674
675     if( !p_block || !p_block->i_samples )
676     {
677         if( p_block ) p_block->pf_release( p_block );
678         return NULL;
679     }
680
681     i_out_size = p_block->i_samples *
682       p_filter->fmt_out.audio.i_bitspersample/8 *
683         aout_FormatNbChannels( &(p_filter->fmt_out.audio) );
684
685     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
686     if( !p_out )
687     {
688         msg_Warn( p_filter, "can't get output buffer" );
689         if( p_block ) p_block->pf_release( p_block );
690         return NULL;
691     }
692
693     p_out->i_samples = p_block->i_samples;
694     p_out->i_dts = p_block->i_dts;
695     p_out->i_pts = p_block->i_pts;
696     p_out->i_length = p_block->i_length;
697
698     aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
699     aout_filter.input = p_filter->fmt_in.audio;
700     aout_filter.input.i_format = p_filter->fmt_in.i_codec;
701     aout_filter.output = p_filter->fmt_out.audio;
702     aout_filter.output.i_format = p_filter->fmt_out.i_codec;
703     aout_filter.b_in_place = 0;
704
705     in_buf.p_buffer = p_block->p_buffer;
706     in_buf.i_nb_bytes = p_block->i_buffer;
707     in_buf.i_nb_samples = p_block->i_samples;
708     out_buf.p_buffer = p_out->p_buffer;
709     out_buf.i_nb_bytes = p_out->i_buffer;
710     out_buf.i_nb_samples = p_out->i_samples;
711
712     DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
713
714     p_out->i_buffer = out_buf.i_nb_bytes;
715     p_out->i_samples = out_buf.i_nb_samples;
716
717     if( p_block ) p_block->pf_release( p_block );
718     return p_out;
719 }