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.11 2001/04/28 03:36:25 sam 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 *****************************************************************************/
29 #include <string.h> /* memcpy(), memset() */
30 #include <sys/types.h> /* off_t */
38 #include "stream_control.h"
39 #include "input_ext-intf.h"
40 #include "input_ext-dec.h"
45 * DISCUSSION : SYNCHRONIZATION METHOD
47 * In some cases we can impose the pace of reading (when reading from a
48 * file or a pipe), and for the synchronization we simply sleep() until
49 * it is time to deliver the packet to the decoders. When reading from
50 * the network, we must be read at the same pace as the server writes,
51 * otherwise the kernel's buffer will trash packets. The risk is now to
52 * overflow the input buffers in case the server goes too fast, that is
53 * why we do these calculations :
55 * We compute a mean for the pcr because we want to eliminate the
56 * network jitter and keep the low frequency variations. The mean is
57 * in fact a low pass filter and the jitter is a high frequency signal
58 * that is why it is eliminated by the filter/average.
60 * The low frequency variations enable us to synchronize the client clock
61 * with the server clock because they represent the time variation between
62 * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
63 * the presentation dates for the audio and video frames. With those dates
64 * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
65 * as it is sent by the server and so we keep the synchronization between
66 * the server and the client.
68 * It is a very important matter if you want to avoid underflow or overflow
69 * in all the FIFOs, but it may be not enough.
72 /*****************************************************************************
74 *****************************************************************************/
76 /* Maximum number of samples used to compute the dynamic average value.
77 * We use the following formula :
78 * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
80 #define CR_MAX_AVERAGE_COUNTER 40
82 /* Maximum gap allowed between two CRs. */
83 #define CR_MAX_GAP 1000000
85 /*****************************************************************************
86 * ClockToSysdate: converts a movie clock to system date
87 *****************************************************************************/
88 static mtime_t ClockToSysdate( input_thread_t * p_input,
89 pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
91 mtime_t i_sysdate = 0;
93 if( p_pgrm->i_synchro_state == SYNCHRO_OK )
95 i_sysdate = (mtime_t)(i_clock - p_pgrm->cr_ref)
96 * (mtime_t)p_input->stream.control.i_rate
99 / (mtime_t)DEFAULT_RATE
100 + (mtime_t)p_pgrm->sysdate_ref;
106 /*****************************************************************************
107 * ClockCurrent: converts current system date to clock units
108 *****************************************************************************
109 * Caution : the synchro state must be SYNCHRO_OK for this to operate.
110 *****************************************************************************/
111 static mtime_t ClockCurrent( input_thread_t * p_input,
112 pgrm_descriptor_t * p_pgrm )
114 return( (mdate() - p_pgrm->sysdate_ref) * 27 * DEFAULT_RATE
115 / p_input->stream.control.i_rate / 300
119 /*****************************************************************************
120 * ClockNewRef: writes a new clock reference
121 *****************************************************************************/
122 static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
123 mtime_t i_clock, mtime_t i_sysdate )
125 p_pgrm->cr_ref = i_clock;
126 p_pgrm->sysdate_ref = i_sysdate;
129 /*****************************************************************************
130 * input_ClockInit: reinitializes the clock reference after a stream
132 *****************************************************************************/
133 void input_ClockInit( pgrm_descriptor_t * p_pgrm )
137 p_pgrm->sysdate_ref = 0;
138 p_pgrm->delta_cr = 0;
139 p_pgrm->c_average_count = 0;
142 /*****************************************************************************
143 * input_ClockManageRef: manages a clock reference
144 *****************************************************************************/
145 void input_ClockManageRef( input_thread_t * p_input,
146 pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
148 if( p_pgrm->i_synchro_state != SYNCHRO_OK )
150 /* Feed synchro with a new reference point. */
151 ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
152 p_pgrm->i_synchro_state = SYNCHRO_OK;
156 if ( p_pgrm->last_cr != 0 &&
157 ( (p_pgrm->last_cr - i_clock) > CR_MAX_GAP
158 || (p_pgrm->last_cr - i_clock) < - CR_MAX_GAP ) )
160 /* Stream discontinuity, for which we haven't received a
161 * warning from the stream control facilities (dd-edited
163 intf_WarnMsg( 3, "Clock gap, unexpected stream discontinuity" );
164 input_ClockInit( p_pgrm );
165 p_pgrm->i_synchro_state = SYNCHRO_START;
166 input_EscapeDiscontinuity( p_input, p_pgrm );
169 p_pgrm->last_cr = i_clock;
171 if( p_input->stream.b_pace_control
172 && p_input->stream.pp_programs[0] == p_pgrm )
174 /* Wait a while before delivering the packets to the decoder.
175 * In case of multiple programs, we arbitrarily follow the
176 * clock of the first program. */
177 mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
179 /* Now take into account interface changes. */
180 vlc_mutex_lock( &p_input->stream.stream_lock );
182 if( p_input->stream.i_new_status == PAUSE_S )
185 vlc_mutex_lock( &p_input->stream.control.control_lock );
186 i_old_status = p_input->stream.control.i_status;
188 p_input->stream.control.i_status = PAUSE_S;
189 vlc_cond_wait( &p_input->stream.stream_wait,
190 &p_input->stream.stream_lock );
191 ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
193 if( p_input->stream.i_new_status == PAUSE_S )
195 /* PAUSE_S undoes the pause state: Return to old state. */
196 p_input->stream.control.i_status = i_old_status;
197 p_input->stream.i_new_status = UNDEF_S;
198 p_input->stream.i_new_rate = UNDEF_S;
201 /* We handle i_new_status != PAUSE_S below... */
202 vlc_mutex_unlock( &p_input->stream.control.control_lock );
205 if( p_input->stream.i_new_status != UNDEF_S )
207 vlc_mutex_lock( &p_input->stream.control.control_lock );
209 p_input->stream.control.i_status = p_input->stream.i_new_status;
211 ClockNewRef( p_input, p_pgrm, i_clock,
212 ClockToSysdate( p_input, p_pgrm, i_clock ) );
214 if( p_input->stream.control.i_status == PLAYING_S )
216 p_input->stream.control.i_rate = DEFAULT_RATE;
217 p_input->stream.control.b_mute = 0;
221 p_input->stream.control.i_rate = p_input->stream.i_new_rate;
222 p_input->stream.control.b_mute = 1;
224 /* Feed the audio decoders with a NULL packet to avoid
225 * discontinuities. */
226 input_EscapeAudioDiscontinuity( p_input, p_pgrm );
229 p_input->stream.i_new_status = UNDEF_S;
230 p_input->stream.i_new_rate = UNDEF_S;
232 vlc_mutex_unlock( &p_input->stream.control.control_lock );
235 vlc_mutex_unlock( &p_input->stream.stream_lock );
239 /* Smooth clock reference variations. */
240 mtime_t i_extrapoled_clock = ClockCurrent( p_input, p_pgrm );
242 /* Bresenham algorithm to smooth variations. */
243 if( p_pgrm->c_average_count == CR_MAX_AVERAGE_COUNTER )
245 p_pgrm->delta_cr = ( p_pgrm->delta_cr
246 * (CR_MAX_AVERAGE_COUNTER - 1)
247 + i_extrapoled_clock )
248 / CR_MAX_AVERAGE_COUNTER;
252 p_pgrm->delta_cr = ( p_pgrm->delta_cr
253 * p_pgrm->c_average_count
254 + i_extrapoled_clock )
255 / (p_pgrm->c_average_count + 1);
256 p_pgrm->c_average_count++;
262 /*****************************************************************************
263 * input_ClockGetTS: manages a PTS or DTS
264 *****************************************************************************/
265 mtime_t input_ClockGetTS( input_thread_t * p_input,
266 pgrm_descriptor_t * p_pgrm, mtime_t i_ts )
268 if( p_pgrm->i_synchro_state == SYNCHRO_OK )
270 return( ClockToSysdate( p_input, p_pgrm, i_ts + p_pgrm->delta_cr )
271 + DEFAULT_PTS_DELAY );