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