]> git.sesse.net Git - vlc/blob - src/audio_output/mixer.c
c4d5437d9df3f388b5e4571695fbc1b004ee0870
[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.8 2002/08/19 23:12:57 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 int aout_MixerNew( aout_instance_t * p_aout )
43 {
44     p_aout->mixer.p_module = module_Need( p_aout, "audio mixer", NULL );
45     if ( p_aout->mixer.p_module == NULL )
46     {
47         msg_Err( p_aout, "no suitable aout mixer" );
48         return -1;
49     }
50     return 0;
51 }
52
53 /*****************************************************************************
54  * aout_MixerDelete: delete the mixer
55  *****************************************************************************/
56 void aout_MixerDelete( aout_instance_t * p_aout )
57 {
58     module_Unneed( p_aout, p_aout->mixer.p_module );
59 }
60
61 /*****************************************************************************
62  * MixBuffer: try to prepare one output buffer
63  *****************************************************************************/
64 static int MixBuffer( aout_instance_t * p_aout )
65 {
66     int             i;
67     aout_buffer_t * p_output_buffer;
68     mtime_t start_date, end_date;
69
70     /* Retrieve the date of the next buffer. */
71     vlc_mutex_lock( &p_aout->mixer_lock );
72     start_date = aout_FifoNextStart( p_aout, &p_aout->output.fifo );
73     if ( start_date != 0 && start_date < mdate() )
74     {
75         /* The output is _very_ late. This can only happen if the user
76          * pauses the stream (or if the decoder is buggy, which cannot
77          * happen :). */
78         msg_Warn( p_aout, "Output PTS is out of range (%lld), clearing out",
79                   start_date );
80         start_date = p_aout->output.fifo.end_date = 0;
81     } 
82
83     /* See if we have enough data to prepare a new buffer for the audio
84      * output. First : start date. */
85     if ( !start_date )
86     {
87         /* Find the latest start date available. */
88         for ( i = 0; i < p_aout->i_nb_inputs; i++ )
89         {
90             aout_input_t * p_input = p_aout->pp_inputs[i];
91             aout_fifo_t * p_fifo = &p_input->fifo;
92             aout_buffer_t * p_buffer;
93
94             p_buffer = p_fifo->p_first;
95             if ( p_buffer == NULL )
96             {
97                 break;
98             }
99
100             if ( !start_date || start_date < p_buffer->start_date )
101             {
102                 start_date = p_buffer->start_date;
103             }
104         }
105
106         if ( i < p_aout->i_nb_inputs )
107         {
108             /* Interrupted before the end... We can't run. */
109             vlc_mutex_unlock( &p_aout->mixer_lock );
110             return -1;
111         }
112     }
113     end_date = start_date + (mtime_t)p_aout->output.i_nb_samples * 1000000
114                              / p_aout->output.output.i_rate;
115
116     /* Check that start_date and end_date are available for all input
117      * streams. */
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_fifo_t * p_fifo = &p_input->fifo;
122         aout_buffer_t * p_buffer;
123         mtime_t prev_date;
124         vlc_bool_t b_drop_buffers;
125
126         p_buffer = p_fifo->p_first;
127         if ( p_buffer == NULL )
128         {
129             break;
130         }
131
132         /* Check for the continuity of start_date */
133         while ( p_buffer != NULL && p_buffer->end_date < start_date )
134         {
135             aout_buffer_t * p_next = p_buffer->p_next;
136             msg_Err( p_aout, "the mixer got a packet in the past (%lld)",
137                      start_date - p_buffer->end_date );
138             aout_BufferFree( p_buffer );
139             p_fifo->p_first = p_buffer = p_next;
140             p_input->p_first_byte_to_mix = NULL;
141         }
142         if ( p_buffer == NULL )
143         {
144             p_fifo->pp_last = &p_fifo->p_first;
145             break;
146         }
147
148         if ( !AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
149         {
150             /* Additionally check that p_first_byte_to_mix is well
151              * located. */
152             unsigned long i_nb_bytes = (start_date - p_buffer->start_date)
153                             * p_aout->mixer.mixer.i_bytes_per_frame
154                             * p_aout->mixer.mixer.i_rate
155                             / p_aout->mixer.mixer.i_frame_length
156                             / 1000000;
157             ptrdiff_t mixer_nb_bytes;
158
159             if ( p_input->p_first_byte_to_mix == NULL )
160             {
161                 p_input->p_first_byte_to_mix = p_buffer->p_buffer;
162             }
163             mixer_nb_bytes = p_input->p_first_byte_to_mix
164                               - p_buffer->p_buffer;
165
166             if ( i_nb_bytes + p_aout->mixer.mixer.i_bytes_per_frame
167                   < mixer_nb_bytes ||
168                  i_nb_bytes - p_aout->mixer.mixer.i_bytes_per_frame
169                   > mixer_nb_bytes )
170             {
171                 msg_Warn( p_aout,
172                           "mixer start isn't output start (%ld)",
173                           i_nb_bytes - mixer_nb_bytes );
174
175                 /* Round to the nearest multiple */
176                 i_nb_bytes /= p_aout->mixer.mixer.i_bytes_per_frame;
177                 i_nb_bytes *= p_aout->mixer.mixer.i_bytes_per_frame;
178                 p_input->p_first_byte_to_mix = p_buffer->p_buffer
179                                                 + i_nb_bytes;
180             }
181         }
182
183         /* Check that we have enough samples. */
184         for ( ; ; )
185         {
186             p_buffer = p_fifo->p_first;
187             if ( p_buffer == NULL ) break;
188             if ( p_buffer->end_date >= end_date ) break;
189
190             /* Check that all buffers are contiguous. */
191             prev_date = p_fifo->p_first->end_date;
192             p_buffer = p_buffer->p_next;
193             b_drop_buffers = 0;
194             for ( ; p_buffer != NULL; p_buffer = p_buffer->p_next )
195             {
196                 if ( prev_date != p_buffer->start_date )
197                 {
198                     msg_Warn( p_aout,
199                               "buffer hole, dropping packets (%lld)",
200                               p_buffer->start_date - prev_date );
201                     b_drop_buffers = 1;
202                     break;
203                 }
204                 if ( p_buffer->end_date >= end_date ) break;
205                 prev_date = p_buffer->end_date;
206             }
207             if ( b_drop_buffers )
208             {
209                 aout_buffer_t * p_deleted = p_fifo->p_first;
210                 while ( p_deleted != NULL && p_deleted != p_buffer )
211                 {
212                     aout_buffer_t * p_next = p_deleted->p_next;
213                     aout_BufferFree( p_deleted );
214                     p_deleted = p_next;
215                 }
216                 p_fifo->p_first = p_deleted; /* == p_buffer */
217             }
218             else break;
219         }
220         if ( p_buffer == NULL ) break;
221     }
222
223     if ( i < p_aout->i_nb_inputs )
224     {
225         /* Interrupted before the end... We can't run. */
226         vlc_mutex_unlock( &p_aout->mixer_lock );
227         return -1;
228     }
229
230     /* Run the mixer. */
231     aout_BufferAlloc( &p_aout->mixer.output_alloc,
232                       ((u64)p_aout->output.i_nb_samples * 1000000)
233                         / p_aout->output.output.i_rate,
234                       /* This is a bit kludgy, but is actually only used
235                        * for the S/PDIF dummy mixer : */
236                       p_aout->pp_inputs[0]->fifo.p_first,
237                       p_output_buffer );
238     if ( p_output_buffer == NULL )
239     {
240         msg_Err( p_aout, "out of memory" );
241         vlc_mutex_unlock( &p_aout->mixer_lock );
242         return -1;
243     }
244     p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
245     p_output_buffer->i_nb_bytes = p_aout->output.i_nb_samples
246                               * p_aout->mixer.mixer.i_bytes_per_frame
247                               / p_aout->mixer.mixer.i_frame_length;
248     p_output_buffer->start_date = start_date;
249     p_output_buffer->end_date = end_date;
250
251     p_aout->mixer.pf_do_work( p_aout, p_output_buffer );
252
253     vlc_mutex_unlock( &p_aout->mixer_lock );
254
255     aout_OutputPlay( p_aout, p_output_buffer );
256
257     return 0;
258 }
259
260 /*****************************************************************************
261  * aout_MixerRun: entry point for the mixer & post-filters processing
262  *****************************************************************************/
263 void aout_MixerRun( aout_instance_t * p_aout )
264 {
265     while( MixBuffer( p_aout ) != -1 );
266 }