]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
d41ea877596b5e0defa9cc784c4c462626b1a1c2
[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.38 2003/09/02 18:06:45 gbazin 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 /*****************************************************************************
41  * aout_InputNew : allocate a new input and rework the filter pipeline
42  *****************************************************************************/
43 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input )
44 {
45     audio_sample_format_t intermediate_format;
46
47     char * psz_filters;
48
49     aout_FormatPrint( p_aout, "input", &p_input->input );
50
51     /* Prepare FIFO. */
52     aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
53     p_input->p_first_byte_to_mix = NULL;
54
55     /* Prepare format structure */
56     memcpy( &intermediate_format, &p_aout->mixer.mixer,
57             sizeof(audio_sample_format_t) );
58     intermediate_format.i_rate = p_input->input.i_rate;
59
60     /* Create filters. */
61     if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
62                                      &p_input->i_nb_filters,
63                                      &p_input->input,
64                                      &intermediate_format
65                                      ) < 0 )
66     {
67         msg_Err( p_aout, "couldn't set an input pipeline" );
68
69         aout_FifoDestroy( p_aout, &p_input->fifo );
70         p_input->b_error = 1;
71         return -1;
72     }
73
74     /* Now add user filters */
75     if( ( psz_filters = config_GetPsz( p_aout , "audio-filter" ) ) )
76     {
77         char *psz_parser = psz_filters;
78         char *psz_next;
79         audio_sample_format_t format_in, format_out;
80
81         memcpy( &format_in, &p_aout->mixer.mixer,
82                 sizeof( audio_sample_format_t ) );
83         memcpy( &format_out,&p_aout->mixer.mixer,
84                 sizeof( audio_sample_format_t ) );
85
86         format_in.i_rate  = p_input->input.i_rate;
87         format_out.i_rate = p_input->input.i_rate;
88
89         while( psz_parser && *psz_parser )
90         {
91             aout_filter_t * p_filter;
92
93             if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
94             {
95                 msg_Dbg( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
96                 break;
97             }
98
99             while( *psz_parser == ' ' && *psz_parser == ',' )
100             {
101                 psz_parser++;
102             }
103             if( ( psz_next = strchr( psz_parser , ','  ) ) )
104             {
105                 *psz_next++ = '\0';
106             }
107             if( *psz_parser =='\0' )
108             {
109                 break;
110             }
111
112             msg_Dbg( p_aout, "user filter %s", psz_parser );
113
114             /* Create a VLC object */
115             p_filter = vlc_object_create( p_aout, sizeof(aout_filter_t) );
116             if( p_filter == NULL )
117             {
118                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
119                          psz_parser );
120                 psz_parser = psz_next;
121                 continue;
122             }
123
124             vlc_object_attach( p_filter , p_aout );
125             memcpy( &p_filter->input, &format_in,
126                     sizeof(audio_sample_format_t) );
127             memcpy( &p_filter->output, &format_out,
128                     sizeof(audio_sample_format_t) );
129
130             p_filter->p_module =
131                 module_Need( p_filter,"audio filter", psz_parser );
132
133             if( p_filter->p_module== NULL )
134             {
135                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
136                          psz_parser );
137
138                 vlc_object_detach( p_filter );
139                 vlc_object_destroy( p_filter );
140                 psz_parser = psz_next;
141                 continue;
142
143             }
144             p_filter->b_continuity = VLC_FALSE;
145
146             p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
147
148             /* next filter if any */
149             psz_parser = psz_next;
150         }
151         free( psz_filters );
152     }
153
154
155     /* Prepare hints for the buffer allocator. */
156     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
157     p_input->input_alloc.i_bytes_per_sec = -1;
158
159     if ( AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
160     {
161         p_input->i_nb_resamplers = 0;
162     }
163     else
164     {
165         /* Create resamplers. */
166         intermediate_format.i_rate = (__MAX(p_input->input.i_rate,
167                                             p_aout->mixer.mixer.i_rate)
168                                  * (100 + AOUT_MAX_RESAMPLING)) / 100;
169         if ( intermediate_format.i_rate == p_aout->mixer.mixer.i_rate )
170         {
171             /* Just in case... */
172             intermediate_format.i_rate++;
173         }
174         if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
175                                          &p_input->i_nb_resamplers,
176                                          &intermediate_format,
177                                          &p_aout->mixer.mixer ) < 0 )
178         {
179             msg_Err( p_aout, "couldn't set a resampler pipeline" );
180
181             aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
182                                          p_input->i_nb_filters );
183             aout_FifoDestroy( p_aout, &p_input->fifo );
184             p_input->b_error = 1;
185
186             return -1;
187         }
188
189         aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
190                                  p_input->i_nb_resamplers,
191                                  &p_input->input_alloc );
192
193         /* Setup the initial rate of the resampler */
194         p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
195     }
196     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
197
198     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
199     aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
200                              p_input->i_nb_filters,
201                              &p_input->input_alloc );
202
203     /* i_bytes_per_sec is still == -1 if no filters */
204     p_input->input_alloc.i_bytes_per_sec = __MAX(
205                                     p_input->input_alloc.i_bytes_per_sec,
206                                     (int)(p_input->input.i_bytes_per_frame
207                                      * p_input->input.i_rate
208                                      / p_input->input.i_frame_length) );
209     /* Allocate in the heap, it is more convenient for the decoder. */
210     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
211
212     p_input->b_error = 0;
213
214     return 0;
215 }
216
217 /*****************************************************************************
218  * aout_InputDelete : delete an input
219  *****************************************************************************
220  * This function must be entered with the mixer lock.
221  *****************************************************************************/
222 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
223 {
224     if ( p_input->b_error ) return 0;
225
226     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
227                                  p_input->i_nb_filters );
228     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
229                                  p_input->i_nb_resamplers );
230     aout_FifoDestroy( p_aout, &p_input->fifo );
231
232     return 0;
233 }
234
235 /*****************************************************************************
236  * aout_InputPlay : play a buffer
237  *****************************************************************************
238  * This function must be entered with the input lock.
239  *****************************************************************************/
240 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
241                     aout_buffer_t * p_buffer )
242 {
243     mtime_t start_date;
244
245     /* We don't care if someone changes the start date behind our back after
246      * this. We'll deal with that when pushing the buffer, and compensate
247      * with the next incoming buffer. */
248     vlc_mutex_lock( &p_aout->input_fifos_lock );
249     start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
250     vlc_mutex_unlock( &p_aout->input_fifos_lock );
251
252     if ( start_date != 0 && start_date < mdate() )
253     {
254         /* The decoder is _very_ late. This can only happen if the user
255          * pauses the stream (or if the decoder is buggy, which cannot
256          * happen :). */
257         msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
258                   "clearing out", mdate() - start_date );
259         vlc_mutex_lock( &p_aout->input_fifos_lock );
260         aout_FifoSet( p_aout, &p_input->fifo, 0 );
261         p_input->p_first_byte_to_mix = NULL;
262         vlc_mutex_unlock( &p_aout->input_fifos_lock );
263         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
264             msg_Warn( p_aout, "timing screwed, stopping resampling" );
265         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
266         if ( p_input->i_nb_resamplers != 0 )
267         {
268             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
269             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
270         }
271         start_date = 0;
272     }
273
274     if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
275     {
276         /* The decoder gives us f*cked up PTS. It's its business, but we
277          * can't present it anyway, so drop the buffer. */
278         msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
279                   mdate() - p_buffer->start_date );
280         aout_BufferFree( p_buffer );
281         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
282         if ( p_input->i_nb_resamplers != 0 )
283         {
284             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
285             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
286         }
287         return 0;
288     }
289
290     if ( start_date == 0 ) start_date = p_buffer->start_date;
291
292     /* Run pre-filters. */
293
294     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
295                       &p_buffer );
296
297     /* Run the resampler if needed.
298      * We first need to calculate the output rate of this resampler. */
299     if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
300          ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
301            || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
302          p_input->i_nb_resamplers > 0 )
303     {
304         /* Can happen in several circumstances :
305          * 1. A problem at the input (clock drift)
306          * 2. A small pause triggered by the user
307          * 3. Some delay in the output stage, causing a loss of lip
308          *    synchronization
309          * Solution : resample the buffer to avoid a scratch.
310          */
311         mtime_t drift = p_buffer->start_date - start_date;
312
313         p_input->i_resamp_start_date = mdate();
314         p_input->i_resamp_start_drift = (int)drift;
315
316         if ( drift > 0 )
317             p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
318         else
319             p_input->i_resampling_type = AOUT_RESAMPLING_UP;
320
321         msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
322                           drift > 0 ? drift : -drift,
323                           drift > 0 ? "in advance" : "late",
324                           drift > 0 ? "down" : "up");
325     }
326
327     if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
328     {
329         /* Resampling has been triggered previously (because of dates
330          * mismatch). We want the resampling to happen progressively so
331          * it isn't too audible to the listener. */
332
333         if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
334         {
335             p_input->pp_resamplers[0]->input.i_rate += 10; /* Hz */
336         }
337         else
338         {
339             p_input->pp_resamplers[0]->input.i_rate -= 10; /* Hz */
340         }
341
342         /* Check if everything is back to normal, in which case we can stop the
343          * resampling */
344         if( p_input->pp_resamplers[0]->input.i_rate ==
345               p_input->input.i_rate )
346         {
347             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
348             msg_Warn( p_aout, "resampling stopped after "I64Fi" usec",
349                       mdate() - p_input->i_resamp_start_date );
350         }
351         else if( abs( (int)(p_buffer->start_date - start_date) ) <
352                  abs( p_input->i_resamp_start_drift ) / 2 )
353         {
354             /* if we reduced the drift from half, then it is time to switch
355              * back the resampling direction. */
356             if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
357                 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
358             else
359                 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
360             p_input->i_resamp_start_drift = 0;
361         }
362         else if( p_input->i_resamp_start_drift &&
363                  ( abs( (int)(p_buffer->start_date - start_date) ) >
364                    abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
365         {
366             /* If the drift is increasing and not decreasing, than something
367              * is bad. We'd better stop the resampling right now. */
368             msg_Warn( p_aout, "timing screwed, stopping resampling" );
369             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
370             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
371         }
372     }
373
374     /* Adding the start date will be managed by aout_FifoPush(). */
375     p_buffer->end_date = start_date +
376         (p_buffer->end_date - p_buffer->start_date);
377     p_buffer->start_date = start_date;
378
379     /* Actually run the resampler now. */
380     if ( p_input->i_nb_resamplers > 0 )
381     {
382         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
383                           p_input->i_nb_resamplers,
384                           &p_buffer );
385     }
386
387     vlc_mutex_lock( &p_aout->input_fifos_lock );
388     aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
389     vlc_mutex_unlock( &p_aout->input_fifos_lock );
390
391     return 0;
392 }