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.8 2001/03/14 00:40:24 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 = (mtime_t)(i_clock - p_pgrm->cr_ref)
93 * (mtime_t)p_input->stream.control.i_rate
96 / (mtime_t)DEFAULT_RATE
97 + (mtime_t)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 * ClockNewRef: writes a new clock reference
118 *****************************************************************************/
119 static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
120 mtime_t i_clock, mtime_t i_sysdate )
122 p_pgrm->cr_ref = i_clock;
123 p_pgrm->sysdate_ref = i_sysdate;
126 /*****************************************************************************
127 * input_ClockInit: reinitializes the clock reference after a stream
129 *****************************************************************************/
130 void input_ClockInit( pgrm_descriptor_t * p_pgrm )
134 p_pgrm->sysdate_ref = 0;
135 p_pgrm->delta_cr = 0;
136 p_pgrm->c_average_count = 0;
139 /*****************************************************************************
140 * input_ClockManageRef: manages a clock reference
141 *****************************************************************************/
142 void input_ClockManageRef( input_thread_t * p_input,
143 pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
145 if( p_pgrm->i_synchro_state != SYNCHRO_OK )
147 /* Feed synchro with a new reference point. */
148 ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
149 p_pgrm->i_synchro_state = SYNCHRO_OK;
153 if ( p_pgrm->last_cr != 0 &&
154 ( (p_pgrm->last_cr - i_clock) > CR_MAX_GAP
155 || (p_pgrm->last_cr - i_clock) < - CR_MAX_GAP ) )
157 /* Stream discontinuity, for which we haven't received a
158 * warning from the stream control facilities (dd-edited
160 intf_WarnMsg( 3, "Clock gap, unexpected stream discontinuity" );
161 input_ClockInit( p_pgrm );
162 p_pgrm->i_synchro_state = SYNCHRO_START;
163 input_EscapeDiscontinuity( p_input, p_pgrm );
166 p_pgrm->last_cr = i_clock;
168 if( p_input->stream.b_pace_control
169 && p_input->stream.pp_programs[0] == p_pgrm )
171 /* Wait a while before delivering the packets to the decoder.
172 * In case of multiple programs, we arbitrarily follow the
173 * clock of the first program. */
174 mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
176 /* Now take into account interface changes. */
177 vlc_mutex_lock( &p_input->stream.stream_lock );
178 if( p_input->stream.i_new_status != UNDEF_S )
180 if( p_input->stream.i_new_status == PAUSE_S )
182 vlc_cond_wait( &p_input->stream.stream_wait,
183 &p_input->stream.stream_lock );
184 ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
188 ClockNewRef( p_input, p_pgrm, i_clock,
189 ClockToSysdate( p_input, p_pgrm, i_clock ) );
192 vlc_mutex_lock( &p_input->stream.control.control_lock );
193 p_input->stream.control.i_status = p_input->stream.i_new_status;
195 if( p_input->stream.control.i_status != PLAYING_S
196 && p_input->stream.control.i_status != PAUSE_S )
198 p_input->stream.control.i_rate = p_input->stream.i_new_rate;
199 p_input->stream.control.b_mute = 1;
201 /* Feed the audio decoders with a NULL packet to avoid
202 * discontinuities. */
203 input_EscapeAudioDiscontinuity( p_input, p_pgrm );
207 p_input->stream.control.i_rate = DEFAULT_RATE;
208 p_input->stream.control.b_mute = 0;
210 vlc_mutex_unlock( &p_input->stream.control.control_lock );
212 p_input->stream.i_new_status = UNDEF_S;
213 p_input->stream.i_new_rate = UNDEF_S;
215 vlc_mutex_unlock( &p_input->stream.stream_lock );
219 /* Smooth clock reference variations. */
220 mtime_t i_extrapoled_clock = ClockCurrent( p_input, p_pgrm );
222 /* Bresenham algorithm to smooth variations. */
223 if( p_pgrm->c_average_count == CR_MAX_AVERAGE_COUNTER )
225 p_pgrm->delta_cr = ( p_pgrm->delta_cr
226 * (CR_MAX_AVERAGE_COUNTER - 1)
227 + i_extrapoled_clock )
228 / CR_MAX_AVERAGE_COUNTER;
232 p_pgrm->delta_cr = ( p_pgrm->delta_cr
233 * p_pgrm->c_average_count
234 + i_extrapoled_clock )
235 / (p_pgrm->c_average_count + 1);
236 p_pgrm->c_average_count++;
242 /*****************************************************************************
243 * input_ClockGetTS: manages a PTS or DTS
244 *****************************************************************************/
245 mtime_t input_ClockGetTS( input_thread_t * p_input,
246 pgrm_descriptor_t * p_pgrm, mtime_t i_ts )
248 if( p_pgrm->i_synchro_state == SYNCHRO_OK )
250 return( ClockToSysdate( p_input, p_pgrm, i_ts + p_pgrm->delta_cr )
251 + DEFAULT_PTS_DELAY );