Fix a bunch of warnings and errors
[vlc.git] / modules / audio_mixer / float32.c
1 /*****************************************************************************
2  * float32.c : precise float32 audio mixer implementation
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include "audio_output.h"
32 #include "aout_internal.h"
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 static int  Create    ( vlc_object_t * );
38
39 static void DoWork    ( aout_instance_t *, aout_buffer_t * );
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 vlc_module_begin();
45     set_category( CAT_AUDIO );
46     set_subcategory( SUBCAT_AUDIO_MISC );
47     set_description( _("Float32 audio mixer") );
48     set_capability( "audio mixer", 10 );
49     set_callbacks( Create, NULL );
50 vlc_module_end();
51
52 /*****************************************************************************
53  * Create: allocate mixer
54  *****************************************************************************/
55 static int Create( vlc_object_t *p_this )
56 {
57     aout_instance_t * p_aout = (aout_instance_t *)p_this;
58
59     if ( p_aout->mixer.mixer.i_format != VLC_FOURCC('f','l','3','2') )
60     {
61         return -1;
62     }
63
64     if ( p_aout->i_nb_inputs == 1 && p_aout->mixer.f_multiplier == 1.0 )
65     {
66         /* Tell the trivial mixer to go for it. */
67         return -1;
68     }
69
70     p_aout->mixer.pf_do_work = DoWork;
71
72     return 0;
73 }
74
75 /*****************************************************************************
76  * ScaleWords: prepare input words for averaging
77  *****************************************************************************/
78 static void ScaleWords( float * p_out, const float * p_in, size_t i_nb_words,
79                         int i_nb_inputs, float f_multiplier )
80 {
81     int i;
82     f_multiplier /= i_nb_inputs;
83
84     for ( i = i_nb_words; i--; )
85     {
86         *p_out++ = *p_in++ * f_multiplier;
87     }
88 }
89
90 /*****************************************************************************
91  * MeanWords: average input words
92  *****************************************************************************/
93 static void MeanWords( float * p_out, const float * p_in, size_t i_nb_words,
94                        int i_nb_inputs, float f_multiplier )
95 {
96     int i;
97     f_multiplier /= i_nb_inputs;
98
99     for ( i = i_nb_words; i--; )
100     {
101         *p_out++ += *p_in++ * f_multiplier;
102     }
103 }
104
105 /*****************************************************************************
106  * DoWork: mix a new output buffer
107  *****************************************************************************
108  * Terminology : in this function a word designates a single float32, eg.
109  * a stereo sample is consituted of two words.
110  *****************************************************************************/
111 static void DoWork( aout_instance_t * p_aout, aout_buffer_t * p_buffer )
112 {
113     int i_nb_inputs = p_aout->i_nb_inputs;
114     float f_multiplier = p_aout->mixer.f_multiplier;
115     int i_input;
116     int i_nb_channels = aout_FormatNbChannels( &p_aout->mixer.mixer );
117
118     for ( i_input = 0; i_input < i_nb_inputs; i_input++ )
119     {
120         int i_nb_words = p_buffer->i_nb_samples * i_nb_channels;
121         aout_input_t * p_input = p_aout->pp_inputs[i_input];
122         float * p_out = (float *)p_buffer->p_buffer;
123         float * p_in = (float *)p_input->p_first_byte_to_mix;
124
125         if ( p_input->b_error ) continue;
126
127         for ( ; ; )
128         {
129             ptrdiff_t i_available_words = (
130                  (float *)p_input->fifo.p_first->p_buffer - p_in)
131                                    + p_input->fifo.p_first->i_nb_samples
132                                    * i_nb_channels;
133
134             if ( i_available_words < i_nb_words )
135             {
136                 aout_buffer_t * p_old_buffer;
137
138                 if ( i_available_words > 0 )
139                 {
140                     if ( !i_input )
141                     {
142                         ScaleWords( p_out, p_in, i_available_words,
143                                     i_nb_inputs, f_multiplier );
144                     }
145                     else
146                     {
147                         MeanWords( p_out, p_in, i_available_words,
148                                    i_nb_inputs, f_multiplier );
149                     }
150                 }
151
152                 i_nb_words -= i_available_words;
153                 p_out += i_available_words;
154
155                 /* Next buffer */
156                 p_old_buffer = aout_FifoPop( p_aout, &p_input->fifo );
157                 aout_BufferFree( p_old_buffer );
158                 if ( p_input->fifo.p_first == NULL )
159                 {
160                     msg_Err( p_aout, "internal amix error" );
161                     return;
162                 }
163                 p_in = (float *)p_input->fifo.p_first->p_buffer;
164             }
165             else
166             {
167                 if ( i_nb_words > 0 )
168                 {
169                     if ( !i_input )
170                     {
171                         ScaleWords( p_out, p_in, i_nb_words, i_nb_inputs,
172                                     f_multiplier );
173                     }
174                     else
175                     {
176                         MeanWords( p_out, p_in, i_nb_words, i_nb_inputs,
177                                    f_multiplier );
178                     }
179                 }
180                 p_input->p_first_byte_to_mix = (void *)(p_in
181                                             + i_nb_words);
182                 break;
183             }
184         }
185     }
186 }
187