1 /*****************************************************************************
2 * input_clock.c: Clock/System date convertions, stream management
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: input_clock.c,v 1.5 2001/02/08 13:08:03 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 *****************************************************************************/
35 #include "stream_control.h"
36 #include "input_ext-intf.h"
37 #include "input_ext-dec.h"
42 * DISCUSSION : SYNCHRONIZATION METHOD
44 * In some cases we can impose the pace of reading (when reading from a
45 * file or a pipe), and for the synchronization we simply sleep() until
46 * it is time to deliver the packet to the decoders. When reading from
47 * the network, we must be read at the same pace as the server writes,
48 * otherwise the kernel's buffer will trash packets. The risk is now to
49 * overflow the input buffers in case the server goes too fast, that is
50 * why we do these calculations :
52 * We compute a mean for the pcr because we want to eliminate the
53 * network jitter and keep the low frequency variations. The mean is
54 * in fact a low pass filter and the jitter is a high frequency signal
55 * that is why it is eliminated by the filter/average.
57 * The low frequency variations enable us to synchronize the client clock
58 * with the server clock because they represent the time variation between
59 * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
60 * the presentation dates for the audio and video frames. With those dates
61 * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
62 * as it is sent by the server and so we keep the synchronization between
63 * the server and the client.
65 * It is a very important matter if you want to avoid underflow or overflow
66 * in all the FIFOs, but it may be not enough.
69 /*****************************************************************************
71 *****************************************************************************/
73 /* Maximum number of samples used to compute the dynamic average value.
74 * We use the following formula :
75 * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
77 #define CR_MAX_AVERAGE_COUNTER 40
79 /* Maximum gap allowed between two CRs. */
80 #define CR_MAX_GAP 1000000
82 /*****************************************************************************
83 * ClockToSysdate: converts a movie clock to system date
84 *****************************************************************************/
85 static mtime_t ClockToSysdate( input_thread_t * p_input,
86 pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
88 mtime_t i_sysdate = 0;
90 if( p_pgrm->i_synchro_state == SYNCHRO_OK )
92 i_sysdate = (i_clock - p_pgrm->cr_ref)
93 * p_input->stream.control.i_rate
97 + p_pgrm->sysdate_ref;
103 /*****************************************************************************
104 * ClockCurrent: converts current system date to clock units
105 *****************************************************************************
106 * Caution : the synchro state must be SYNCHRO_OK for this to operate.
107 *****************************************************************************/
108 static mtime_t ClockCurrent( input_thread_t * p_input,
109 pgrm_descriptor_t * p_pgrm )
111 return( (mdate() - p_pgrm->sysdate_ref) * 27 * DEFAULT_RATE
112 / p_input->stream.control.i_rate / 300
116 /*****************************************************************************
117 * input_ClockNewRef: writes a new clock reference
118 *****************************************************************************/
119 void input_ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
120 mtime_t i_clock, mtime_t i_sysdate )
122 intf_WarnMsg( 1, "Old ref: %lld/%lld, New ref: %lld/%lld", p_pgrm->cr_ref,
123 p_pgrm->sysdate_ref, i_clock, i_sysdate );
124 p_pgrm->cr_ref = i_clock;
125 p_pgrm->sysdate_ref = i_sysdate;
128 /*****************************************************************************
129 * EscapeDiscontinuity: send a NULL packet to the decoders
130 *****************************************************************************/
131 static void EscapeDiscontinuity( input_thread_t * p_input,
132 pgrm_descriptor_t * p_pgrm )
136 for( i_es = 0; i_es < p_pgrm->i_es_number; i_es++ )
138 es_descriptor_t * p_es = p_pgrm->pp_es[i_es];
140 if( p_es->p_decoder_fifo != NULL )
142 input_NullPacket( p_input, p_es );
147 /*****************************************************************************
148 * EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
149 *****************************************************************************/
150 static void EscapeAudioDiscontinuity( input_thread_t * p_input,
151 pgrm_descriptor_t * p_pgrm )
155 for( i_es = 0; i_es < p_pgrm->i_es_number; i_es++ )
157 es_descriptor_t * p_es = p_pgrm->pp_es[i_es];
159 if( p_es->p_decoder_fifo != NULL && p_es->b_audio )
161 input_NullPacket( p_input, p_es );
166 /*****************************************************************************
167 * input_ClockInit: reinitializes the clock reference after a stream
169 *****************************************************************************/
170 void input_ClockInit( pgrm_descriptor_t * p_pgrm )
174 p_pgrm->sysdate_ref = 0;
175 p_pgrm->delta_cr = 0;
176 p_pgrm->c_average_count = 0;
179 /*****************************************************************************
180 * input_ClockManageRef: manages a clock reference
181 *****************************************************************************/
182 void input_ClockManageRef( input_thread_t * p_input,
183 pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
185 if( p_pgrm->i_synchro_state != SYNCHRO_OK )
187 /* Feed synchro with a new reference point. */
188 input_ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
189 p_pgrm->i_synchro_state = SYNCHRO_OK;
193 if ( p_pgrm->last_cr != 0 &&
194 ( (p_pgrm->last_cr - i_clock) > CR_MAX_GAP
195 || (p_pgrm->last_cr - i_clock) < - CR_MAX_GAP ) )
197 /* Stream discontinuity, for which we haven't received a
198 * warning from the stream control facilities (dd-edited
200 intf_WarnMsg( 3, "Clock gap, unexpected stream discontinuity" );
201 input_ClockInit( p_pgrm );
202 p_pgrm->i_synchro_state = SYNCHRO_START;
203 EscapeDiscontinuity( p_input, p_pgrm );
206 p_pgrm->last_cr = i_clock;
208 if( p_input->stream.b_pace_control
209 && p_input->stream.pp_programs[0] == p_pgrm )
211 /* Wait a while before delivering the packets to the decoder.
212 * In case of multiple programs, we arbitrarily follow the
213 * clock of the first program. */
214 mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
216 /* Now take into account interface changes. */
217 vlc_mutex_lock( &p_input->stream.stream_lock );
218 if( p_input->stream.i_new_status != UNDEF_S )
220 if( p_input->stream.i_new_status == PAUSE_S )
222 vlc_cond_wait( &p_input->stream.stream_wait,
223 &p_input->stream.stream_lock );
224 input_ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
228 input_ClockNewRef( p_input, p_pgrm, i_clock,
229 ClockToSysdate( p_input, p_pgrm, i_clock ) );
232 vlc_mutex_lock( &p_input->stream.control.control_lock );
233 p_input->stream.control.i_status = p_input->stream.i_new_status;
235 if( p_input->stream.control.i_status != PLAYING_S
236 && p_input->stream.control.i_status != PAUSE_S )
238 p_input->stream.control.i_rate = p_input->stream.i_new_rate;
239 p_input->stream.control.b_mute = 1;
241 /* Feed the audio decoders with a NULL packet to avoid
242 * discontinuities. */
243 EscapeAudioDiscontinuity( p_input, p_pgrm );
247 p_input->stream.control.i_rate = DEFAULT_RATE;
248 p_input->stream.control.b_mute = 0;
250 vlc_mutex_unlock( &p_input->stream.control.control_lock );
252 p_input->stream.i_new_status = UNDEF_S;
253 p_input->stream.i_new_rate = UNDEF_S;
255 vlc_mutex_unlock( &p_input->stream.stream_lock );
259 /* Smooth clock reference variations. */
260 mtime_t i_extrapoled_clock = ClockCurrent( p_input, p_pgrm );
262 /* Bresenham algorithm to smooth variations. */
263 if( p_pgrm->c_average_count == CR_MAX_AVERAGE_COUNTER )
265 p_pgrm->delta_cr = ( p_pgrm->delta_cr
266 * (CR_MAX_AVERAGE_COUNTER - 1)
267 + i_extrapoled_clock )
268 / CR_MAX_AVERAGE_COUNTER;
272 p_pgrm->delta_cr = ( p_pgrm->delta_cr
273 * p_pgrm->c_average_count
274 + i_extrapoled_clock )
275 / (p_pgrm->c_average_count + 1);
276 p_pgrm->c_average_count++;
282 /*****************************************************************************
283 * input_ClockGetTS: manages a PTS or DTS
284 *****************************************************************************/
285 mtime_t input_ClockGetTS( input_thread_t * p_input,
286 pgrm_descriptor_t * p_pgrm, mtime_t i_ts )
288 if( p_pgrm->i_synchro_state == SYNCHRO_OK )
290 return( ClockToSysdate( p_input, p_pgrm, i_ts + p_pgrm->delta_cr )
291 + DEFAULT_PTS_DELAY );