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