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