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