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