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