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