1 /*****************************************************************************
2 * input_clock.c: Clock/System date convertions, stream management
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include "input_internal.h"
33 * DISCUSSION : SYNCHRONIZATION METHOD
35 * In some cases we can impose the pace of reading (when reading from a
36 * file or a pipe), and for the synchronization we simply sleep() until
37 * it is time to deliver the packet to the decoders. When reading from
38 * the network, we must be read at the same pace as the server writes,
39 * otherwise the kernel's buffer will trash packets. The risk is now to
40 * overflow the input buffers in case the server goes too fast, that is
41 * why we do these calculations :
43 * We compute a mean for the pcr because we want to eliminate the
44 * network jitter and keep the low frequency variations. The mean is
45 * in fact a low pass filter and the jitter is a high frequency signal
46 * that is why it is eliminated by the filter/average.
48 * The low frequency variations enable us to synchronize the client clock
49 * with the server clock because they represent the time variation between
50 * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
51 * the presentation dates for the audio and video frames. With those dates
52 * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
53 * as it is sent by the server and so we keep the synchronization between
54 * the server and the client.
56 * It is a very important matter if you want to avoid underflow or overflow
57 * in all the FIFOs, but it may be not enough.
60 /* p_input->p->i_cr_average : Maximum number of samples used to compute the
61 * dynamic average value.
62 * We use the following formula :
63 * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
67 static void ClockNewRef( input_clock_t * p_pgrm,
68 mtime_t i_clock, mtime_t i_sysdate );
70 /*****************************************************************************
72 *****************************************************************************/
74 /* Maximum gap allowed between two CRs. */
75 #define CR_MAX_GAP (I64C(2000000)*100/9)
77 /* Latency introduced on DVDs with CR == 0 on chapter change - this is from
79 #define CR_MEAN_PTS_GAP 300000
81 /*****************************************************************************
82 * ClockToSysdate: converts a movie clock to system date
83 *****************************************************************************/
84 static mtime_t ClockToSysdate( input_clock_t *cl, mtime_t i_clock )
86 if( cl->i_synchro_state != SYNCHRO_OK )
89 return (i_clock - cl->cr_ref) * cl->i_rate / INPUT_RATE_DEFAULT +
93 /*****************************************************************************
94 * ClockCurrent: converts current system date to clock units
95 *****************************************************************************
96 * Caution : the synchro state must be SYNCHRO_OK for this to operate.
97 *****************************************************************************/
98 static mtime_t ClockCurrent( input_clock_t *cl )
100 return (mdate() - cl->sysdate_ref) * INPUT_RATE_DEFAULT / cl->i_rate +
104 /*****************************************************************************
105 * ClockNewRef: writes a new clock reference
106 *****************************************************************************/
107 static void ClockNewRef( input_clock_t *cl,
108 mtime_t i_clock, mtime_t i_sysdate )
110 cl->cr_ref = i_clock;
111 cl->sysdate_ref = i_sysdate ;
114 /*****************************************************************************
115 * input_ClockInit: reinitializes the clock reference after a stream
117 *****************************************************************************/
118 void input_ClockInit( input_thread_t *p_input,
119 input_clock_t *cl, vlc_bool_t b_master, int i_cr_average )
121 cl->i_synchro_state = SYNCHRO_START;
125 cl->last_sysdate = 0;
129 cl->i_delta_cr_residue = 0;
130 cl->i_rate = p_input->p->i_rate;
132 cl->i_cr_average = i_cr_average;
134 cl->b_master = b_master;
137 /*****************************************************************************
138 * input_ClockSetPCR: manages a clock reference
139 *****************************************************************************/
140 void input_ClockSetPCR( input_thread_t *p_input,
141 input_clock_t *cl, mtime_t i_clock )
143 const vlc_bool_t b_synchronize = p_input->b_can_pace_control && cl->b_master;
144 const mtime_t i_mdate = mdate();
146 if( ( cl->i_synchro_state != SYNCHRO_OK ) ||
147 ( i_clock == 0 && cl->last_cr != 0 ) )
149 /* Feed synchro with a new reference point. */
150 ClockNewRef( cl, i_clock,
151 __MAX( cl->last_pts + CR_MEAN_PTS_GAP, i_mdate ) );
152 cl->i_synchro_state = SYNCHRO_OK;
158 cl->i_delta_cr_residue = 0;
159 cl->last_sysdate = 0;
163 else if ( cl->last_cr != 0 &&
164 ( (cl->last_cr - i_clock) > CR_MAX_GAP ||
165 (cl->last_cr - i_clock) < - CR_MAX_GAP ) )
167 /* Stream discontinuity, for which we haven't received a
168 * warning from the stream control facilities (dd-edited
170 msg_Warn( p_input, "clock gap, unexpected stream discontinuity" );
171 input_ClockInit( p_input, cl, cl->b_master, cl->i_cr_average );
174 cl->last_cr = i_clock;
175 cl->last_sysdate = i_mdate;
179 /* Wait a while before delivering the packets to the decoder.
180 * In case of multiple programs, we arbitrarily follow the
181 * clock of the selected program. */
182 if( !p_input->p->b_out_pace_control )
184 mtime_t i_wakeup = ClockToSysdate( cl, i_clock );
185 while( (i_wakeup - mdate()) / CLOCK_FREQ > 1 )
187 msleep( CLOCK_FREQ );
188 if( p_input->b_die ) i_wakeup = mdate();
193 else if ( i_mdate - cl->last_update > 200000 )
195 /* Smooth clock reference variations. */
196 const mtime_t i_extrapoled_clock = ClockCurrent( cl );
197 /* Bresenham algorithm to smooth variations. */
198 const mtime_t i_tmp = cl->delta_cr * (cl->i_cr_average - 1) +
199 ( i_extrapoled_clock - i_clock ) * 1 +
200 cl->i_delta_cr_residue;
202 cl->i_delta_cr_residue = i_tmp % cl->i_cr_average;
203 cl->delta_cr = i_tmp / cl->i_cr_average;
205 cl->last_update = i_mdate;
209 /*****************************************************************************
210 * input_ClockGetTS: manages a PTS or DTS
211 *****************************************************************************/
212 mtime_t input_ClockGetTS( input_thread_t * p_input,
213 input_clock_t *cl, mtime_t i_ts )
215 if( cl->i_synchro_state != SYNCHRO_OK )
218 cl->last_pts = ClockToSysdate( cl, i_ts + cl->delta_cr );
219 return cl->last_pts + p_input->i_pts_delay;
222 /*****************************************************************************
223 * input_ClockSetRate:
224 *****************************************************************************/
225 void input_ClockSetRate( input_thread_t *p_input, input_clock_t *cl )
227 if( cl->i_synchro_state == SYNCHRO_OK )
229 /* Move the reference point */
230 cl->cr_ref = cl->last_cr;
231 cl->sysdate_ref = cl->last_sysdate;
233 cl->i_rate = p_input->p->i_rate;