]> git.sesse.net Git - vlc/blob - src/audio_output/input.c
c44266ccf92714ef28af973189d029469530dfb1
[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.26 2002/12/06 10:10:39 sam 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     aout_FormatPrint( p_aout, "input", &p_input->input );
47
48     /* Prepare FIFO. */
49     aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
50     p_input->p_first_byte_to_mix = NULL;
51
52     /* Create filters. */
53     memcpy( &intermediate_format, &p_aout->mixer.mixer,
54             sizeof(audio_sample_format_t) );
55     intermediate_format.i_rate = p_input->input.i_rate;
56     if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
57                                      &p_input->i_nb_filters, &p_input->input,
58                                      &intermediate_format ) < 0 )
59     {
60         msg_Err( p_aout, "couldn't set an input pipeline" );
61
62         aout_FifoDestroy( p_aout, &p_input->fifo );
63         p_input->b_error = 1;
64
65         return -1;
66     }
67
68     /* Prepare hints for the buffer allocator. */
69     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
70     p_input->input_alloc.i_bytes_per_sec = -1;
71
72     if ( AOUT_FMT_NON_LINEAR( &p_aout->mixer.mixer ) )
73     {
74         p_input->i_nb_resamplers = 0;
75     }
76     else
77     {
78         /* Create resamplers. */
79         intermediate_format.i_rate = (p_input->input.i_rate
80                                  * (100 + AOUT_MAX_RESAMPLING)) / 100;
81         if ( intermediate_format.i_rate == p_aout->mixer.mixer.i_rate )
82         {
83             /* Just in case... */
84             intermediate_format.i_rate++;
85         }
86         if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
87                                          &p_input->i_nb_resamplers,
88                                          &intermediate_format,
89                                          &p_aout->mixer.mixer ) < 0 )
90         {
91             msg_Err( p_aout, "couldn't set a resampler pipeline" );
92
93             aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
94                                          p_input->i_nb_filters );
95             aout_FifoDestroy( p_aout, &p_input->fifo );
96             p_input->b_error = 1;
97
98             return -1;
99         }
100
101         aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
102                                  p_input->i_nb_resamplers,
103                                  &p_input->input_alloc );
104
105         /* Setup the initial rate of the resampler */
106         p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
107     }
108     p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
109
110     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
111     p_input->input_alloc.i_bytes_per_sec = -1;
112     aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
113                              p_input->i_nb_filters,
114                              &p_input->input_alloc );
115
116     /* i_bytes_per_sec is still == -1 if no filters */
117     p_input->input_alloc.i_bytes_per_sec = __MAX(
118                                     p_input->input_alloc.i_bytes_per_sec,
119                                     (int)(p_input->input.i_bytes_per_frame
120                                      * p_input->input.i_rate
121                                      / p_input->input.i_frame_length) );
122     /* Allocate in the heap, it is more convenient for the decoder. */
123     p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
124
125     p_input->b_error = 0;
126
127     return 0;
128 }
129
130 /*****************************************************************************
131  * aout_InputDelete : delete an input
132  *****************************************************************************
133  * This function must be entered with the mixer lock.
134  *****************************************************************************/
135 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
136 {
137     if ( p_input->b_error ) return 0;
138
139     aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
140                                  p_input->i_nb_filters );
141     aout_FiltersDestroyPipeline( p_aout, p_input->pp_resamplers,
142                                  p_input->i_nb_resamplers );
143     aout_FifoDestroy( p_aout, &p_input->fifo );
144
145     return 0;
146 }
147
148 /*****************************************************************************
149  * aout_InputPlay : play a buffer
150  *****************************************************************************
151  * This function must be entered with the input lock.
152  *****************************************************************************/
153 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
154                     aout_buffer_t * p_buffer )
155 {
156     mtime_t start_date;
157
158     /* We don't care if someone changes the start date behind our back after
159      * this. We'll deal with that when pushing the buffer, and compensate
160      * with the next incoming buffer. */
161     vlc_mutex_lock( &p_aout->input_fifos_lock );
162     start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
163     vlc_mutex_unlock( &p_aout->input_fifos_lock );
164
165     if ( start_date != 0 && start_date < mdate() )
166     {
167         /* The decoder is _very_ late. This can only happen if the user
168          * pauses the stream (or if the decoder is buggy, which cannot
169          * happen :). */
170         msg_Warn( p_aout, "computed PTS is out of range ("I64Fd"), "
171                   "clearing out", mdate() - start_date );
172         vlc_mutex_lock( &p_aout->input_fifos_lock );
173         aout_FifoSet( p_aout, &p_input->fifo, 0 );
174         vlc_mutex_unlock( &p_aout->input_fifos_lock );
175         if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
176             msg_Warn( p_aout, "timing screwed, stopping resampling" );
177         p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
178         if ( p_input->i_nb_resamplers != 0 )
179         {
180             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
181             p_input->pp_resamplers[0]->b_reinit = VLC_TRUE;
182         }
183         start_date = 0;
184     }
185
186     if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
187     {
188         /* The decoder gives us f*cked up PTS. It's its business, but we
189          * can't present it anyway, so drop the buffer. */
190         msg_Warn( p_aout, "PTS is out of range ("I64Fd"), dropping buffer",
191                   mdate() - p_buffer->start_date );
192         aout_BufferFree( p_buffer );
193
194         return 0;
195     }
196
197     if ( start_date == 0 ) start_date = p_buffer->start_date;
198
199     /* Run pre-filters. */
200     aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
201                       &p_buffer );
202
203     /* Run the resampler if needed.
204      * We first need to calculate the output rate of this resampler. */
205     if ( ( p_input->i_resampling_type == AOUT_RESAMPLING_NONE ) &&
206          ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
207            || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE ) &&
208          p_input->i_nb_resamplers > 0 )
209     {
210         /* Can happen in several circumstances :
211          * 1. A problem at the input (clock drift)
212          * 2. A small pause triggered by the user
213          * 3. Some delay in the output stage, causing a loss of lip
214          *    synchronization
215          * Solution : resample the buffer to avoid a scratch.
216          */
217         mtime_t drift = p_buffer->start_date - start_date;
218
219         p_input->i_resamp_start_date = mdate();
220         p_input->i_resamp_start_drift = (int)drift;
221
222         if ( drift > 0 )
223             p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
224         else
225             p_input->i_resampling_type = AOUT_RESAMPLING_UP;
226
227         msg_Warn( p_aout, "buffer is "I64Fd" %s, triggering %ssampling",
228                           drift > 0 ? drift : -drift,
229                           drift > 0 ? "in advance" : "late",
230                           drift > 0 ? "down" : "up");
231     }
232
233     if ( p_input->i_resampling_type != AOUT_RESAMPLING_NONE )
234     {
235         /* Resampling has been triggered previously (because of dates
236          * mismatch). We want the resampling to happen progressively so
237          * it isn't too audible to the listener. */
238
239         if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
240         {
241             p_input->pp_resamplers[0]->input.i_rate += 10; /* Hz */
242         }
243         else
244         {
245             p_input->pp_resamplers[0]->input.i_rate -= 10; /* Hz */
246         }
247
248         /* Check if everything is back to normal, in which case we can stop the
249          * resampling */
250         if( p_input->pp_resamplers[0]->input.i_rate ==
251               p_input->input.i_rate )
252         {
253             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
254             msg_Warn( p_aout, "resampling stopped after "I64Fi" usec",
255                       mdate() - p_input->i_resamp_start_date );
256         }
257         else if( abs( (int)(p_buffer->start_date - start_date) ) <
258                  abs( p_input->i_resamp_start_drift ) / 2 )
259         {
260             /* if we reduced the drift from half, then it is time to switch
261              * back the resampling direction. */
262             if( p_input->i_resampling_type == AOUT_RESAMPLING_UP )
263                 p_input->i_resampling_type = AOUT_RESAMPLING_DOWN;
264             else
265                 p_input->i_resampling_type = AOUT_RESAMPLING_UP;
266             p_input->i_resamp_start_drift = 0;
267         }
268         else if( p_input->i_resamp_start_drift &&
269                  ( abs( (int)(p_buffer->start_date - start_date) ) >
270                    abs( p_input->i_resamp_start_drift ) * 3 / 2 ) )
271         {
272             /* If the drift is increasing and not decreasing, than something
273              * is bad. We'd better stop the resampling right now. */
274             msg_Warn( p_aout, "timing screwed, stopping resampling" );
275             p_input->i_resampling_type = AOUT_RESAMPLING_NONE;
276             p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
277         }
278
279     }
280
281     /* Adding the start date will be managed by aout_FifoPush(). */
282     p_buffer->start_date = start_date;
283     p_buffer->end_date = start_date +
284         (p_buffer->end_date - p_buffer->start_date);
285
286     /* Actually run the resampler now. */
287     if ( p_input->i_nb_resamplers > 0 && p_aout->mixer.mixer.i_rate !=
288          p_input->pp_resamplers[0]->input.i_rate )
289     {
290         aout_FiltersPlay( p_aout, p_input->pp_resamplers,
291                           p_input->i_nb_resamplers,
292                           &p_buffer );
293     }
294
295     vlc_mutex_lock( &p_aout->input_fifos_lock );
296     aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
297     vlc_mutex_unlock( &p_aout->input_fifos_lock );
298
299     return 0;
300 }