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