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