]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
* include/aout_internal.h: added a b_restart field to aout_input_t.
[vlc] / src / audio_output / input.c
1 /*****************************************************************************
2  * input.c : internal management of input streams for the audio output
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id$
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 #include <vlc/input.h>                 /* for input_thread_t and i_pts_delay */
32
33 #ifdef HAVE_ALLOCA_H
34 #   include <alloca.h>
35 #endif
36
37 #include "audio_output.h"
38 #include "aout_internal.h"
39
40 static int VisualizationCallback( vlc_object_t *, char const *,
41                                 vlc_value_t, vlc_value_t, void * );
42 static aout_filter_t * allocateUserChannelMixer( aout_instance_t *,
43                                                  audio_sample_format_t *,
44                                                  audio_sample_format_t * );
45
46 /*****************************************************************************
47  * aout_InputNew : allocate a new input and rework the filter pipeline
48  *****************************************************************************/
49 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input )
50 {
51     audio_sample_format_t user_filter_format;
52     audio_sample_format_t intermediate_format;/* input of resampler */
53     vlc_value_t val, text;
54     char * psz_filters;
55     aout_filter_t * p_user_channel_mixer;
56
57     aout_FormatPrint( p_aout, "input", &p_input->input );
58
59     /* Prepare FIFO. */
60     aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
61     p_input->p_first_byte_to_mix = NULL;
62
63     /* Prepare format structure */
64     memcpy( &intermediate_format, &p_aout->mixer.mixer,
65             sizeof(audio_sample_format_t) );
66     intermediate_format.i_rate = p_input->input.i_rate;
67
68     /* Try to use the channel mixer chosen by the user */
69     memcpy ( &user_filter_format, &intermediate_format,
70              sizeof(audio_sample_format_t) );
71     user_filter_format.i_physical_channels = p_input->input.i_physical_channels;
72     user_filter_format.i_original_channels = p_input->input.i_original_channels;
73     user_filter_format.i_bytes_per_frame = user_filter_format.i_bytes_per_frame
74                               * aout_FormatNbChannels( &user_filter_format )
75                               / aout_FormatNbChannels( &intermediate_format );
76     p_user_channel_mixer = allocateUserChannelMixer( p_aout, &user_filter_format,
77                                                    &intermediate_format );
78     /* If it failed, let the main pipeline do channel mixing */
79     if ( ! p_user_channel_mixer )
80     {
81         memcpy ( &user_filter_format, &intermediate_format,
82                  sizeof(audio_sample_format_t) );
83     }
84
85     /* Create filters. */
86     if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
87                                      &p_input->i_nb_filters,
88                                      &p_input->input,
89                                      &user_filter_format
90                                      ) < 0 )
91     {
92         msg_Err( p_aout, "couldn't set an input pipeline" );
93
94         aout_FifoDestroy( p_aout, &p_input->fifo );
95         p_input->b_error = 1;
96         return -1;
97     }
98
99     /* Now add user filters */
100     if( var_Type( p_aout, "visual" ) == 0 )
101     {
102         module_t *p_module;
103         var_Create( p_aout, "visual", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
104         text.psz_string = _("Visualizations");
105         var_Change( p_aout, "visual", VLC_VAR_SETTEXT, &text, NULL );
106         val.psz_string = ""; text.psz_string = _("Disable");
107         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
108         val.psz_string = "random"; text.psz_string = _("Random");
109         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
110         val.psz_string = "scope"; text.psz_string = _("Scope");
111         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
112         val.psz_string = "spectrum"; text.psz_string = _("Spectrum");
113         var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
114
115         /* Look for goom plugin */
116         p_module = config_FindModule( VLC_OBJECT(p_aout), "goom" );
117         if( p_module )
118         {
119             val.psz_string = "goom"; text.psz_string = _("Goom");
120             var_Change( p_aout, "visual", VLC_VAR_ADDCHOICE, &val, &text );
121         }
122
123         if( var_Get( p_aout, "effect-list", &val ) == VLC_SUCCESS )
124         {
125             var_Set( p_aout, "visual", val );
126             if( val.psz_string ) free( val.psz_string );
127         }
128         var_AddCallback( p_aout, "visual", VisualizationCallback, NULL );
129     }
130
131     if( var_Type( p_aout, "audio-filter" ) == 0 )
132     {
133         var_Create( p_aout, "audio-filter",
134                     VLC_VAR_STRING | VLC_VAR_DOINHERIT );
135         text.psz_string = _("Audio filters");
136         var_Change( p_aout, "audio-filter", VLC_VAR_SETTEXT, &text, NULL );
137     }
138
139     var_Get( p_aout, "audio-filter", &val );
140     psz_filters = val.psz_string;
141     if( psz_filters && *psz_filters )
142     {
143         char *psz_parser = psz_filters;
144         char *psz_next;
145
146         while( psz_parser && *psz_parser )
147         {
148             aout_filter_t * p_filter;
149
150             if( p_input->i_nb_filters >= AOUT_MAX_FILTERS )
151             {
152                 msg_Dbg( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
153                 break;
154             }
155
156             while( *psz_parser == ' ' && *psz_parser == ',' )
157             {
158                 psz_parser++;
159             }
160             if( ( psz_next = strchr( psz_parser , ','  ) ) )
161             {
162                 *psz_next++ = '\0';
163             }
164             if( *psz_parser =='\0' )
165             {
166                 break;
167             }
168
169             msg_Dbg( p_aout, "user filter \"%s\"", psz_parser );
170
171             /* Create a VLC object */
172             p_filter = vlc_object_create( p_aout, sizeof(aout_filter_t) );
173             if( p_filter == NULL )
174             {
175                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
176                          psz_parser );
177                 psz_parser = psz_next;
178                 continue;
179             }
180
181             vlc_object_attach( p_filter , p_aout );
182             memcpy( &p_filter->input, &user_filter_format,
183                     sizeof(audio_sample_format_t) );
184             memcpy( &p_filter->output, &user_filter_format,
185                     sizeof(audio_sample_format_t) );
186
187             p_filter->p_module =
188                 module_Need( p_filter,"audio filter", psz_parser, VLC_TRUE );
189
190             if( p_filter->p_module== NULL )
191             {
192                 msg_Err( p_aout, "cannot add user filter %s (skipped)",
193                          psz_parser );
194
195                 vlc_object_detach( p_filter );
196                 vlc_object_destroy( p_filter );
197                 psz_parser = psz_next;
198                 continue;
199
200             }
201             p_filter->b_continuity = VLC_FALSE;
202
203             p_input->pp_filters[p_input->i_nb_filters++] = p_filter;
204
205             /* next filter if any */
206             psz_parser = psz_next;
207         }
208     }
209     if( psz_filters ) free( psz_filters );
210
211     /* Attach the user channel mixer */
212     if ( p_user_channel_mixer )
213     {
214         p_input->pp_filters[p_input->i_nb_filters++] = p_user_channel_mixer;
215     }
216
217     /* Prepare hints for the buffer allocator. */
218     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
219     p_input->input_alloc.i_bytes_per_sec = -1;
220
221     if ( AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
222     {
223         p_input->i_nb_resamplers = 0;
224     }
225     else
226     {
227         /* Create resamplers. */
228         intermediate_format.i_rate = (__MAX(p_input->input.i_rate,
229                                             p_aout->mixer.mixer.i_rate)
230                                  * (100 + AOUT_MAX_RESAMPLING)) / 100;
231         if ( intermediate_format.i_rate == p_aout->mixer.mixer.i_rate )
232         {
233             /* Just in case... */
234             intermediate_format.i_rate++;
235         }
236         if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
237                                          &p_input->i_nb_resamplers,
238                                          &intermediate_format,
239                                          &p_aout->mixer.mixer ) < 0 )
240         {
241             msg_Err( p_aout, "couldn't set a resampler pipeline" );
242
243             aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
244                                          p_input->i_nb_filters );
245             aout_FifoDestroy( p_aout, &p_input->fifo );
246             var_Destroy( p_aout, "visual" );
247             p_input->b_error = 1;
248
249             return -1;
250         }
251
252         aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
253                                  p_input->i_nb_resamplers,
254                                  &p_input->input_alloc );
255
256         /* Setup the initial rate of the resampler */
257         p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
258     }
259     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
260
261     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
262     aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
263                              p_input->i_nb_filters,
264                              &p_input->input_alloc );
265
266     /* i_bytes_per_sec is still == -1 if no filters */
267     p_input->input_alloc.i_bytes_per_sec = __MAX(
268                                     p_input->input_alloc.i_bytes_per_sec,
269                                     (int)(p_input->input.i_bytes_per_frame
270                                      * p_input->input.i_rate
271                                      / p_input->input.i_frame_length) );
272     /* Allocate in the heap, it is more convenient for the decoder. */
273     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
274
275     p_input->b_error = VLC_FALSE;
276     p_input->b_restart = VLC_FALSE;
277
278     return 0;
279 }
280
281 /*****************************************************************************
282  * aout_InputDelete : delete an input
283  *****************************************************************************
284  * This function must be entered with the mixer lock.
285  *****************************************************************************/
286 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
287 {
288     if ( p_input->b_error ) return 0;
289
290     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
291                                  p_input->i_nb_filters );
292     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
293                                  p_input->i_nb_resamplers );
294     aout_FifoDestroy( p_aout, &p_input->fifo );
295
296     return 0;
297 }
298
299 /*****************************************************************************
300  * aout_InputPlay : play a buffer
301  *****************************************************************************
302  * This function must be entered with the input lock.
303  *****************************************************************************/
304 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
305                     aout_buffer_t * p_buffer )
306 {
307     mtime_t start_date;
308
309     if( p_input->b_restart )
310     {
311         vlc_mutex_lock( &p_aout->mixer_lock );
312         vlc_mutex_lock( &p_input->lock );
313         aout_InputDelete( p_aout, p_input );
314         aout_InputNew( p_aout, p_input );
315         vlc_mutex_unlock( &p_input->lock );
316         vlc_mutex_unlock( &p_aout->mixer_lock );
317     }
318
319     /* We don't care if someone changes the start date behind our back after
320      * this. We'll deal with that when pushing the buffer, and compensate
321      * with the next incoming buffer. */
322     vlc_mutex_lock( &p_aout->input_fifos_lock );
323     start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
324     vlc_mutex_unlock( &p_aout->input_fifos_lock );
325
326     if ( start_date != 0 && start_date < mdate() )
327     {
328         /* The decoder is _very_ late. This can only happen if the user
329          * pauses the stream (or if the decoder is buggy, which cannot
330          * happen :). */
331         msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
332                   "clearing out", mdate() - start_date );
333         vlc_mutex_lock( &p_aout->input_fifos_lock );
334         aout_FifoSet( p_aout, &p_input->fifo, 0 );
335         p_input->p_first_byte_to_mix = NULL;
336         vlc_mutex_unlock( &p_aout->input_fifos_lock );
337         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
338             msg_Warn( p_aout, "timing screwed, stopping resampling" );
339         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
340         if ( p_input->i_nb_resamplers != 0 )
341         {
342             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
343             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
344         }
345         start_date = 0;
346     }
347
348     if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
349     {
350         /* The decoder gives us f*cked up PTS. It's its business, but we
351          * can't present it anyway, so drop the buffer. */
352         msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
353                   mdate() - p_buffer->start_date );
354         aout_BufferFree( p_buffer );
355         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
356         if ( p_input->i_nb_resamplers != 0 )
357         {
358             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
359             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
360         }
361         return 0;
362     }
363
364     if ( start_date == 0 ) start_date = p_buffer->start_date;
365
366     /* Run pre-filters. */
367
368     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
369                       &p_buffer );
370
371     /* Run the resampler if needed.
372      * We first need to calculate the output rate of this resampler. */
373     if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
374          ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
375            || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
376          p_input->i_nb_resamplers > 0 )
377     {
378         /* Can happen in several circumstances :
379          * 1. A problem at the input (clock drift)
380          * 2. A small pause triggered by the user
381          * 3. Some delay in the output stage, causing a loss of lip
382          *    synchronization
383          * Solution : resample the buffer to avoid a scratch.
384          */
385         mtime_t drift = p_buffer->start_date - start_date;
386
387         p_input->i_resamp_start_date = mdate();
388         p_input->i_resamp_start_drift = (int)drift;
389
390         if ( drift > 0 )
391             p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
392         else
393             p_input->i_resampling_type = AOUT_RESAMPLING_UP;
394
395         msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
396                           drift > 0 ? drift : -drift,
397                           drift > 0 ? "in advance" : "late",
398                           drift > 0 ? "down" : "up");
399     }
400
401     if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
402     {
403         /* Resampling has been triggered previously (because of dates
404          * mismatch). We want the resampling to happen progressively so
405          * it isn't too audible to the listener. */
406
407         if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
408         {
409             p_input->pp_resamplers[0]->input.i_rate += 10; /* Hz */
410         }
411         else
412         {
413             p_input->pp_resamplers[0]->input.i_rate -= 10; /* Hz */
414         }
415
416         /* Check if everything is back to normal, in which case we can stop the
417          * resampling */
418         if( p_input->pp_resamplers[0]->input.i_rate ==
419               p_input->input.i_rate )
420         {
421             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
422             msg_Warn( p_aout, "resampling stopped after "I64Fi" usec",
423                       mdate() - p_input->i_resamp_start_date );
424         }
425         else if( abs( (int)(p_buffer->start_date - start_date) ) <
426                  abs( p_input->i_resamp_start_drift ) / 2 )
427         {
428             /* if we reduced the drift from half, then it is time to switch
429              * back the resampling direction. */
430             if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
431                 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
432             else
433                 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
434             p_input->i_resamp_start_drift = 0;
435         }
436         else if( p_input->i_resamp_start_drift &&
437                  ( abs( (int)(p_buffer->start_date - start_date) ) >
438                    abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
439         {
440             /* If the drift is increasing and not decreasing, than something
441              * is bad. We'd better stop the resampling right now. */
442             msg_Warn( p_aout, "timing screwed, stopping resampling" );
443             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
444             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
445         }
446     }
447
448     /* Adding the start date will be managed by aout_FifoPush(). */
449     p_buffer->end_date = start_date +
450         (p_buffer->end_date - p_buffer->start_date);
451     p_buffer->start_date = start_date;
452
453     /* Actually run the resampler now. */
454     if ( p_input->i_nb_resamplers > 0 )
455     {
456         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
457                           p_input->i_nb_resamplers,
458                           &p_buffer );
459     }
460
461     vlc_mutex_lock( &p_aout->input_fifos_lock );
462     aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
463     vlc_mutex_unlock( &p_aout->input_fifos_lock );
464
465     return 0;
466 }
467
468 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
469                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
470 {
471     aout_instance_t *p_aout = (aout_instance_t *)p_this;
472     char *psz_mode = newval.psz_string;
473     vlc_value_t val;
474     int i;
475
476     if( !psz_mode || !*psz_mode )
477     {
478         val.psz_string = "";
479         var_Set( p_aout, "audio-filter", val );
480     }
481     else
482     {
483         if( !strcmp( "goom", psz_mode ) )
484         {
485             val.psz_string = "goom";
486         }
487         else
488         {
489             val.psz_string = psz_mode;
490             var_Create( p_aout, "effect-list", VLC_VAR_STRING );
491             var_Set( p_aout, "effect-list", val );
492
493             val.psz_string = "visual";
494         }
495
496         var_Set( p_aout, "audio-filter", val );
497     }
498
499     /* That sucks */
500     for( i = 0; i < p_aout->i_nb_inputs; i++ )
501     {
502         p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
503     }
504
505     return VLC_SUCCESS;
506 }
507
508 static aout_filter_t * allocateUserChannelMixer( aout_instance_t * p_aout,
509                                      audio_sample_format_t * p_input_format,
510                                      audio_sample_format_t * p_output_format )
511 {
512     aout_filter_t * p_channel_mixer;
513
514     /* Retreive user preferred channel mixer */
515     char * psz_name = config_GetPsz( p_aout, "audio-channel-mixer" );
516
517     /* Not specified => let the main pipeline do the mixing */
518     if ( ! psz_name ) return NULL;
519
520     /* Debug information */
521     aout_FormatsPrint( p_aout, "channel mixer", p_input_format,
522                        p_output_format );
523
524     /* Create a VLC object */
525     p_channel_mixer = vlc_object_create( p_aout, sizeof(aout_filter_t) );
526     if( p_channel_mixer == NULL )
527     {
528         msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
529         return NULL;
530     }
531     vlc_object_attach( p_channel_mixer , p_aout );
532
533     /* Attach the suitable module */
534     memcpy( &p_channel_mixer->input, p_input_format,
535                     sizeof(audio_sample_format_t) );
536     memcpy( &p_channel_mixer->output, p_output_format,
537                     sizeof(audio_sample_format_t) );
538     p_channel_mixer->p_module =
539         module_Need( p_channel_mixer,"audio filter", psz_name, VLC_TRUE );
540     if( p_channel_mixer->p_module== NULL )
541     {
542         msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
543         vlc_object_detach( p_channel_mixer );
544         vlc_object_destroy( p_channel_mixer );
545         return NULL;
546     }
547     p_channel_mixer->b_continuity = VLC_FALSE;
548
549     /* Ok */
550     return p_channel_mixer;
551 }