]> git.sesse.net Git - vlc/blob - src/audio_output/mixer.c
Don't include config.h from the headers - refs #297.
[vlc] / src / audio_output / mixer.c
1 /*****************************************************************************
2  * mixer.c : audio output mixing operations
3  *****************************************************************************
4  * Copyright (C) 2002-2004 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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc/vlc.h>
32
33 #ifdef HAVE_ALLOCA_H
34 #   include <alloca.h>
35 #endif
36 #include <vlc_aout.h>
37 #include "aout_internal.h"
38 /*****************************************************************************
39  * aout_MixerNew: prepare a mixer plug-in
40  *****************************************************************************
41  * Please note that you must hold the mixer lock.
42  *****************************************************************************/
43 int aout_MixerNew( aout_instance_t * p_aout )
44 {
45     p_aout->mixer.p_module = module_Need( p_aout, "audio mixer", NULL, 0 );
46     if ( p_aout->mixer.p_module == NULL )
47     {
48         msg_Err( p_aout, "no suitable audio mixer" );
49         return -1;
50     }
51     p_aout->mixer.b_error = 0;
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     if ( p_aout->mixer.b_error ) return;
63     module_Unneed( p_aout, p_aout->mixer.p_module );
64     p_aout->mixer.b_error = 1;
65 }
66
67 /*****************************************************************************
68  * MixBuffer: try to prepare one output buffer
69  *****************************************************************************
70  * Please note that you must hold the mixer lock.
71  *****************************************************************************/
72 static int MixBuffer( aout_instance_t * p_aout )
73 {
74     int             i, i_first_input = 0;
75     aout_buffer_t * p_output_buffer;
76     mtime_t start_date, end_date;
77     audio_date_t exact_start_date;
78
79     if ( p_aout->mixer.b_error )
80     {
81         /* Free all incoming buffers. */
82         vlc_mutex_lock( &p_aout->input_fifos_lock );
83         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
84         {
85             aout_input_t * p_input = p_aout->pp_inputs[i];
86             aout_buffer_t * p_buffer = p_input->fifo.p_first;
87             if ( p_input->b_error ) continue;
88             while ( p_buffer != NULL )
89             {
90                 aout_buffer_t * p_next = p_buffer->p_next;
91                 aout_BufferFree( p_buffer );
92                 p_buffer = p_next;
93             }
94         }
95         vlc_mutex_unlock( &p_aout->input_fifos_lock );
96         return -1;
97     }
98
99
100     vlc_mutex_lock( &p_aout->output_fifo_lock );
101     vlc_mutex_lock( &p_aout->input_fifos_lock );
102
103     /* Retrieve the date of the next buffer. */
104     memcpy( &exact_start_date, &p_aout->output.fifo.end_date,
105             sizeof(audio_date_t) );
106     start_date = aout_DateGet( &exact_start_date );
107
108     if ( start_date != 0 && start_date < mdate() )
109     {
110         /* The output is _very_ late. This can only happen if the user
111          * pauses the stream (or if the decoder is buggy, which cannot
112          * happen :). */
113         msg_Warn( p_aout, "output PTS is out of range ("I64Fd"), clearing out",
114                   mdate() - start_date );
115         aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
116         aout_DateSet( &exact_start_date, 0 );
117         start_date = 0;
118     }
119
120     vlc_mutex_unlock( &p_aout->output_fifo_lock );
121
122     /* See if we have enough data to prepare a new buffer for the audio
123      * output. First : start date. */
124     if ( !start_date )
125     {
126         /* Find the latest start date available. */
127         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
128         {
129             aout_input_t * p_input = p_aout->pp_inputs[i];
130             aout_fifo_t * p_fifo = &p_input->fifo;
131             aout_buffer_t * p_buffer;
132
133             if ( p_input->b_error ) continue;
134
135             p_buffer = p_fifo->p_first;
136             while ( p_buffer != NULL && p_buffer->start_date < mdate() )
137             {
138                 msg_Warn( p_aout, "input PTS is out of range ("I64Fd"), "
139                           "trashing", mdate() - p_buffer->start_date );
140                 p_buffer = aout_FifoPop( p_aout, p_fifo );
141                 aout_BufferFree( p_buffer );
142                 if( p_input->p_input_thread )
143                 {
144 //                    stats_UpdateInteger( p_input->p_input_thread,
145 //                                         "lost_abuffers", 1 );
146                 }
147                 p_buffer = p_fifo->p_first;
148                 p_input->p_first_byte_to_mix = NULL;
149             }
150
151             if ( p_buffer == NULL )
152             {
153                 break;
154             }
155
156             if ( !start_date || start_date < p_buffer->start_date )
157             {
158                 aout_DateSet( &exact_start_date, p_buffer->start_date );
159                 start_date = p_buffer->start_date;
160             }
161         }
162
163         if ( i < p_aout->i_nb_inputs )
164         {
165             /* Interrupted before the end... We can't run. */
166             vlc_mutex_unlock( &p_aout->input_fifos_lock );
167             return -1;
168         }
169     }
170     aout_DateIncrement( &exact_start_date, p_aout->output.i_nb_samples );
171     end_date = aout_DateGet( &exact_start_date );
172
173     /* Check that start_date and end_date are available for all input
174      * streams. */
175     for ( i = 0; i < p_aout->i_nb_inputs; i++ )
176     {
177         aout_input_t * p_input = p_aout->pp_inputs[i];
178         aout_fifo_t * p_fifo = &p_input->fifo;
179         aout_buffer_t * p_buffer;
180         mtime_t prev_date;
181         vlc_bool_t b_drop_buffers;
182
183         if ( p_input->b_error )
184         {
185             if ( i_first_input == i ) i_first_input++;
186             continue;
187         }
188
189         p_buffer = p_fifo->p_first;
190         if ( p_buffer == NULL )
191         {
192             break;
193         }
194
195         /* Check for the continuity of start_date */
196         while ( p_buffer != NULL && p_buffer->end_date < start_date - 1 )
197         {
198             /* We authorize a +-1 because rounding errors get compensated
199              * regularly. */
200             aout_buffer_t * p_next = p_buffer->p_next;
201             msg_Warn( p_aout, "the mixer got a packet in the past ("I64Fd")",
202                       start_date - p_buffer->end_date );
203             aout_BufferFree( p_buffer );
204             if( p_input->p_input_thread )
205             {
206 //                stats_UpdateInteger( p_input->p_input_thread,
207 //                                     "lost_abuffers", 1 );
208             }
209             p_fifo->p_first = p_buffer = p_next;
210             p_input->p_first_byte_to_mix = NULL;
211         }
212         if ( p_buffer == NULL )
213         {
214             p_fifo->pp_last = &p_fifo->p_first;
215             break;
216         }
217
218         /* Check that we have enough samples. */
219         for ( ; ; )
220         {
221             p_buffer = p_fifo->p_first;
222             if ( p_buffer == NULL ) break;
223             if ( p_buffer->end_date >= end_date ) break;
224
225             /* Check that all buffers are contiguous. */
226             prev_date = p_fifo->p_first->end_date;
227             p_buffer = p_buffer->p_next;
228             b_drop_buffers = 0;
229             for ( ; p_buffer != NULL; p_buffer = p_buffer->p_next )
230             {
231                 if ( prev_date != p_buffer->start_date )
232                 {
233                     msg_Warn( p_aout,
234                               "buffer hole, dropping packets ("I64Fd")",
235                               p_buffer->start_date - prev_date );
236                     b_drop_buffers = 1;
237                     break;
238                 }
239                 if ( p_buffer->end_date >= end_date ) break;
240                 prev_date = p_buffer->end_date;
241             }
242             if ( b_drop_buffers )
243             {
244                 aout_buffer_t * p_deleted = p_fifo->p_first;
245                 while ( p_deleted != NULL && p_deleted != p_buffer )
246                 {
247                     aout_buffer_t * p_next = p_deleted->p_next;
248                     aout_BufferFree( p_deleted );
249                     p_deleted = p_next;
250                 }
251                 p_fifo->p_first = p_deleted; /* == p_buffer */
252             }
253             else break;
254         }
255         if ( p_buffer == NULL ) break;
256
257         p_buffer = p_fifo->p_first;
258         if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
259         {
260             /* Additionally check that p_first_byte_to_mix is well
261              * located. */
262             mtime_t i_nb_bytes = (start_date - p_buffer->start_date)
263                             * p_aout->mixer.mixer.i_bytes_per_frame
264                             * p_aout->mixer.mixer.i_rate
265                             / p_aout->mixer.mixer.i_frame_length
266                             / 1000000;
267             ptrdiff_t mixer_nb_bytes;
268
269             if ( p_input->p_first_byte_to_mix == NULL )
270             {
271                 p_input->p_first_byte_to_mix = p_buffer->p_buffer;
272             }
273             mixer_nb_bytes = p_input->p_first_byte_to_mix - p_buffer->p_buffer;
274
275             if ( !((i_nb_bytes + p_aout->mixer.mixer.i_bytes_per_frame
276                      > mixer_nb_bytes) &&
277                    (i_nb_bytes < p_aout->mixer.mixer.i_bytes_per_frame
278                      + mixer_nb_bytes)) )
279             {
280                 msg_Warn( p_aout, "mixer start isn't output start ("I64Fd")",
281                           i_nb_bytes - mixer_nb_bytes );
282
283                 /* Round to the nearest multiple */
284                 i_nb_bytes /= p_aout->mixer.mixer.i_bytes_per_frame;
285                 i_nb_bytes *= p_aout->mixer.mixer.i_bytes_per_frame;
286                 if( i_nb_bytes < 0 )
287                 {
288                     /* Is it really the best way to do it ? */
289                     aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
290                     aout_DateSet( &exact_start_date, 0 );
291                     break;
292                 }
293
294                 p_input->p_first_byte_to_mix = p_buffer->p_buffer + i_nb_bytes;
295             }
296         }
297     }
298
299     if ( i < p_aout->i_nb_inputs || i_first_input == p_aout->i_nb_inputs )
300     {
301         /* Interrupted before the end... We can't run. */
302         vlc_mutex_unlock( &p_aout->input_fifos_lock );
303         return -1;
304     }
305
306     /* Run the mixer. */
307     aout_BufferAlloc( &p_aout->mixer.output_alloc,
308                       ((uint64_t)p_aout->output.i_nb_samples * 1000000)
309                         / p_aout->output.output.i_rate,
310                       /* This is a bit kludgy, but is actually only used
311                        * for the S/PDIF dummy mixer : */
312                       p_aout->pp_inputs[i_first_input]->fifo.p_first,
313                       p_output_buffer );
314     if ( p_output_buffer == NULL )
315     {
316         msg_Err( p_aout, "out of memory" );
317         vlc_mutex_unlock( &p_aout->input_fifos_lock );
318         return -1;
319     }
320     /* This is again a bit kludgy - for the S/PDIF mixer. */
321     if ( p_aout->mixer.output_alloc.i_alloc_type != AOUT_ALLOC_NONE )
322     {
323         p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
324         p_output_buffer->i_nb_bytes = p_aout->output.i_nb_samples
325                               * p_aout->mixer.mixer.i_bytes_per_frame
326                               / p_aout->mixer.mixer.i_frame_length;
327     }
328     p_output_buffer->start_date = start_date;
329     p_output_buffer->end_date = end_date;
330
331     p_aout->mixer.pf_do_work( p_aout, p_output_buffer );
332
333     vlc_mutex_unlock( &p_aout->input_fifos_lock );
334
335     aout_OutputPlay( p_aout, p_output_buffer );
336
337     return 0;
338 }
339
340 /*****************************************************************************
341  * aout_MixerRun: entry point for the mixer & post-filters processing
342  *****************************************************************************
343  * Please note that you must hold the mixer lock.
344  *****************************************************************************/
345 void aout_MixerRun( aout_instance_t * p_aout )
346 {
347     while( MixBuffer( p_aout ) != -1 );
348 }
349
350 /*****************************************************************************
351  * aout_MixerMultiplierSet: set p_aout->mixer.f_multiplier
352  *****************************************************************************
353  * Please note that we assume that you own the mixer lock when entering this
354  * function. This function returns -1 on error.
355  *****************************************************************************/
356 int aout_MixerMultiplierSet( aout_instance_t * p_aout, float f_multiplier )
357 {
358     float f_old = p_aout->mixer.f_multiplier;
359     vlc_bool_t b_new_mixer = 0;
360
361     if ( !p_aout->mixer.b_error )
362     {
363         aout_MixerDelete( p_aout );
364         b_new_mixer = 1;
365     }
366
367     p_aout->mixer.f_multiplier = f_multiplier;
368
369     if ( b_new_mixer && aout_MixerNew( p_aout ) )
370     {
371         p_aout->mixer.f_multiplier = f_old;
372         aout_MixerNew( p_aout );
373         return -1;
374     }
375
376     return 0;
377 }
378
379 /*****************************************************************************
380  * aout_MixerMultiplierGet: get p_aout->mixer.f_multiplier
381  *****************************************************************************
382  * Please note that we assume that you own the mixer lock when entering this
383  * function. This function returns -1 on error.
384  *****************************************************************************/
385 int aout_MixerMultiplierGet( aout_instance_t * p_aout, float * pf_multiplier )
386 {
387     *pf_multiplier = p_aout->mixer.f_multiplier;
388     return 0;
389 }
390