]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/headphone.c
Don't include config.h from the headers - refs #297.
[vlc] / 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 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc/vlc.h>
35 #include <vlc_aout.h>
36 #include <vlc_filter.h>
37 #include <vlc_block.h>
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  Create    ( vlc_object_t * );
43 static void Destroy   ( vlc_object_t * );
44
45 static void DoWork    ( aout_instance_t *, aout_filter_t *, aout_buffer_t *,
46                         aout_buffer_t * );
47
48 /* Audio filter2 */
49 static int  OpenFilter ( vlc_object_t * );
50 static void CloseFilter( vlc_object_t * );
51 static block_t *Convert( filter_t *, block_t * );
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 #define MODULE_DESCRIPTION N_ ( \
57      "This effect gives you the feeling that you are standing in a room " \
58      "with a complete 7.1 speaker set when using only a headphone, " \
59      "providing a more realistic sound experience. It should also be " \
60      "more comfortable and less tiring when listening to music for " \
61      "long periods of time.\nIt works with any source format from mono " \
62      "to 7.1.")
63
64 #define HEADPHONE_DIM_TEXT N_("Characteristic dimension")
65 #define HEADPHONE_DIM_LONGTEXT N_( \
66      "Distance between front left speaker and listener in meters.")
67
68 #define HEADPHONE_COMPENSATE_TEXT N_("Compensate delay")
69 #define HEADPHONE_COMPENSATE_LONGTEXT N_( \
70      "The delay which is introduced by the physical algorithm may "\
71      "sometimes be disturbing for the synchronization between lips-movement "\
72      "and speech. In case, turn this on to compensate.")
73
74 #define HEADPHONE_DOLBY_TEXT N_("No decoding of Dolby Surround")
75 #define HEADPHONE_DOLBY_LONGTEXT N_( \
76      "Dolby Surround encoded streams won't be decoded before being " \
77      "processed by this filter. Enabling this setting is not recommended.")
78
79 vlc_module_begin();
80     set_description( _("Headphone virtual spatialization effect") );
81     set_shortname( _("Headphone effect") );
82     set_help( MODULE_DESCRIPTION );
83     set_category( CAT_AUDIO );
84     set_subcategory( SUBCAT_AUDIO_AFILTER );
85
86     add_integer( "headphone-dim", 10, NULL, HEADPHONE_DIM_TEXT,
87                  HEADPHONE_DIM_LONGTEXT, VLC_FALSE );
88     add_bool( "headphone-compensate", 0, NULL, HEADPHONE_COMPENSATE_TEXT,
89               HEADPHONE_COMPENSATE_LONGTEXT, VLC_TRUE );
90     add_bool( "headphone-dolby", 0, NULL, HEADPHONE_DOLBY_TEXT,
91               HEADPHONE_DOLBY_LONGTEXT, VLC_TRUE );
92
93     set_capability( "audio filter", 0 );
94     set_callbacks( Create, Destroy );
95     add_shortcut( "headphone" );
96
97     /* Audio filter 2 */
98     add_submodule();
99     set_description( _("Headphone virtual spatialization effect") );
100     set_capability( "audio filter2", 0 );
101     set_callbacks( OpenFilter, CloseFilter );
102 vlc_module_end();
103
104
105 /*****************************************************************************
106  * Internal data structures
107  *****************************************************************************/
108 struct atomic_operation_t
109 {
110     int i_source_channel_offset;
111     int i_dest_channel_offset;
112     unsigned int i_delay;/* in sample unit */
113     double d_amplitude_factor;
114 };
115
116 struct aout_filter_sys_t
117 {
118     size_t i_overflow_buffer_size;/* in bytes */
119     byte_t * p_overflow_buffer;
120     unsigned int i_nb_atomic_operations;
121     struct atomic_operation_t * p_atomic_operations;
122 };
123
124 struct filter_sys_t
125 {
126     size_t i_overflow_buffer_size;/* in bytes */
127     byte_t * p_overflow_buffer;
128     unsigned int i_nb_atomic_operations;
129     struct atomic_operation_t * p_atomic_operations;
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 aout_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 aout_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 (float) )
341         {
342             p_data->i_overflow_buffer_size
343                 = p_data->p_atomic_operations[i].i_delay * 2 * sizeof (float);
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  * Create: allocate headphone downmixer
360  *****************************************************************************/
361 static int Create( vlc_object_t *p_this )
362 {
363     aout_filter_t * p_filter = (aout_filter_t *)p_this;
364     vlc_bool_t b_fit = VLC_TRUE;
365
366     /* Activate this filter only with stereo devices */
367     if( p_filter->output.i_physical_channels
368             != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
369     {
370         msg_Dbg( p_filter, "filter discarded (incompatible format)" );
371         return VLC_EGENERIC;
372     }
373
374     /* Request a specific format if not already compatible */
375     if( p_filter->input.i_original_channels
376             != p_filter->output.i_original_channels )
377     {
378         b_fit = VLC_FALSE;
379         p_filter->input.i_original_channels =
380                                         p_filter->output.i_original_channels;
381     }
382     if( 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         b_fit = VLC_FALSE;
386         p_filter->input.i_format = VLC_FOURCC('f','l','3','2');
387         p_filter->output.i_format = VLC_FOURCC('f','l','3','2');
388     }
389     if( p_filter->input.i_rate != p_filter->output.i_rate )
390     {
391         b_fit = VLC_FALSE;
392         p_filter->input.i_rate = p_filter->output.i_rate;
393     }
394     if( p_filter->input.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
395           && ( p_filter->input.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
396           && ! config_GetInt ( p_filter , "headphone-dolby" ) )
397     {
398         b_fit = VLC_FALSE;
399         p_filter->input.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
400                                               AOUT_CHAN_CENTER |
401                                               AOUT_CHAN_REARLEFT |
402                                               AOUT_CHAN_REARRIGHT;
403     }
404     if( ! b_fit )
405     {
406         msg_Dbg( p_filter, "requesting specific format" );
407         return VLC_EGENERIC;
408     }
409
410     /* Allocate the memory needed to store the module's structure */
411     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
412     if( p_filter->p_sys == NULL )
413     {
414         msg_Err( p_filter, "out of memory" );
415         return VLC_EGENERIC;
416     }
417     p_filter->p_sys->i_overflow_buffer_size = 0;
418     p_filter->p_sys->p_overflow_buffer = NULL;
419     p_filter->p_sys->i_nb_atomic_operations = 0;
420     p_filter->p_sys->p_atomic_operations = NULL;
421
422     if( Init( VLC_OBJECT(p_filter), p_filter->p_sys
423                 , aout_FormatNbChannels ( &p_filter->input )
424                 , p_filter->input.i_physical_channels
425                 , p_filter->input.i_rate ) < 0 )
426     {
427         return VLC_EGENERIC;
428     }
429
430     p_filter->pf_do_work = DoWork;
431     p_filter->b_in_place = 0;
432
433     return VLC_SUCCESS;
434 }
435
436 /*****************************************************************************
437  * Destroy: deallocate resources associated with headphone downmixer
438  *****************************************************************************/
439 static void Destroy( vlc_object_t *p_this )
440 {
441     aout_filter_t * p_filter = (aout_filter_t *)p_this;
442
443     if( p_filter->p_sys != NULL )
444     {
445         if( p_filter->p_sys->p_overflow_buffer != NULL )
446         {
447             free( p_filter->p_sys->p_overflow_buffer );
448         }
449         if( p_filter->p_sys->p_atomic_operations != NULL )
450         {
451             free( p_filter->p_sys->p_atomic_operations );
452         }
453         free( p_filter->p_sys );
454         p_filter->p_sys = NULL;
455     }
456 }
457
458 /*****************************************************************************
459  * DoWork: convert a buffer
460  *****************************************************************************/
461 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
462                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
463 {
464     int i_input_nb = aout_FormatNbChannels( &p_filter->input );
465     int i_output_nb = aout_FormatNbChannels( &p_filter->output );
466
467     float * p_in = (float*) p_in_buf->p_buffer;
468     byte_t * p_out;
469     byte_t * p_overflow;
470     byte_t * p_slide;
471
472     size_t i_overflow_size;     /* in bytes */
473     size_t i_out_size;          /* in bytes */
474
475     unsigned int i, j;
476
477     int i_source_channel_offset;
478     int i_dest_channel_offset;
479     unsigned int i_delay;
480     double d_amplitude_factor;
481
482     /* out buffer characterisitcs */
483     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
484     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
485     p_out = p_out_buf->p_buffer;
486     i_out_size = p_out_buf->i_nb_bytes;
487
488     if( p_filter->p_sys != NULL )
489     {
490         /* Slide the overflow buffer */
491         p_overflow = p_filter->p_sys->p_overflow_buffer;
492         i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
493
494         memset( p_out, 0, i_out_size );
495         if ( i_out_size > i_overflow_size )
496             memcpy( p_out, p_overflow, i_overflow_size );
497         else
498             memcpy( p_out, p_overflow, i_out_size );
499
500         p_slide = p_filter->p_sys->p_overflow_buffer;
501         while( p_slide < p_overflow + i_overflow_size )
502         {
503             if( p_slide + i_out_size < p_overflow + i_overflow_size )
504             {
505                 memset( p_slide, 0, i_out_size );
506                 if( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
507                     memcpy( p_slide, p_slide + i_out_size, i_out_size );
508                 else
509                     memcpy( p_slide, p_slide + i_out_size,
510                             p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
511             }
512             else
513             {
514                 memset( p_slide, 0, p_overflow + i_overflow_size - p_slide );
515             }
516             p_slide += i_out_size;
517         }
518
519         /* apply the atomic operations */
520         for( i = 0; i < p_filter->p_sys->i_nb_atomic_operations; i++ )
521         {
522             /* shorter variable names */
523             i_source_channel_offset
524                 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
525             i_dest_channel_offset
526                 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
527             i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
528             d_amplitude_factor
529                 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
530
531             if( p_out_buf->i_nb_samples > i_delay )
532             {
533                 /* current buffer coefficients */
534                 for( j = 0; j < p_out_buf->i_nb_samples - i_delay; j++ )
535                 {
536                     ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
537                         += p_in[ j * i_input_nb + i_source_channel_offset ]
538                            * d_amplitude_factor;
539                 }
540
541                 /* overflow buffer coefficients */
542                 for( j = 0; j < i_delay; j++ )
543                 {
544                     ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
545                         += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
546                            * i_input_nb + i_source_channel_offset ]
547                            * d_amplitude_factor;
548                 }
549             }
550             else
551             {
552                 /* overflow buffer coefficients only */
553                 for( j = 0; j < p_out_buf->i_nb_samples; j++ )
554                 {
555                     ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
556                         * i_output_nb + i_dest_channel_offset ]
557                         += p_in[ j * i_input_nb + i_source_channel_offset ]
558                            * d_amplitude_factor;
559                 }
560             }
561         }
562     }
563     else
564     {
565         memset( p_out, 0, i_out_size );
566     }
567 }
568
569 /*
570  * Audio filter 2
571  */
572 /*****************************************************************************
573  * OpenFilter:
574  *****************************************************************************/
575 static int OpenFilter( vlc_object_t *p_this )
576 {
577     filter_t *p_filter = (filter_t *)p_this;
578     vlc_bool_t b_fit = VLC_TRUE;
579
580     /* Activate this filter only with stereo devices */
581     if( p_filter->fmt_out.audio.i_physical_channels
582             != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
583     {
584         msg_Dbg( p_filter, "filter discarded (incompatible format)" );
585         return VLC_EGENERIC;
586     }
587
588     /* Request a specific format if not already compatible */
589     if( p_filter->fmt_in.audio.i_original_channels
590             != p_filter->fmt_out.audio.i_original_channels )
591     {
592         b_fit = VLC_FALSE;
593         p_filter->fmt_in.audio.i_original_channels =
594                                         p_filter->fmt_out.audio.i_original_channels;
595     }
596     if( 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         b_fit = VLC_FALSE;
600         p_filter->fmt_in.audio.i_format = VLC_FOURCC('f','l','3','2');
601         p_filter->fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
602     }
603     if( p_filter->fmt_in.audio.i_rate != p_filter->fmt_out.audio.i_rate )
604     {
605         b_fit = VLC_FALSE;
606         p_filter->fmt_in.audio.i_rate = p_filter->fmt_out.audio.i_rate;
607     }
608     if( p_filter->fmt_in.audio.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
609           && ( p_filter->fmt_in.audio.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
610           && !config_GetInt( p_filter, "headphone-dolby" ) )
611     {
612         b_fit = VLC_FALSE;
613         p_filter->fmt_in.audio.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
614                                               AOUT_CHAN_CENTER |
615                                               AOUT_CHAN_REARLEFT |
616                                               AOUT_CHAN_REARRIGHT;
617     }
618     if( !b_fit )
619     {
620         msg_Dbg( p_filter, "requesting specific format" );
621         return VLC_EGENERIC;
622     }
623
624     /* Allocate the memory needed to store the module's structure */
625     p_filter->p_sys = malloc( sizeof(struct filter_sys_t) );
626     if( p_filter->p_sys == NULL )
627     {
628         msg_Err( p_filter, "out of memory" );
629         return VLC_EGENERIC;
630     }
631     p_filter->p_sys->i_overflow_buffer_size = 0;
632     p_filter->p_sys->p_overflow_buffer = NULL;
633     p_filter->p_sys->i_nb_atomic_operations = 0;
634     p_filter->p_sys->p_atomic_operations = NULL;
635
636     if( Init( VLC_OBJECT(p_filter), (struct aout_filter_sys_t *)p_filter->p_sys
637                 , aout_FormatNbChannels ( &(p_filter->fmt_in.audio) )
638                 , p_filter->fmt_in.audio.i_physical_channels
639                 , p_filter->fmt_in.audio.i_rate ) < 0 )
640     {
641         return VLC_EGENERIC;
642     }
643
644     p_filter->pf_audio_filter = Convert;
645     p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
646
647     return VLC_SUCCESS;
648 }
649
650 /*****************************************************************************
651  * CloseFilter : deallocate data structures
652  *****************************************************************************/
653 static void CloseFilter( vlc_object_t *p_this )
654 {
655     filter_t *p_filter = (filter_t *)p_this;
656
657     if( p_filter->p_sys != NULL )
658     {
659         if( p_filter->p_sys->p_overflow_buffer != NULL )
660         {
661             free ( p_filter->p_sys->p_overflow_buffer );
662         }
663         if( p_filter->p_sys->p_atomic_operations != NULL )
664         {
665             free ( p_filter->p_sys->p_atomic_operations );
666         }
667         free( p_filter->p_sys );
668         p_filter->p_sys = NULL;
669     }
670 }
671
672 static block_t *Convert( filter_t *p_filter, block_t *p_block )
673 {
674     aout_filter_t aout_filter;
675     aout_buffer_t in_buf, out_buf;
676     block_t *p_out;
677     int i_out_size;
678
679     if( !p_block || !p_block->i_samples )
680     {
681         if( p_block ) p_block->pf_release( p_block );
682         return NULL;
683     }
684
685     i_out_size = p_block->i_samples *
686       p_filter->fmt_out.audio.i_bitspersample/8 *
687         aout_FormatNbChannels( &(p_filter->fmt_out.audio) );
688
689     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
690     if( !p_out )
691     {
692         msg_Warn( p_filter, "can't get output buffer" );
693         if( p_block ) p_block->pf_release( p_block );
694         return NULL;
695     }
696
697     p_out->i_samples = p_block->i_samples;
698     p_out->i_dts = p_block->i_dts;
699     p_out->i_pts = p_block->i_pts;
700     p_out->i_length = p_block->i_length;
701
702     aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
703     aout_filter.input = p_filter->fmt_in.audio;
704     aout_filter.input.i_format = p_filter->fmt_in.i_codec;
705     aout_filter.output = p_filter->fmt_out.audio;
706     aout_filter.output.i_format = p_filter->fmt_out.i_codec;
707     aout_filter.b_in_place = 0;
708
709     in_buf.p_buffer = p_block->p_buffer;
710     in_buf.i_nb_bytes = p_block->i_buffer;
711     in_buf.i_nb_samples = p_block->i_samples;
712     out_buf.p_buffer = p_out->p_buffer;
713     out_buf.i_nb_bytes = p_out->i_buffer;
714     out_buf.i_nb_samples = p_out->i_samples;
715
716     DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
717
718     p_out->i_buffer = out_buf.i_nb_bytes;
719     p_out->i_samples = out_buf.i_nb_samples;
720
721     if( p_block ) p_block->pf_release( p_block );
722     return p_out;
723 }