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