1 /*****************************************************************************
2 * input_clock.c: Clock/System date convertions, stream management
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: input_clock.c,v 1.38 2003/07/27 14:10:02 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 <string.h> /* memcpy(), memset() */
31 #include "stream_control.h"
32 #include "input_ext-intf.h"
33 #include "input_ext-dec.h"
34 #include "input_ext-plugins.h"
37 * DISCUSSION : SYNCHRONIZATION METHOD
39 * In some cases we can impose the pace of reading (when reading from a
40 * file or a pipe), and for the synchronization we simply sleep() until
41 * it is time to deliver the packet to the decoders. When reading from
42 * the network, we must be read at the same pace as the server writes,
43 * otherwise the kernel's buffer will trash packets. The risk is now to
44 * overflow the input buffers in case the server goes too fast, that is
45 * why we do these calculations :
47 * We compute a mean for the pcr because we want to eliminate the
48 * network jitter and keep the low frequency variations. The mean is
49 * in fact a low pass filter and the jitter is a high frequency signal
50 * that is why it is eliminated by the filter/average.
52 * The low frequency variations enable us to synchronize the client clock
53 * with the server clock because they represent the time variation between
54 * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
55 * the presentation dates for the audio and video frames. With those dates
56 * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
57 * as it is sent by the server and so we keep the synchronization between
58 * the server and the client.
60 * It is a very important matter if you want to avoid underflow or overflow
61 * in all the FIFOs, but it may be not enough.
64 static void ClockNewRef( pgrm_descriptor_t * p_pgrm,
65 mtime_t i_clock, mtime_t i_sysdate );
67 /*****************************************************************************
69 *****************************************************************************/
71 /* Maximum number of samples used to compute the dynamic average value.
72 * We use the following formula :
73 * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
75 #define CR_MAX_AVERAGE_COUNTER 40
77 /* Maximum gap allowed between two CRs. */
78 #define CR_MAX_GAP 1000000
80 /* Latency introduced on DVDs with CR == 0 on chapter change - this is from
82 #define CR_MEAN_PTS_GAP 300000
84 /*****************************************************************************
85 * ClockToSysdate: converts a movie clock to system date
86 *****************************************************************************/
87 static mtime_t ClockToSysdate( input_thread_t * p_input,
88 pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
90 mtime_t i_sysdate = 0;
92 if( p_pgrm->i_synchro_state == SYNCHRO_OK )
94 i_sysdate = (mtime_t)(i_clock - p_pgrm->cr_ref)
95 * (mtime_t)p_input->stream.control.i_rate
99 i_sysdate += (mtime_t)p_pgrm->sysdate_ref;
101 if ( i_sysdate < mdate() - CR_MAX_GAP )
103 msg_Warn( p_input, "Bogus clock encountered, resetting" );
104 ClockNewRef( p_pgrm, i_clock, i_sysdate = mdate() );
111 /*****************************************************************************
112 * ClockCurrent: converts current system date to clock units
113 *****************************************************************************
114 * Caution : the synchro state must be SYNCHRO_OK for this to operate.
115 *****************************************************************************/
116 static mtime_t ClockCurrent( input_thread_t * p_input,
117 pgrm_descriptor_t * p_pgrm )
119 return( (mdate() - p_pgrm->sysdate_ref) * 27 * DEFAULT_RATE
120 / p_input->stream.control.i_rate / 300
124 /*****************************************************************************
125 * ClockNewRef: writes a new clock reference
126 *****************************************************************************/
127 static void ClockNewRef( pgrm_descriptor_t * p_pgrm,
128 mtime_t i_clock, mtime_t i_sysdate )
130 p_pgrm->cr_ref = i_clock;
131 p_pgrm->sysdate_ref = i_sysdate ;
134 /*****************************************************************************
135 * input_ClockInit: reinitializes the clock reference after a stream
137 *****************************************************************************/
138 void input_ClockInit( pgrm_descriptor_t * p_pgrm )
141 p_pgrm->last_pts = 0;
143 p_pgrm->sysdate_ref = 0;
144 p_pgrm->delta_cr = 0;
145 p_pgrm->c_average_count = 0;
148 /*****************************************************************************
149 * input_ClockManageControl: handles the messages from the interface
150 *****************************************************************************
151 * Returns UNDEF_S if nothing happened, PAUSE_S if the stream was paused
152 *****************************************************************************/
153 int input_ClockManageControl( input_thread_t * p_input,
154 pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
156 int i_return_value = UNDEF_S;
158 vlc_mutex_lock( &p_input->stream.stream_lock );
160 if( p_input->stream.i_new_status == PAUSE_S )
164 vlc_mutex_lock( &p_input->stream.control.control_lock );
165 i_old_status = p_input->stream.control.i_status;
166 p_input->stream.control.i_status = PAUSE_S;
167 vlc_mutex_unlock( &p_input->stream.control.control_lock );
169 vlc_cond_wait( &p_input->stream.stream_wait,
170 &p_input->stream.stream_lock );
171 ClockNewRef( p_pgrm, i_clock, p_pgrm->last_pts > mdate() ?
172 p_pgrm->last_pts : mdate() );
174 if( p_input->stream.i_new_status == PAUSE_S )
176 /* PAUSE_S undoes the pause state: Return to old state. */
177 vlc_mutex_lock( &p_input->stream.control.control_lock );
178 p_input->stream.control.i_status = i_old_status;
179 vlc_mutex_unlock( &p_input->stream.control.control_lock );
181 p_input->stream.i_new_status = UNDEF_S;
182 p_input->stream.i_new_rate = UNDEF_S;
185 /* We handle i_new_status != PAUSE_S below... */
187 i_return_value = PAUSE_S;
190 if( p_input->stream.i_new_status != UNDEF_S )
192 vlc_mutex_lock( &p_input->stream.control.control_lock );
194 p_input->stream.control.i_status = p_input->stream.i_new_status;
196 ClockNewRef( p_pgrm, i_clock,
197 ClockToSysdate( p_input, p_pgrm, i_clock ) );
199 if( p_input->stream.control.i_status == PLAYING_S )
201 p_input->stream.control.i_rate = DEFAULT_RATE;
202 p_input->stream.control.b_mute = 0;
206 p_input->stream.control.i_rate = p_input->stream.i_new_rate;
207 p_input->stream.control.b_mute = 1;
209 /* Feed the audio decoders with a NULL packet to avoid
210 * discontinuities. */
211 input_EscapeAudioDiscontinuity( p_input );
214 p_input->stream.i_new_status = UNDEF_S;
215 p_input->stream.i_new_rate = UNDEF_S;
217 vlc_mutex_unlock( &p_input->stream.control.control_lock );
220 vlc_mutex_unlock( &p_input->stream.stream_lock );
222 return( i_return_value );
225 /*****************************************************************************
226 * input_ClockManageRef: manages a clock reference
227 *****************************************************************************/
228 void input_ClockManageRef( input_thread_t * p_input,
229 pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
231 /* take selected program if none specified */
234 p_pgrm = p_input->stream.p_selected_program;
237 if( ( p_pgrm->i_synchro_state != SYNCHRO_OK ) || ( i_clock == 0 ) )
239 /* Feed synchro with a new reference point. */
240 ClockNewRef( p_pgrm, i_clock,
241 p_pgrm->last_pts + CR_MEAN_PTS_GAP > mdate() ?
242 p_pgrm->last_pts + CR_MEAN_PTS_GAP : mdate() );
243 p_pgrm->i_synchro_state = SYNCHRO_OK;
245 if( p_input->stream.b_pace_control
246 && p_input->stream.p_selected_program == p_pgrm )
248 p_pgrm->last_cr = i_clock;
249 mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
254 p_pgrm->delta_cr = 0;
255 p_pgrm->c_average_count = 0;
260 if ( p_pgrm->last_cr != 0 &&
261 ( (p_pgrm->last_cr - i_clock) > CR_MAX_GAP
262 || (p_pgrm->last_cr - i_clock) < - CR_MAX_GAP ) )
264 /* Stream discontinuity, for which we haven't received a
265 * warning from the stream control facilities (dd-edited
267 msg_Warn( p_input, "clock gap, unexpected stream discontinuity" );
268 input_ClockInit( p_pgrm );
269 p_pgrm->i_synchro_state = SYNCHRO_START;
270 input_EscapeDiscontinuity( p_input );
273 p_pgrm->last_cr = i_clock;
275 if( p_input->stream.b_pace_control
276 && p_input->stream.p_selected_program == p_pgrm )
278 /* Wait a while before delivering the packets to the decoder.
279 * In case of multiple programs, we arbitrarily follow the
280 * clock of the first program. */
281 mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
283 /* Now take into account interface changes. */
284 input_ClockManageControl( p_input, p_pgrm, i_clock );
288 /* Smooth clock reference variations. */
289 mtime_t i_extrapoled_clock = ClockCurrent( p_input, p_pgrm );
291 /* Bresenham algorithm to smooth variations. */
292 if( p_pgrm->c_average_count == CR_MAX_AVERAGE_COUNTER )
294 p_pgrm->delta_cr = ( p_pgrm->delta_cr
295 * (CR_MAX_AVERAGE_COUNTER - 1)
296 + ( i_extrapoled_clock - i_clock ) )
297 / CR_MAX_AVERAGE_COUNTER;
301 p_pgrm->delta_cr = ( p_pgrm->delta_cr
302 * p_pgrm->c_average_count
303 + ( i_extrapoled_clock - i_clock ) )
304 / (p_pgrm->c_average_count + 1);
305 p_pgrm->c_average_count++;
311 /*****************************************************************************
312 * input_ClockGetTS: manages a PTS or DTS
313 *****************************************************************************/
314 mtime_t input_ClockGetTS( input_thread_t * p_input,
315 pgrm_descriptor_t * p_pgrm, mtime_t i_ts )
317 /* take selected program if none specified */
320 p_pgrm = p_input->stream.p_selected_program;
323 if( p_pgrm->i_synchro_state == SYNCHRO_OK )
325 p_pgrm->last_pts = ClockToSysdate( p_input, p_pgrm,
326 i_ts + p_pgrm->delta_cr );
327 return( p_pgrm->last_pts + p_input->i_pts_delay );