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