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