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 $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* calloc(), malloc(), free() */
36 #include "audio_output.h"
37 #include "aout_internal.h"
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 )
44 audio_sample_format_t intermediate_format;
47 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
48 p_input->p_first_byte_to_mix = NULL;
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 )
58 msg_Err( p_aout, "couldn't set an input pipeline" );
60 aout_FifoDestroy( p_aout, &p_input->fifo );
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 )
72 intermediate_format.i_rate++;
74 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_resamplers,
75 &p_input->i_nb_resamplers,
77 &p_aout->mixer.mixer ) < 0 )
79 msg_Err( p_aout, "couldn't set a resampler pipeline" );
81 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
82 p_input->i_nb_filters );
83 aout_FifoDestroy( p_aout, &p_input->fifo );
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;
93 aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
94 p_input->i_nb_resamplers,
95 &p_input->input_alloc );
97 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
98 p_input->i_nb_filters,
99 &p_input->input_alloc );
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;
110 p_input->b_error = 0;
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 )
122 if ( p_input->b_error ) return 0;
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 );
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 )
141 mtime_t start_date, duration;
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 );
150 if ( start_date != 0 && start_date < mdate() )
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
155 msg_Warn( p_aout, "computed PTS is out of range (%lld), clearing out",
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 );
163 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
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 );
171 vlc_mutex_unlock( &p_input->lock );
175 if ( start_date == 0 ) start_date = p_buffer->start_date;
177 /* Run pre-filters. */
178 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
181 if ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
182 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE )
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
189 * Solution : resample the buffer to avoid a scratch.
192 mtime_t old_duration;
193 mtime_t drift = p_buffer->start_date - start_date;
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 /* % */ )
204 duration = (old_duration * (100 - AOUT_MAX_RESAMPLING)) / 100;
206 if ( i_ratio > 100 + AOUT_MAX_RESAMPLING /* % */ )
208 duration = (old_duration * (100 + AOUT_MAX_RESAMPLING)) / 100;
210 p_input->pp_resamplers[0]->input.i_rate
211 = (p_input->input.i_rate * old_duration) / duration;
213 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
214 p_input->i_nb_resamplers,
219 duration = p_buffer->end_date - p_buffer->start_date;
221 if ( p_input->input.i_rate != p_aout->mixer.mixer.i_rate )
223 /* Standard resampling is needed ! */
224 p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
226 aout_FiltersPlay( p_aout, p_input->pp_resamplers,
227 p_input->i_nb_resamplers,
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;
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 );