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