]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
* Makefile.old: Fixed BeOS compilation.
[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.14 2002/09/27 23:38:04 massiot 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 /*****************************************************************************
40  * aout_InputNew : allocate a new input and rework the filter pipeline
41  *****************************************************************************/
42 int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input )
43 {
44     audio_sample_format_t intermediate_format;
45
46     /* Prepare FIFO. */
47     aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
48     p_input->p_first_byte_to_mix = NULL;
49
50     /* Create filters. */
51     memcpy( &intermediate_format, &p_aout->mixer.mixer,
52             sizeof(audio_sample_format_t) );
53     intermediate_format.i_rate = p_input->input.i_rate;
54     if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
55                                      &p_input->i_nb_filters, &p_input->input,
56                                      &intermediate_format ) < 0 )
57     {
58         msg_Err( p_aout, "couldn't set an input pipeline" );
59
60         aout_FifoDestroy( p_aout, &p_input->fifo );
61         p_input->b_error = 1;
62
63         return -1;
64     }
65
66     /* Create resamplers. */
67     intermediate_format.i_rate = (p_input->input.i_rate
68                              * (100 + AOUT_MAX_RESAMPLING)) / 100;
69     if ( intermediate_format.i_rate == p_aout->mixer.mixer.i_rate )
70     {
71         /* Just in case... */
72         intermediate_format.i_rate++;
73     }
74     if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
75                                      &p_input->i_nb_resamplers,
76                                      &intermediate_format,
77                                      &p_aout->mixer.mixer ) < 0 )
78     {
79         msg_Err( p_aout, "couldn't set a resampler pipeline" );
80
81         aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
82                                      p_input->i_nb_filters );
83         aout_FifoDestroy( p_aout, &p_input->fifo );
84         p_input->b_error = 1;
85
86         return -1;
87     }
88
89     /* Prepare hints for the buffer allocator. */
90     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
91     p_input->input_alloc.i_bytes_per_sec = -1;
92
93     aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
94                              p_input->i_nb_resamplers,
95                              &p_input->input_alloc );
96
97     aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
98                              p_input->i_nb_filters,
99                              &p_input->input_alloc );
100
101     /* i_bytes_per_sec is still == -1 if no filters */
102     p_input->input_alloc.i_bytes_per_sec = __MAX(
103                                     p_input->input_alloc.i_bytes_per_sec,
104                                     p_input->input.i_bytes_per_frame
105                                      * p_input->input.i_rate
106                                      / p_input->input.i_frame_length );
107     /* Allocate in the heap, it is more convenient for the decoder. */
108     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
109
110     p_input->b_error = 0;
111
112     return 0;
113 }
114
115 /*****************************************************************************
116  * aout_InputDelete : delete an input
117  *****************************************************************************
118  * This function must be entered with the mixer lock.
119  *****************************************************************************/
120 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
121 {
122     if ( p_input->b_error ) return 0;
123
124     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
125                                  p_input->i_nb_filters );
126     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
127                                  p_input->i_nb_resamplers );
128     aout_FifoDestroy( p_aout, &p_input->fifo );
129
130     return 0;
131 }
132
133 /*****************************************************************************
134  * aout_InputPlay : play a buffer
135  *****************************************************************************
136  * This function must be entered with the input lock.
137  *****************************************************************************/
138 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
139                     aout_buffer_t * p_buffer )
140 {
141     mtime_t start_date, duration;
142
143     /* We don't care if someone changes the start date behind our back after
144      * this. We'll deal with that when pushing the buffer, and compensate
145      * with the next incoming buffer. */
146     vlc_mutex_lock( &p_aout->input_fifos_lock );
147     start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
148     vlc_mutex_unlock( &p_aout->input_fifos_lock );
149
150     if ( start_date != 0 && start_date < mdate() )
151     {
152         /* The decoder is _very_ late. This can only happen if the user
153          * pauses the stream (or if the decoder is buggy, which cannot
154          * happen :). */
155         msg_Warn( p_aout, "computed PTS is out of range (%lld), clearing out",
156                   start_date );
157         vlc_mutex_lock( &p_aout->mixer_lock );
158         aout_FifoSet( p_aout, &p_input->fifo, 0 );
159         vlc_mutex_unlock( &p_aout->mixer_lock );
160         start_date = 0;
161     } 
162
163     if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
164     {
165         /* The decoder gives us f*cked up PTS. It's its business, but we
166          * can't present it anyway, so drop the buffer. */
167         msg_Warn( p_aout, "PTS is out of range (%lld), dropping buffer",
168                   mdate() - p_buffer->start_date );
169         aout_BufferFree( p_buffer );
170
171         vlc_mutex_unlock( &p_input->lock );
172         return 0;
173     }
174
175     if ( start_date == 0 ) start_date = p_buffer->start_date;
176
177     /* Run pre-filters. */
178     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
179                       &p_buffer );
180
181     if ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
182           || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE )
183     {
184         /* Can happen in several circumstances :
185          * 1. A problem at the input (clock drift)
186          * 2. A small pause triggered by the user
187          * 3. Some delay in the output stage, causing a loss of lip
188          *    synchronization
189          * Solution : resample the buffer to avoid a scratch.
190          */
191         int i_ratio;
192         mtime_t old_duration;
193         mtime_t drift = p_buffer->start_date - start_date;
194
195         msg_Warn( p_aout, "buffer is %lld %s, resampling",
196                          drift > 0 ? drift : -drift,
197                          drift > 0 ? "in advance" : "late" );
198         old_duration = p_buffer->end_date - p_buffer->start_date;
199         duration = p_buffer->end_date - start_date;
200         i_ratio = (duration * 100) / old_duration;
201         /* If the ratio is too != 100, the sound quality will be awful. */
202         if ( i_ratio < 100 - AOUT_MAX_RESAMPLING /* % */ )
203         {
204             duration = (old_duration * (100 - AOUT_MAX_RESAMPLING)) / 100;
205         }
206         if ( i_ratio > 100 + AOUT_MAX_RESAMPLING /* % */ )
207         {
208             duration = (old_duration * (100 + AOUT_MAX_RESAMPLING)) / 100;
209         }
210         p_input->pp_resamplers[0]->input.i_rate 
211             = (p_input->input.i_rate * old_duration) / duration;
212
213         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
214                           p_input->i_nb_resamplers,
215                           &p_buffer );
216     }
217     else
218     {
219         duration = p_buffer->end_date - p_buffer->start_date;
220
221         if ( p_input->input.i_rate != p_aout->mixer.mixer.i_rate )
222         {
223             /* Standard resampling is needed ! */
224             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
225
226             aout_FiltersPlay( p_aout, p_input->pp_resamplers,
227                               p_input->i_nb_resamplers,
228                               &p_buffer );
229         }
230     }
231
232     /* Adding the start date will be managed by aout_FifoPush(). */
233     p_buffer->start_date = start_date;
234     p_buffer->end_date = start_date + duration;
235
236     vlc_mutex_lock( &p_aout->input_fifos_lock );
237     aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
238     vlc_mutex_unlock( &p_aout->input_fifos_lock );
239
240     return 0;
241 }