]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
* src/audio_output/input.c: add goom (if present) to the list of visualization choices.
[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 = 0;
276
277     return 0;
278 }
279
280 /*****************************************************************************
281  * aout_InputDelete : delete an input
282  *****************************************************************************
283  * This function must be entered with the mixer lock.
284  *****************************************************************************/
285 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
286 {
287     if ( p_input->b_error ) return 0;
288
289     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
290                                  p_input->i_nb_filters );
291     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
292                                  p_input->i_nb_resamplers );
293     aout_FifoDestroy( p_aout, &p_input->fifo );
294
295     return 0;
296 }
297
298 /*****************************************************************************
299  * aout_InputPlay : play a buffer
300  *****************************************************************************
301  * This function must be entered with the input lock.
302  *****************************************************************************/
303 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
304                     aout_buffer_t * p_buffer )
305 {
306     mtime_t start_date;
307
308     /* We don't care if someone changes the start date behind our back after
309      * this. We'll deal with that when pushing the buffer, and compensate
310      * with the next incoming buffer. */
311     vlc_mutex_lock( &p_aout->input_fifos_lock );
312     start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
313     vlc_mutex_unlock( &p_aout->input_fifos_lock );
314
315     if ( start_date != 0 && start_date < mdate() )
316     {
317         /* The decoder is _very_ late. This can only happen if the user
318          * pauses the stream (or if the decoder is buggy, which cannot
319          * happen :). */
320         msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
321                   "clearing out", mdate() - start_date );
322         vlc_mutex_lock( &p_aout->input_fifos_lock );
323         aout_FifoSet( p_aout, &p_input->fifo, 0 );
324         p_input->p_first_byte_to_mix = NULL;
325         vlc_mutex_unlock( &p_aout->input_fifos_lock );
326         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
327             msg_Warn( p_aout, "timing screwed, stopping resampling" );
328         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
329         if ( p_input->i_nb_resamplers != 0 )
330         {
331             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
332             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
333         }
334         start_date = 0;
335     }
336
337     if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
338     {
339         /* The decoder gives us f*cked up PTS. It's its business, but we
340          * can't present it anyway, so drop the buffer. */
341         msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
342                   mdate() - p_buffer->start_date );
343         aout_BufferFree( p_buffer );
344         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
345         if ( p_input->i_nb_resamplers != 0 )
346         {
347             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
348             p_input->pp_resamplers[0]->b_continuity = VLC_FALSE;
349         }
350         return 0;
351     }
352
353     if ( start_date == 0 ) start_date = p_buffer->start_date;
354
355     /* Run pre-filters. */
356
357     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
358                       &p_buffer );
359
360     /* Run the resampler if needed.
361      * We first need to calculate the output rate of this resampler. */
362     if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
363          ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
364            || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
365          p_input->i_nb_resamplers > 0 )
366     {
367         /* Can happen in several circumstances :
368          * 1. A problem at the input (clock drift)
369          * 2. A small pause triggered by the user
370          * 3. Some delay in the output stage, causing a loss of lip
371          *    synchronization
372          * Solution : resample the buffer to avoid a scratch.
373          */
374         mtime_t drift = p_buffer->start_date - start_date;
375
376         p_input->i_resamp_start_date = mdate();
377         p_input->i_resamp_start_drift = (int)drift;
378
379         if ( drift > 0 )
380             p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
381         else
382             p_input->i_resampling_type = AOUT_RESAMPLING_UP;
383
384         msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
385                           drift > 0 ? drift : -drift,
386                           drift > 0 ? "in advance" : "late",
387                           drift > 0 ? "down" : "up");
388     }
389
390     if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
391     {
392         /* Resampling has been triggered previously (because of dates
393          * mismatch). We want the resampling to happen progressively so
394          * it isn't too audible to the listener. */
395
396         if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
397         {
398             p_input->pp_resamplers[0]->input.i_rate += 10; /* Hz */
399         }
400         else
401         {
402             p_input->pp_resamplers[0]->input.i_rate -= 10; /* Hz */
403         }
404
405         /* Check if everything is back to normal, in which case we can stop the
406          * resampling */
407         if( p_input->pp_resamplers[0]->input.i_rate ==
408               p_input->input.i_rate )
409         {
410             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
411             msg_Warn( p_aout, "resampling stopped after "I64Fi" usec",
412                       mdate() - p_input->i_resamp_start_date );
413         }
414         else if( abs( (int)(p_buffer->start_date - start_date) ) <
415                  abs( p_input->i_resamp_start_drift ) / 2 )
416         {
417             /* if we reduced the drift from half, then it is time to switch
418              * back the resampling direction. */
419             if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
420                 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
421             else
422                 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
423             p_input->i_resamp_start_drift = 0;
424         }
425         else if( p_input->i_resamp_start_drift &&
426                  ( abs( (int)(p_buffer->start_date - start_date) ) >
427                    abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
428         {
429             /* If the drift is increasing and not decreasing, than something
430              * is bad. We'd better stop the resampling right now. */
431             msg_Warn( p_aout, "timing screwed, stopping resampling" );
432             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
433             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
434         }
435     }
436
437     /* Adding the start date will be managed by aout_FifoPush(). */
438     p_buffer->end_date = start_date +
439         (p_buffer->end_date - p_buffer->start_date);
440     p_buffer->start_date = start_date;
441
442     /* Actually run the resampler now. */
443     if ( p_input->i_nb_resamplers > 0 )
444     {
445         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
446                           p_input->i_nb_resamplers,
447                           &p_buffer );
448     }
449
450     vlc_mutex_lock( &p_aout->input_fifos_lock );
451     aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
452     vlc_mutex_unlock( &p_aout->input_fifos_lock );
453
454     return 0;
455 }
456
457 static int VisualizationCallback( vlc_object_t *p_this, char const *psz_cmd,
458                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
459 {
460     aout_instance_t *p_aout = (aout_instance_t *)p_this;
461     vlc_value_t val;
462     char *psz_mode = newval.psz_string;
463     char *psz_filter;
464
465     var_Get( p_aout, "audio-filter", &val );
466     psz_filter = val.psz_string;
467
468     if( !psz_mode || !*psz_mode )
469     {
470         val.psz_string = "";
471         var_Set( p_aout, "audio-filter", val );
472     }
473     else
474     {
475         if( !psz_filter || !*psz_filter )
476         {
477             if( !strcmp( "goom", psz_mode) )
478                 val.psz_string = "goom";
479             else
480                 val.psz_string = "visual";
481             var_Set( p_aout, "audio-filter", val );
482         }
483         else
484         {
485             if( strstr( psz_filter, "visual" ) == NULL )
486             {
487                 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
488                 strcat( psz_filter, ",visual" );
489             }
490             val.psz_string = psz_filter;
491             var_Set( p_aout, "audio-filter", val );
492         }
493     }
494
495     if( psz_mode && *psz_mode )
496     {
497         vlc_value_t val;
498         val.psz_string = psz_mode;
499         var_Create( p_aout, "effect-list", VLC_VAR_STRING );
500         var_Set( p_aout, "effect-list", val);
501     }
502
503     if( psz_filter ) free( psz_filter );
504
505     aout_Restart( p_aout );
506
507     return VLC_SUCCESS;
508 }
509
510 static aout_filter_t * allocateUserChannelMixer( aout_instance_t * p_aout,
511                                      audio_sample_format_t * p_input_format,
512                                      audio_sample_format_t * p_output_format )
513 {
514     aout_filter_t * p_channel_mixer;
515
516     /* Retreive user preferred channel mixer */
517     char * psz_name = config_GetPsz( p_aout, "audio-channel-mixer" );
518
519     /* Not specified => let the main pipeline do the mixing */
520     if ( ! psz_name ) return NULL;
521
522     /* Debug information */
523     aout_FormatsPrint( p_aout, "channel mixer", p_input_format,
524                        p_output_format );
525
526     /* Create a VLC object */
527     p_channel_mixer = vlc_object_create( p_aout, sizeof(aout_filter_t) );
528     if( p_channel_mixer == NULL )
529     {
530         msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
531         return NULL;
532     }
533     vlc_object_attach( p_channel_mixer , p_aout );
534
535     /* Attach the suitable module */
536     memcpy( &p_channel_mixer->input, p_input_format,
537                     sizeof(audio_sample_format_t) );
538     memcpy( &p_channel_mixer->output, p_output_format,
539                     sizeof(audio_sample_format_t) );
540     p_channel_mixer->p_module =
541         module_Need( p_channel_mixer,"audio filter", psz_name, VLC_TRUE );
542     if( p_channel_mixer->p_module== NULL )
543     {
544         msg_Err( p_aout, "cannot add user channel mixer %s", psz_name );
545         vlc_object_detach( p_channel_mixer );
546         vlc_object_destroy( p_channel_mixer );
547         return NULL;
548     }
549     p_channel_mixer->b_continuity = VLC_FALSE;
550
551     /* Ok */
552     return p_channel_mixer;
553 }