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