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.13 2002/09/26 22:40:25 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 )
45 aout_FifoInit( p_aout, &p_input->fifo, p_aout->mixer.mixer.i_rate );
46 p_input->p_first_byte_to_mix = NULL;
49 if ( aout_FiltersCreatePipeline( p_aout, p_input->pp_filters,
50 &p_input->i_nb_filters, &p_input->input,
51 &p_aout->mixer.mixer ) < 0 )
53 msg_Err( p_aout, "couldn't set an input pipeline" );
55 aout_FifoDestroy( p_aout, &p_input->fifo );
61 /* Prepare hints for the buffer allocator. */
62 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
63 p_input->input_alloc.i_bytes_per_sec = -1;
65 aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
66 p_input->i_nb_filters,
67 &p_input->input_alloc );
69 /* i_bytes_per_sec is still == -1 if no filters */
70 p_input->input_alloc.i_bytes_per_sec = __MAX(
71 p_input->input_alloc.i_bytes_per_sec,
72 p_input->input.i_bytes_per_frame
73 * p_input->input.i_rate
74 / p_input->input.i_frame_length );
75 /* Allocate in the heap, it is more convenient for the decoder. */
76 p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
83 /*****************************************************************************
84 * aout_InputDelete : delete an input
85 *****************************************************************************
86 * This function must be entered with the mixer lock.
87 *****************************************************************************/
88 int aout_InputDelete( aout_instance_t * p_aout, aout_input_t * p_input )
90 if ( p_input->b_error ) return 0;
92 aout_FiltersDestroyPipeline( p_aout, p_input->pp_filters,
93 p_input->i_nb_filters );
94 aout_FifoDestroy( p_aout, &p_input->fifo );
99 /*****************************************************************************
100 * aout_InputPlay : play a buffer
101 *****************************************************************************
102 * This function must be entered with the input lock.
103 *****************************************************************************/
104 int aout_InputPlay( aout_instance_t * p_aout, aout_input_t * p_input,
105 aout_buffer_t * p_buffer )
107 mtime_t start_date, duration;
109 /* We don't care if someone changes the start date behind our back after
110 * this. We'll deal with that when pushing the buffer, and compensate
111 * with the next incoming buffer. */
112 vlc_mutex_lock( &p_aout->input_fifos_lock );
113 start_date = aout_FifoNextStart( p_aout, &p_input->fifo );
114 vlc_mutex_unlock( &p_aout->input_fifos_lock );
116 if ( start_date != 0 && start_date < mdate() )
118 /* The decoder is _very_ late. This can only happen if the user
119 * pauses the stream (or if the decoder is buggy, which cannot
121 msg_Warn( p_aout, "computed PTS is out of range (%lld), clearing out",
123 vlc_mutex_lock( &p_aout->mixer_lock );
124 aout_FifoSet( p_aout, &p_input->fifo, 0 );
125 vlc_mutex_unlock( &p_aout->mixer_lock );
129 if ( p_buffer->start_date < mdate() + AOUT_MIN_PREPARE_TIME )
131 /* The decoder gives us f*cked up PTS. It's its business, but we
132 * can't present it anyway, so drop the buffer. */
133 msg_Warn( p_aout, "PTS is out of range (%lld), dropping buffer",
134 mdate() - p_buffer->start_date );
135 aout_BufferFree( p_buffer );
137 vlc_mutex_unlock( &p_input->lock );
141 if ( start_date == 0 ) start_date = p_buffer->start_date;
143 if ( start_date < p_buffer->start_date - AOUT_PTS_TOLERANCE
144 || start_date > p_buffer->start_date + AOUT_PTS_TOLERANCE )
146 /* Can happen in several circumstances :
147 * 1. A problem at the input (clock drift)
148 * 2. A small pause triggered by the user
149 * 3. Some delay in the output stage, causing a loss of lip
151 * Solution : resample the buffer to avoid a scratch.
153 audio_sample_format_t new_input;
154 int i_ratio, i_nb_filters;
155 mtime_t old_duration;
156 aout_filter_t * pp_filters[AOUT_MAX_FILTERS];
157 aout_buffer_t * p_new_buffer;
158 aout_alloc_t dummy_alloc;
159 mtime_t drift = p_buffer->start_date - start_date;
161 msg_Warn( p_aout, "buffer is %lld %s, resampling",
162 drift > 0 ? drift : -drift,
163 drift > 0 ? "in advance" : "late" );
164 memcpy( &new_input, &p_input->input,
165 sizeof(audio_sample_format_t) );
166 old_duration = p_buffer->end_date - p_buffer->start_date;
167 duration = p_buffer->end_date - start_date;
168 i_ratio = duration * 100 / old_duration;
169 /* If the ratio is too != 100, the sound quality will be awful. */
170 if ( i_ratio < 66 /* % */ )
172 duration = old_duration * 66 / 100;
174 if ( i_ratio > 150 /* % */ )
176 duration = old_duration * 150 / 100;
178 new_input.i_rate = new_input.i_rate * old_duration / duration;
179 aout_FormatPrepare( &new_input );
181 if ( aout_FiltersCreatePipeline( p_aout, pp_filters,
182 &i_nb_filters, &new_input,
183 &p_aout->mixer.mixer ) < 0 )
185 msg_Err( p_aout, "couldn't set an input pipeline for resampling" );
186 vlc_mutex_lock( &p_aout->mixer_lock );
187 aout_FifoSet( p_aout, &p_input->fifo, 0 );
188 vlc_mutex_unlock( &p_aout->mixer_lock );
189 aout_BufferFree( p_buffer );
191 vlc_mutex_unlock( &p_input->lock );
195 dummy_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
196 dummy_alloc.i_bytes_per_sec = -1;
197 aout_FiltersHintBuffers( p_aout, pp_filters, i_nb_filters,
199 dummy_alloc.i_bytes_per_sec = __MAX(
200 dummy_alloc.i_bytes_per_sec,
201 new_input.i_bytes_per_frame
203 / new_input.i_frame_length );
204 dummy_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
206 aout_BufferAlloc( &dummy_alloc, duration, NULL, p_new_buffer );
207 memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
208 p_buffer->i_nb_bytes );
209 p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
210 p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
212 aout_BufferFree( p_buffer );
213 p_buffer = p_new_buffer;
215 aout_FiltersPlay( p_aout, pp_filters, i_nb_filters,
218 aout_FiltersDestroyPipeline( p_aout, pp_filters,
223 /* No resampling needed (except maybe the one imposed by the
225 duration = p_buffer->end_date - p_buffer->start_date;
226 aout_FiltersPlay( p_aout, p_input->pp_filters, p_input->i_nb_filters,
230 /* Adding the start date will be managed by aout_FifoPush(). */
231 p_buffer->start_date = start_date;
232 p_buffer->end_date = start_date + duration;
234 vlc_mutex_lock( &p_aout->input_fifos_lock );
235 aout_FifoPush( p_aout, &p_input->fifo, p_buffer );
236 vlc_mutex_unlock( &p_aout->input_fifos_lock );