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