]> git.sesse.net Git - vlc/blob - src/audio_output/mixer.c
* configure.in: Fixed detection of Qt-embedded.
[vlc] / src / audio_output / mixer.c
1 /*****************************************************************************
2  * mixer.c : audio output mixing operations
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: mixer.c,v 1.12 2002/09/16 20:46:38 massiot Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                            /* calloc(), malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31
32 #ifdef HAVE_ALLOCA_H
33 #   include <alloca.h> 
34 #endif
35
36 #include "audio_output.h"
37 #include "aout_internal.h"
38
39 /*****************************************************************************
40  * aout_MixerNew: prepare a mixer plug-in
41  *****************************************************************************
42  * Please note that you must hold the mixer lock.
43  *****************************************************************************/
44 int aout_MixerNew( aout_instance_t * p_aout )
45 {
46     p_aout->mixer.p_module = module_Need( p_aout, "audio mixer", NULL );
47     if ( p_aout->mixer.p_module == NULL )
48     {
49         msg_Err( p_aout, "no suitable aout mixer" );
50         return -1;
51     }
52     return 0;
53 }
54
55 /*****************************************************************************
56  * aout_MixerDelete: delete the mixer
57  *****************************************************************************
58  * Please note that you must hold the mixer lock.
59  *****************************************************************************/
60 void aout_MixerDelete( aout_instance_t * p_aout )
61 {
62     module_Unneed( p_aout, p_aout->mixer.p_module );
63 }
64
65 /*****************************************************************************
66  * MixBuffer: try to prepare one output buffer
67  *****************************************************************************/
68 static int MixBuffer( aout_instance_t * p_aout )
69 {
70     int             i;
71     aout_buffer_t * p_output_buffer;
72     mtime_t start_date, end_date;
73     audio_date_t exact_start_date;
74
75     vlc_mutex_lock( &p_aout->mixer_lock );
76     vlc_mutex_lock( &p_aout->output_fifo_lock );
77     vlc_mutex_lock( &p_aout->input_fifos_lock );
78
79     /* Retrieve the date of the next buffer. */
80     memcpy( &exact_start_date, &p_aout->output.fifo.end_date,
81             sizeof(audio_date_t) );
82     start_date = aout_DateGet( &exact_start_date );
83
84     if ( start_date != 0 && start_date < mdate() )
85     {
86         /* The output is _very_ late. This can only happen if the user
87          * pauses the stream (or if the decoder is buggy, which cannot
88          * happen :). */
89         msg_Warn( p_aout, "Output PTS is out of range (%lld), clearing out",
90                   start_date );
91         aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
92         aout_DateSet( &exact_start_date, 0 );
93         start_date = 0;
94     } 
95
96     vlc_mutex_unlock( &p_aout->output_fifo_lock );
97
98     /* See if we have enough data to prepare a new buffer for the audio
99      * output. First : start date. */
100     if ( !start_date )
101     {
102         /* Find the latest start date available. */
103         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
104         {
105             aout_input_t * p_input = p_aout->pp_inputs[i];
106             aout_fifo_t * p_fifo = &p_input->fifo;
107             aout_buffer_t * p_buffer;
108
109             p_buffer = p_fifo->p_first;
110             if ( p_buffer == NULL )
111             {
112                 break;
113             }
114
115             if ( !start_date || start_date < p_buffer->start_date )
116             {
117                 aout_DateSet( &exact_start_date, p_buffer->start_date );
118                 start_date = p_buffer->start_date;
119             }
120         }
121
122         if ( i < p_aout->i_nb_inputs )
123         {
124             /* Interrupted before the end... We can't run. */
125             vlc_mutex_unlock( &p_aout->input_fifos_lock );
126             vlc_mutex_unlock( &p_aout->mixer_lock );
127             return -1;
128         }
129     }
130     aout_DateIncrement( &exact_start_date, p_aout->output.i_nb_samples );
131     end_date = aout_DateGet( &exact_start_date );
132
133     /* Check that start_date and end_date are available for all input
134      * streams. */
135     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
136     {
137         aout_input_t * p_input = p_aout->pp_inputs[i];
138         aout_fifo_t * p_fifo = &p_input->fifo;
139         aout_buffer_t * p_buffer;
140         mtime_t prev_date;
141         vlc_bool_t b_drop_buffers;
142
143         p_buffer = p_fifo->p_first;
144         if ( p_buffer == NULL )
145         {
146             break;
147         }
148
149         /* Check for the continuity of start_date */
150         while ( p_buffer != NULL && p_buffer->end_date < start_date )
151         {
152             aout_buffer_t * p_next = p_buffer->p_next;
153             msg_Err( p_aout, "the mixer got a packet in the past (%lld)",
154                      start_date - p_buffer->end_date );
155             aout_BufferFree( p_buffer );
156             p_fifo->p_first = p_buffer = p_next;
157             p_input->p_first_byte_to_mix = NULL;
158         }
159         if ( p_buffer == NULL )
160         {
161             p_fifo->pp_last = &p_fifo->p_first;
162             break;
163         }
164
165         if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
166         {
167             /* Additionally check that p_first_byte_to_mix is well
168              * located. */
169             unsigned long i_nb_bytes = (start_date - p_buffer->start_date)
170                             * p_aout->mixer.mixer.i_bytes_per_frame
171                             * p_aout->mixer.mixer.i_rate
172                             / p_aout->mixer.mixer.i_frame_length
173                             / 1000000;
174             ptrdiff_t mixer_nb_bytes;
175
176             if ( p_input->p_first_byte_to_mix == NULL )
177             {
178                 p_input->p_first_byte_to_mix = p_buffer->p_buffer;
179             }
180             mixer_nb_bytes = p_input->p_first_byte_to_mix
181                               - p_buffer->p_buffer;
182
183             if ( !((i_nb_bytes + p_aout->mixer.mixer.i_bytes_per_frame
184                      > mixer_nb_bytes) &&
185                    (i_nb_bytes < p_aout->mixer.mixer.i_bytes_per_frame
186                      + mixer_nb_bytes)) )
187             {
188                 msg_Warn( p_aout,
189                           "mixer start isn't output start (%d)",
190                           i_nb_bytes - mixer_nb_bytes );
191
192                 /* Round to the nearest multiple */
193                 i_nb_bytes /= p_aout->mixer.mixer.i_bytes_per_frame;
194                 i_nb_bytes *= p_aout->mixer.mixer.i_bytes_per_frame;
195                 p_input->p_first_byte_to_mix = p_buffer->p_buffer
196                                                 + i_nb_bytes;
197             }
198         }
199
200         /* Check that we have enough samples. */
201         for ( ; ; )
202         {
203             p_buffer = p_fifo->p_first;
204             if ( p_buffer == NULL ) break;
205             if ( p_buffer->end_date >= end_date ) break;
206
207             /* Check that all buffers are contiguous. */
208             prev_date = p_fifo->p_first->end_date;
209             p_buffer = p_buffer->p_next;
210             b_drop_buffers = 0;
211             for ( ; p_buffer != NULL; p_buffer = p_buffer->p_next )
212             {
213                 if ( prev_date != p_buffer->start_date )
214                 {
215                     msg_Warn( p_aout,
216                               "buffer hole, dropping packets (%lld)",
217                               p_buffer->start_date - prev_date );
218                     b_drop_buffers = 1;
219                     break;
220                 }
221                 if ( p_buffer->end_date >= end_date ) break;
222                 prev_date = p_buffer->end_date;
223             }
224             if ( b_drop_buffers )
225             {
226                 aout_buffer_t * p_deleted = p_fifo->p_first;
227                 while ( p_deleted != NULL && p_deleted != p_buffer )
228                 {
229                     aout_buffer_t * p_next = p_deleted->p_next;
230                     aout_BufferFree( p_deleted );
231                     p_deleted = p_next;
232                 }
233                 p_fifo->p_first = p_deleted; /* == p_buffer */
234             }
235             else break;
236         }
237         if ( p_buffer == NULL ) break;
238     }
239
240     if ( i < p_aout->i_nb_inputs )
241     {
242         /* Interrupted before the end... We can't run. */
243         vlc_mutex_unlock( &p_aout->input_fifos_lock );
244         vlc_mutex_unlock( &p_aout->mixer_lock );
245         return -1;
246     }
247
248     /* Run the mixer. */
249     aout_BufferAlloc( &p_aout->mixer.output_alloc,
250                       ((u64)p_aout->output.i_nb_samples * 1000000)
251                         / p_aout->output.output.i_rate,
252                       /* This is a bit kludgy, but is actually only used
253                        * for the S/PDIF dummy mixer : */
254                       p_aout->pp_inputs[0]->fifo.p_first,
255                       p_output_buffer );
256     if ( p_output_buffer == NULL )
257     {
258         msg_Err( p_aout, "out of memory" );
259         vlc_mutex_unlock( &p_aout->input_fifos_lock );
260         vlc_mutex_unlock( &p_aout->mixer_lock );
261         return -1;
262     }
263     /* This is again a bit kludgy - for the S/PDIF mixer. */
264     if ( p_aout->mixer.output_alloc.i_alloc_type != AOUT_ALLOC_NONE )
265     {
266         p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
267         p_output_buffer->i_nb_bytes = p_aout->output.i_nb_samples
268                               * p_aout->mixer.mixer.i_bytes_per_frame
269                               / p_aout->mixer.mixer.i_frame_length;
270     }
271     p_output_buffer->start_date = start_date;
272     p_output_buffer->end_date = end_date;
273
274     p_aout->mixer.pf_do_work( p_aout, p_output_buffer );
275
276     vlc_mutex_unlock( &p_aout->input_fifos_lock );
277
278     aout_OutputPlay( p_aout, p_output_buffer );
279
280     vlc_mutex_unlock( &p_aout->mixer_lock );
281
282     return 0;
283 }
284
285 /*****************************************************************************
286  * aout_MixerRun: entry point for the mixer & post-filters processing
287  *****************************************************************************/
288 void aout_MixerRun( aout_instance_t * p_aout )
289 {
290     while( MixBuffer( p_aout ) != -1 );
291 }
292
293 /*****************************************************************************
294  * aout_MixerMultiplierSet: set p_aout->mixer.f_multiplier
295  *****************************************************************************
296  * Please note that we assume that you own the mixer lock when entering this
297  * function. This function returns -1 on error.
298  *****************************************************************************/
299 int aout_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier )
300 {
301     float f_old = p_aout->mixer.f_multiplier;
302
303     if ( p_aout->mixer.mixer.i_format != AOUT_FMT_FLOAT32 )
304     {
305         msg_Warn( p_aout, "MixerMultiplierSet called for non-float32 mixer" );
306         return -1;
307     }
308
309     aout_MixerDelete( p_aout );
310
311     p_aout->mixer.f_multiplier = f_multiplier;
312
313     if ( aout_MixerNew( p_aout ) )
314     {
315         p_aout->mixer.f_multiplier = f_old;
316         aout_MixerNew( p_aout );
317         return -1;
318     }
319
320     return 0;
321 }
322
323 /*****************************************************************************
324  * aout_MixerMultiplierGet: get p_aout->mixer.f_multiplier
325  *****************************************************************************
326  * Please note that we assume that you own the mixer lock when entering this
327  * function. This function returns -1 on error.
328  *****************************************************************************/
329 int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier )
330 {
331     if ( p_aout->mixer.mixer.i_format != AOUT_FMT_FLOAT32 )
332     {
333         msg_Warn( p_aout, "MixerMultiplierGet called for non-float32 mixer" );
334         return -1;
335     }
336
337     *pf_multiplier = p_aout->mixer.f_multiplier;
338     return 0;
339 }
340