]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
Big round of fixes in the aout3.
[vlc] / src / audio_output / input.c
1 /*****************************************************************************
2  * input.c : internal management of input streams for the audio output
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: input.c,v 1.7 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 #include "audio_output.h"
33 #include "aout_internal.h"
34
35 /*****************************************************************************
36  * aout_InputNew : allocate a new input and rework the filter pipeline
37  *****************************************************************************/
38 static aout_input_t * InputNew( aout_instance_t * p_aout,
39                                 audio_sample_format_t * p_format )
40 {
41     aout_input_t * p_input = malloc(sizeof(aout_input_t));
42
43     if ( p_input == NULL ) return NULL;
44
45     vlc_mutex_lock( &p_aout->mixer_lock );
46
47     if ( p_aout->i_nb_inputs == 0 )
48     {
49         /* Recreate the output using the new format. */
50         if ( aout_OutputNew( p_aout, p_format ) < 0 )
51         {
52             free( p_input );
53             return NULL;
54         }
55     }
56     else
57     {
58         aout_MixerDelete( p_aout );
59     }
60
61     memcpy( &p_input->input, p_format,
62             sizeof(audio_sample_format_t) );
63     aout_FormatPrepare( &p_input->input );
64
65     /* Prepare FIFO. */
66     aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
67     p_input->p_first_byte_to_mix = NULL;
68
69     /* Create filters. */
70     if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
71                                      &p_input->i_nb_filters, &p_input->input,
72                                      &p_aout->mixer.mixer ) < 0 )
73     {
74         msg_Err( p_aout, "couldn't set an input pipeline" );
75
76         aout_FifoDestroy( p_aout, &p_input->fifo );
77
78         free( p_input );
79
80         if ( !p_aout->i_nb_inputs )
81         {
82             aout_OutputDelete( p_aout );
83         }
84         return NULL;
85     }
86
87     p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
88     p_aout->i_nb_inputs++;
89
90     if ( aout_MixerNew( p_aout ) < 0 )
91     {
92         p_aout->i_nb_inputs--;
93         aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
94                                      p_input->i_nb_filters );
95         aout_FifoDestroy( p_aout, &p_input->fifo );
96
97         free( p_input );
98
99         if ( !p_aout->i_nb_inputs )
100         {
101             aout_OutputDelete( p_aout );
102         }
103         else
104         {
105             aout_MixerNew( p_aout );
106         }
107         vlc_mutex_unlock( &p_aout->mixer_lock );
108
109         return NULL;
110     }
111
112     vlc_mutex_unlock( &p_aout->mixer_lock );
113
114     /* Prepare hints for the buffer allocator. */
115     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
116     p_input->input_alloc.i_bytes_per_sec = -1;
117
118     aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
119                              p_input->i_nb_filters,
120                              &p_input->input_alloc );
121
122     /* i_bytes_per_sec is still == -1 if no filters */
123     p_input->input_alloc.i_bytes_per_sec = __MAX(
124                                     p_input->input_alloc.i_bytes_per_sec,
125                                     p_input->input.i_bytes_per_frame
126                                      * p_input->input.i_rate
127                                      / p_input->input.i_frame_length );
128     /* Allocate in the heap, it is more convenient for the decoder. */
129     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
130
131     msg_Dbg( p_aout, "input 0x%x created", p_input );
132     return p_input;
133 }
134
135 aout_input_t * __aout_InputNew( vlc_object_t * p_this,
136                                 aout_instance_t ** pp_aout,
137                                 audio_sample_format_t * p_format )
138 {
139     /* Create an audio output if there is none. */
140     *pp_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
141
142     if( *pp_aout == NULL )
143     {
144         msg_Dbg( p_this, "no aout present, spawning one" );
145
146         *pp_aout = aout_NewInstance( p_this );
147         /* Everything failed, I'm a loser, I just wanna die */
148         if( *pp_aout == NULL )
149         {
150             return NULL;
151         }
152     }
153     else
154     {
155         vlc_object_release( *pp_aout );
156     }
157
158     return InputNew( *pp_aout, p_format );
159 }
160
161 /*****************************************************************************
162  * aout_InputDelete : delete an input
163  *****************************************************************************/
164 void aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
165 {
166     int i_input;
167
168     vlc_mutex_lock( &p_aout->mixer_lock );
169
170     for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
171     {
172         if ( p_aout->pp_inputs[i_input] == p_input )
173         {
174             break;
175         }
176     }
177
178     if ( i_input == p_aout->i_nb_inputs )
179     {
180         msg_Err( p_aout, "cannot find an input to delete" );
181         return;
182     }
183
184     /* Remove the input from the list. */
185     memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
186              (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
187     p_aout->i_nb_inputs--;
188
189     vlc_mutex_unlock( &p_aout->mixer_lock );
190
191     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
192                                  p_input->i_nb_filters );
193     aout_FifoDestroy( p_aout, &p_input->fifo );
194
195     free( p_input );
196
197     if ( !p_aout->i_nb_inputs )
198     {
199         aout_OutputDelete( p_aout );
200         aout_MixerDelete( p_aout );
201     }
202
203     msg_Dbg( p_aout, "input 0x%x destroyed", p_input );
204 }
205
206 /*****************************************************************************
207  * aout_InputPlay : play a buffer
208  *****************************************************************************/
209 void aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
210                      aout_buffer_t * p_buffer )
211 {
212     mtime_t start_date, duration;
213
214     vlc_mutex_lock( &p_aout->input_lock );
215     while( p_aout->b_change_requested )
216     {
217         vlc_cond_wait( &p_aout->input_signal, &p_aout->input_lock );
218     }
219     p_aout->i_inputs_active++;
220     vlc_mutex_unlock( &p_aout->input_lock );
221
222     /* We don't care if someone changes the start date behind our back after
223      * this. We'll deal with that when pushing the buffer, and compensate
224      * with the next incoming buffer. */
225     start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
226
227     if ( start_date != 0 && start_date < mdate() )
228     {
229         /* The decoder is _very_ late. This can only happen if the user
230          * pauses the stream (or if the decoder is buggy, which cannot
231          * happen :). */
232         msg_Warn( p_aout, "Computed PTS is out of range (%lld), clearing out",
233                   start_date );
234         vlc_mutex_lock( &p_aout->mixer_lock );
235         aout_FifoSet( p_aout, &p_input->fifo, 0 );
236         vlc_mutex_unlock( &p_aout->mixer_lock );
237         start_date = 0;
238     } 
239
240     if ( p_buffer->start_date < mdate() - AOUT_MIN_PREPARE_TIME )
241     {
242         /* The decoder gives us f*cked up PTS. It's its business, but we
243          * can't present it anyway, so drop the buffer. */
244         msg_Warn( p_aout, "PTS is out of range (%lld), dropping buffer",
245                   mdate() - p_buffer->start_date );
246         aout_BufferFree( p_buffer );
247
248         vlc_mutex_lock( &p_aout->input_lock );
249         p_aout->i_inputs_active--;
250         vlc_cond_broadcast( &p_aout->input_signal );
251         vlc_mutex_unlock( &p_aout->input_lock );
252         return;
253     }
254
255     if ( start_date == 0 ) start_date = p_buffer->start_date;
256
257     if ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
258           || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE )
259     {
260         /* Can happen in several circumstances :
261          * 1. A problem at the input (clock drift)
262          * 2. A small pause triggered by the user
263          * 3. Some delay in the output stage, causing a loss of lip
264          *    synchronization
265          * Solution : resample the buffer to avoid a scratch.
266          */
267         audio_sample_format_t new_output;
268         int i_ratio, i_nb_filters;
269         mtime_t old_duration;
270         aout_filter_t * pp_filters[AOUT_MAX_FILTERS];
271         aout_buffer_t * p_new_buffer;
272         aout_alloc_t dummy_alloc;
273         mtime_t drift = p_buffer->start_date - start_date;
274
275         msg_Warn( p_aout, "buffer is %lld %s, resampling",
276                          drift > 0 ? drift : -drift,
277                          drift > 0 ? "in advance" : "late" );
278         memcpy( &new_output, &p_aout->mixer.mixer,
279                 sizeof(audio_sample_format_t) );
280         old_duration = p_buffer->end_date - p_buffer->start_date;
281         duration = p_buffer->end_date - start_date;
282         i_ratio = duration * 100 / old_duration;
283         /* If the ratio is too != 100, the sound quality will be awful. */
284         if ( i_ratio < 66 /* % */ )
285         {
286             duration = old_duration * 66 / 100;
287         }
288         if ( i_ratio > 150 /* % */ )
289         {
290             duration = old_duration * 150 / 100;
291         }
292         new_output.i_rate = new_output.i_rate * duration / old_duration;
293
294         if ( aout_FiltersCreatePipeline( p_aout, pp_filters,
295                                          &i_nb_filters, &p_input->input,
296                                          &new_output ) < 0 )
297         {
298             msg_Err( p_aout, "couldn't set an input pipeline for resampling" );
299             vlc_mutex_lock( &p_aout->mixer_lock );
300             aout_FifoSet( p_aout, &p_input->fifo, 0 );
301             vlc_mutex_unlock( &p_aout->mixer_lock );
302             aout_BufferFree( p_buffer );
303
304             vlc_mutex_lock( &p_aout->input_lock );
305             p_aout->i_inputs_active--;
306             vlc_cond_broadcast( &p_aout->input_signal );
307             vlc_mutex_unlock( &p_aout->input_lock );
308             return;
309         }
310
311         dummy_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
312         dummy_alloc.i_bytes_per_sec = -1;
313         aout_FiltersHintBuffers( p_aout, pp_filters, i_nb_filters,
314                                  &dummy_alloc );
315         dummy_alloc.i_bytes_per_sec = __MAX(
316                                     dummy_alloc.i_bytes_per_sec,
317                                     p_input->input.i_bytes_per_frame
318                                      * p_input->input.i_rate
319                                      / p_input->input.i_frame_length );
320         dummy_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
321
322         aout_BufferAlloc( &dummy_alloc, old_duration, NULL, p_new_buffer );
323         memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
324                 p_buffer->i_nb_bytes );
325         p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
326         p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
327         aout_BufferFree( p_buffer );
328         p_buffer = p_new_buffer;
329
330         aout_FiltersPlay( p_aout, pp_filters, i_nb_filters,
331                           &p_buffer );
332
333         aout_FiltersDestroyPipeline( p_aout, pp_filters,
334                                      i_nb_filters );
335     }
336     else
337     {
338         /* No resampling needed (except maybe the one imposed by the
339          * output). */
340         duration = p_buffer->end_date - p_buffer->start_date;
341         aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
342                           &p_buffer );
343     }
344
345     vlc_mutex_lock( &p_aout->mixer_lock );
346     /* Adding the start date will be managed by aout_FifoPush(). */
347     p_buffer->start_date = start_date;
348     p_buffer->end_date = start_date + duration;
349     aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
350     vlc_mutex_unlock( &p_aout->mixer_lock );
351
352     vlc_mutex_lock( &p_aout->input_lock );
353     p_aout->i_inputs_active--;
354     vlc_cond_broadcast( &p_aout->input_signal );
355     vlc_mutex_unlock( &p_aout->input_lock );
356 }