]> git.sesse.net Git - vlc/blob - modules/audio_filter/channel_mixer/headphone.c
Remove unneeded msg_Error about memory failure.
[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_common.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( N_("Headphone virtual spatialization effect") );
82     set_shortname( N_("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( N_("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         return -1;
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         return -1;
347     memset( p_data->p_overflow_buffer, 0 , p_data->i_overflow_buffer_size );
348
349     /* end */
350     return 0;
351 }
352
353 /*****************************************************************************
354  * Create: allocate headphone downmixer
355  *****************************************************************************/
356 static int Create( vlc_object_t *p_this )
357 {
358     aout_filter_t * p_filter = (aout_filter_t *)p_this;
359     bool b_fit = true;
360
361     /* Activate this filter only with stereo devices */
362     if( p_filter->output.i_physical_channels
363             != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
364     {
365         msg_Dbg( p_filter, "filter discarded (incompatible format)" );
366         return VLC_EGENERIC;
367     }
368
369     /* Request a specific format if not already compatible */
370     if( p_filter->input.i_original_channels
371             != p_filter->output.i_original_channels )
372     {
373         b_fit = false;
374         p_filter->input.i_original_channels =
375                                         p_filter->output.i_original_channels;
376     }
377     if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2')
378           || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
379     {
380         b_fit = false;
381         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     if( p_filter->input.i_rate != p_filter->output.i_rate )
385     {
386         b_fit = false;
387         p_filter->input.i_rate = p_filter->output.i_rate;
388     }
389     if( p_filter->input.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
390           && ( p_filter->input.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
391           && ! config_GetInt ( p_filter , "headphone-dolby" ) )
392     {
393         b_fit = false;
394         p_filter->input.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
395                                               AOUT_CHAN_CENTER |
396                                               AOUT_CHAN_REARLEFT |
397                                               AOUT_CHAN_REARRIGHT;
398     }
399     if( ! b_fit )
400     {
401         msg_Dbg( p_filter, "requesting specific format" );
402         return VLC_EGENERIC;
403     }
404
405     /* Allocate the memory needed to store the module's structure */
406     p_filter->p_sys = malloc( sizeof(struct aout_filter_sys_t) );
407     if( p_filter->p_sys == NULL )
408         return VLC_ENOMEM;
409     p_filter->p_sys->i_overflow_buffer_size = 0;
410     p_filter->p_sys->p_overflow_buffer = NULL;
411     p_filter->p_sys->i_nb_atomic_operations = 0;
412     p_filter->p_sys->p_atomic_operations = NULL;
413
414     if( Init( VLC_OBJECT(p_filter), p_filter->p_sys
415                 , aout_FormatNbChannels ( &p_filter->input )
416                 , p_filter->input.i_physical_channels
417                 , p_filter->input.i_rate ) < 0 )
418     {
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         if( p_filter->p_sys->p_overflow_buffer != NULL )
438         {
439             free( p_filter->p_sys->p_overflow_buffer );
440         }
441         if( p_filter->p_sys->p_atomic_operations != NULL )
442         {
443             free( p_filter->p_sys->p_atomic_operations );
444         }
445         free( p_filter->p_sys );
446         p_filter->p_sys = NULL;
447     }
448 }
449
450 /*****************************************************************************
451  * DoWork: convert a buffer
452  *****************************************************************************/
453 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
454                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
455 {
456     VLC_UNUSED(p_aout);
457     int i_input_nb = aout_FormatNbChannels( &p_filter->input );
458     int i_output_nb = aout_FormatNbChannels( &p_filter->output );
459
460     float * p_in = (float*) p_in_buf->p_buffer;
461     uint8_t * p_out;
462     uint8_t * p_overflow;
463     uint8_t * p_slide;
464
465     size_t i_overflow_size;     /* in bytes */
466     size_t i_out_size;          /* in bytes */
467
468     unsigned int i, j;
469
470     int i_source_channel_offset;
471     int i_dest_channel_offset;
472     unsigned int i_delay;
473     double d_amplitude_factor;
474
475     /* out buffer characterisitcs */
476     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
477     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes * i_output_nb / i_input_nb;
478     p_out = p_out_buf->p_buffer;
479     i_out_size = p_out_buf->i_nb_bytes;
480
481     if( p_filter->p_sys != NULL )
482     {
483         /* Slide the overflow buffer */
484         p_overflow = p_filter->p_sys->p_overflow_buffer;
485         i_overflow_size = p_filter->p_sys->i_overflow_buffer_size;
486
487         memset( p_out, 0, i_out_size );
488         if ( i_out_size > i_overflow_size )
489             memcpy( p_out, p_overflow, i_overflow_size );
490         else
491             memcpy( p_out, p_overflow, i_out_size );
492
493         p_slide = p_filter->p_sys->p_overflow_buffer;
494         while( p_slide < p_overflow + i_overflow_size )
495         {
496             if( p_slide + i_out_size < p_overflow + i_overflow_size )
497             {
498                 memset( p_slide, 0, i_out_size );
499                 if( p_slide + 2 * i_out_size < p_overflow + i_overflow_size )
500                     memcpy( p_slide, p_slide + i_out_size, i_out_size );
501                 else
502                     memcpy( p_slide, p_slide + i_out_size,
503                             p_overflow + i_overflow_size - ( p_slide + i_out_size ) );
504             }
505             else
506             {
507                 memset( p_slide, 0, p_overflow + i_overflow_size - p_slide );
508             }
509             p_slide += i_out_size;
510         }
511
512         /* apply the atomic operations */
513         for( i = 0; i < p_filter->p_sys->i_nb_atomic_operations; i++ )
514         {
515             /* shorter variable names */
516             i_source_channel_offset
517                 = p_filter->p_sys->p_atomic_operations[i].i_source_channel_offset;
518             i_dest_channel_offset
519                 = p_filter->p_sys->p_atomic_operations[i].i_dest_channel_offset;
520             i_delay = p_filter->p_sys->p_atomic_operations[i].i_delay;
521             d_amplitude_factor
522                 = p_filter->p_sys->p_atomic_operations[i].d_amplitude_factor;
523
524             if( p_out_buf->i_nb_samples > i_delay )
525             {
526                 /* current buffer coefficients */
527                 for( j = 0; j < p_out_buf->i_nb_samples - i_delay; j++ )
528                 {
529                     ((float*)p_out)[ (i_delay+j)*i_output_nb + i_dest_channel_offset ]
530                         += p_in[ j * i_input_nb + i_source_channel_offset ]
531                            * d_amplitude_factor;
532                 }
533
534                 /* overflow buffer coefficients */
535                 for( j = 0; j < i_delay; j++ )
536                 {
537                     ((float*)p_overflow)[ j*i_output_nb + i_dest_channel_offset ]
538                         += p_in[ (p_out_buf->i_nb_samples - i_delay + j)
539                            * i_input_nb + i_source_channel_offset ]
540                            * d_amplitude_factor;
541                 }
542             }
543             else
544             {
545                 /* overflow buffer coefficients only */
546                 for( j = 0; j < p_out_buf->i_nb_samples; j++ )
547                 {
548                     ((float*)p_overflow)[ (i_delay - p_out_buf->i_nb_samples + j)
549                         * i_output_nb + i_dest_channel_offset ]
550                         += p_in[ j * i_input_nb + i_source_channel_offset ]
551                            * d_amplitude_factor;
552                 }
553             }
554         }
555     }
556     else
557     {
558         memset( p_out, 0, i_out_size );
559     }
560 }
561
562 /*
563  * Audio filter 2
564  */
565 /*****************************************************************************
566  * OpenFilter:
567  *****************************************************************************/
568 static int OpenFilter( vlc_object_t *p_this )
569 {
570     filter_t *p_filter = (filter_t *)p_this;
571     bool b_fit = true;
572
573     /* Activate this filter only with stereo devices */
574     if( p_filter->fmt_out.audio.i_physical_channels
575             != (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
576     {
577         msg_Dbg( p_filter, "filter discarded (incompatible format)" );
578         return VLC_EGENERIC;
579     }
580
581     /* Request a specific format if not already compatible */
582     if( p_filter->fmt_in.audio.i_original_channels
583             != p_filter->fmt_out.audio.i_original_channels )
584     {
585         b_fit = false;
586         p_filter->fmt_in.audio.i_original_channels =
587                                         p_filter->fmt_out.audio.i_original_channels;
588     }
589     if( p_filter->fmt_in.audio.i_format != VLC_FOURCC('f','l','3','2')
590           || p_filter->fmt_out.audio.i_format != VLC_FOURCC('f','l','3','2') )
591     {
592         b_fit = false;
593         p_filter->fmt_in.audio.i_format = VLC_FOURCC('f','l','3','2');
594         p_filter->fmt_out.audio.i_format = VLC_FOURCC('f','l','3','2');
595     }
596     if( p_filter->fmt_in.audio.i_rate != p_filter->fmt_out.audio.i_rate )
597     {
598         b_fit = false;
599         p_filter->fmt_in.audio.i_rate = p_filter->fmt_out.audio.i_rate;
600     }
601     if( p_filter->fmt_in.audio.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT)
602           && ( p_filter->fmt_in.audio.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
603           && !config_GetInt( p_filter, "headphone-dolby" ) )
604     {
605         b_fit = false;
606         p_filter->fmt_in.audio.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
607                                               AOUT_CHAN_CENTER |
608                                               AOUT_CHAN_REARLEFT |
609                                               AOUT_CHAN_REARRIGHT;
610     }
611     if( !b_fit )
612     {
613         msg_Dbg( p_filter, "requesting specific format" );
614         return VLC_EGENERIC;
615     }
616
617     /* Allocate the memory needed to store the module's structure */
618     p_filter->p_sys = malloc( sizeof(struct filter_sys_t) );
619     if( p_filter->p_sys == NULL )
620         return VLC_ENOMEM;
621     p_filter->p_sys->i_overflow_buffer_size = 0;
622     p_filter->p_sys->p_overflow_buffer = NULL;
623     p_filter->p_sys->i_nb_atomic_operations = 0;
624     p_filter->p_sys->p_atomic_operations = NULL;
625
626     if( Init( VLC_OBJECT(p_filter), (struct aout_filter_sys_t *)p_filter->p_sys
627                 , aout_FormatNbChannels ( &(p_filter->fmt_in.audio) )
628                 , p_filter->fmt_in.audio.i_physical_channels
629                 , p_filter->fmt_in.audio.i_rate ) < 0 )
630     {
631         return VLC_EGENERIC;
632     }
633
634     p_filter->pf_audio_filter = Convert;
635     p_filter->fmt_out.audio.i_rate = p_filter->fmt_in.audio.i_rate;
636
637     return VLC_SUCCESS;
638 }
639
640 /*****************************************************************************
641  * CloseFilter : deallocate data structures
642  *****************************************************************************/
643 static void CloseFilter( vlc_object_t *p_this )
644 {
645     filter_t *p_filter = (filter_t *)p_this;
646
647     if( p_filter->p_sys != NULL )
648     {
649         if( p_filter->p_sys->p_overflow_buffer != NULL )
650         {
651             free ( p_filter->p_sys->p_overflow_buffer );
652         }
653         if( p_filter->p_sys->p_atomic_operations != NULL )
654         {
655             free ( p_filter->p_sys->p_atomic_operations );
656         }
657         free( p_filter->p_sys );
658         p_filter->p_sys = NULL;
659     }
660 }
661
662 static block_t *Convert( filter_t *p_filter, block_t *p_block )
663 {
664     aout_filter_t aout_filter;
665     aout_buffer_t in_buf, out_buf;
666     block_t *p_out;
667     int i_out_size;
668
669     if( !p_block || !p_block->i_samples )
670     {
671         if( p_block ) p_block->pf_release( p_block );
672         return NULL;
673     }
674
675     i_out_size = p_block->i_samples *
676       p_filter->fmt_out.audio.i_bitspersample/8 *
677         aout_FormatNbChannels( &(p_filter->fmt_out.audio) );
678
679     p_out = p_filter->pf_audio_buffer_new( p_filter, i_out_size );
680     if( !p_out )
681     {
682         msg_Warn( p_filter, "can't get output buffer" );
683         if( p_block ) p_block->pf_release( p_block );
684         return NULL;
685     }
686
687     p_out->i_samples = p_block->i_samples;
688     p_out->i_dts = p_block->i_dts;
689     p_out->i_pts = p_block->i_pts;
690     p_out->i_length = p_block->i_length;
691
692     aout_filter.p_sys = (struct aout_filter_sys_t *)p_filter->p_sys;
693     aout_filter.input = p_filter->fmt_in.audio;
694     aout_filter.input.i_format = p_filter->fmt_in.i_codec;
695     aout_filter.output = p_filter->fmt_out.audio;
696     aout_filter.output.i_format = p_filter->fmt_out.i_codec;
697     aout_filter.b_in_place = 0;
698
699     in_buf.p_buffer = p_block->p_buffer;
700     in_buf.i_nb_bytes = p_block->i_buffer;
701     in_buf.i_nb_samples = p_block->i_samples;
702     out_buf.p_buffer = p_out->p_buffer;
703     out_buf.i_nb_bytes = p_out->i_buffer;
704     out_buf.i_nb_samples = p_out->i_samples;
705
706     DoWork( (aout_instance_t *)p_filter, &aout_filter, &in_buf, &out_buf );
707
708     p_out->i_buffer = out_buf.i_nb_bytes;
709     p_out->i_samples = out_buf.i_nb_samples;
710
711     if( p_block ) p_block->pf_release( p_block );
712     return p_out;
713 }