]> git.sesse.net Git - vlc/blob - src/input/clock.c
backport [13191] and [13192] once again
[vlc] / src / input / clock.c
1 /*****************************************************************************
2  * input_clock.c: Clock/System date convertions, stream management
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <vlc/vlc.h>
29
30 #include <vlc/input.h>
31 #include "input_internal.h"
32
33 /*
34  * DISCUSSION : SYNCHRONIZATION METHOD
35  *
36  * In some cases we can impose the pace of reading (when reading from a
37  * file or a pipe), and for the synchronization we simply sleep() until
38  * it is time to deliver the packet to the decoders. When reading from
39  * the network, we must be read at the same pace as the server writes,
40  * otherwise the kernel's buffer will trash packets. The risk is now to
41  * overflow the input buffers in case the server goes too fast, that is
42  * why we do these calculations :
43  *
44  * We compute a mean for the pcr because we want to eliminate the
45  * network jitter and keep the low frequency variations. The mean is
46  * in fact a low pass filter and the jitter is a high frequency signal
47  * that is why it is eliminated by the filter/average.
48  *
49  * The low frequency variations enable us to synchronize the client clock
50  * with the server clock because they represent the time variation between
51  * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
52  * the presentation dates for the audio and video frames. With those dates
53  * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
54  * as it is sent by the server and so we keep the synchronization between
55  * the server and the client.
56  *
57  * It is a very important matter if you want to avoid underflow or overflow
58  * in all the FIFOs, but it may be not enough.
59  */
60
61 /* p_input->i_cr_average : Maximum number of samples used to compute the
62  * dynamic average value.
63  * We use the following formula :
64  * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
65  */
66
67
68 static void ClockNewRef( input_clock_t * p_pgrm,
69                          mtime_t i_clock, mtime_t i_sysdate );
70
71 /*****************************************************************************
72  * Constants
73  *****************************************************************************/
74
75 /* Maximum gap allowed between two CRs. */
76 #define CR_MAX_GAP 2000000
77
78 /* Latency introduced on DVDs with CR == 0 on chapter change - this is from
79  * my dice --Meuuh */
80 #define CR_MEAN_PTS_GAP 300000
81
82 /*****************************************************************************
83  * ClockToSysdate: converts a movie clock to system date
84  *****************************************************************************/
85 static mtime_t ClockToSysdate( input_thread_t *p_input,
86                                input_clock_t *cl, mtime_t i_clock )
87 {
88     mtime_t     i_sysdate = 0;
89
90     if( cl->i_synchro_state == SYNCHRO_OK )
91     {
92         i_sysdate = (mtime_t)(i_clock - cl->cr_ref)
93                         * (mtime_t)p_input->i_rate
94                         * (mtime_t)300;
95         i_sysdate /= 27;
96         i_sysdate /= 1000;
97         i_sysdate += (mtime_t)cl->sysdate_ref;
98     }
99
100     return( i_sysdate );
101 }
102
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                              input_clock_t *cl )
110 {
111     return( (mdate() - cl->sysdate_ref) * 27 * INPUT_RATE_DEFAULT
112              / p_input->i_rate / 300
113              + cl->cr_ref );
114 }
115
116 /*****************************************************************************
117  * ClockNewRef: writes a new clock reference
118  *****************************************************************************/
119 static void ClockNewRef( input_clock_t *cl,
120                          mtime_t i_clock, mtime_t i_sysdate )
121 {
122     cl->cr_ref = i_clock;
123     cl->sysdate_ref = i_sysdate ;
124 }
125
126 /*****************************************************************************
127  * input_ClockInit: reinitializes the clock reference after a stream
128  *                  discontinuity
129  *****************************************************************************/
130 void input_ClockInit( input_clock_t *cl, vlc_bool_t b_master, int i_cr_average )
131 {
132     cl->i_synchro_state = SYNCHRO_START;
133
134     cl->last_cr = 0;
135     cl->last_pts = 0;
136     cl->last_sysdate = 0;
137     cl->cr_ref = 0;
138     cl->sysdate_ref = 0;
139     cl->delta_cr = 0;
140     cl->i_delta_cr_residue = 0;
141
142     cl->i_cr_average = i_cr_average;
143
144     cl->b_master = b_master;
145 }
146
147 #if 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                                input_clock_t *cl, mtime_t i_clock )
155 {
156 #if 0
157     vlc_value_t val;
158     int i_return_value = UNDEF_S;
159
160     vlc_mutex_lock( &p_input->stream.stream_lock );
161
162     if( p_input->stream.i_new_status == PAUSE_S )
163     {
164         int i_old_status;
165
166         vlc_mutex_lock( &p_input->stream.control.control_lock );
167         i_old_status = p_input->stream.control.i_status;
168         p_input->stream.control.i_status = PAUSE_S;
169         vlc_mutex_unlock( &p_input->stream.control.control_lock );
170
171         vlc_cond_wait( &p_input->stream.stream_wait,
172                        &p_input->stream.stream_lock );
173         ClockNewRef( p_pgrm, i_clock, p_pgrm->last_pts > mdate() ?
174                                       p_pgrm->last_pts : mdate() );
175
176         if( p_input->stream.i_new_status == PAUSE_S )
177         {
178             /* PAUSE_S undoes the pause state: Return to old state. */
179             vlc_mutex_lock( &p_input->stream.control.control_lock );
180             p_input->stream.control.i_status = i_old_status;
181             vlc_mutex_unlock( &p_input->stream.control.control_lock );
182
183             p_input->stream.i_new_status = UNDEF_S;
184             p_input->stream.i_new_rate = UNDEF_S;
185         }
186
187         /* We handle i_new_status != PAUSE_S below... */
188
189         i_return_value = PAUSE_S;
190     }
191
192     if( p_input->stream.i_new_status != UNDEF_S )
193     {
194         vlc_mutex_lock( &p_input->stream.control.control_lock );
195
196         p_input->stream.control.i_status = p_input->stream.i_new_status;
197
198         ClockNewRef( p_pgrm, i_clock,
199                      ClockToSysdate( p_input, p_pgrm, i_clock ) );
200
201         if( p_input->stream.control.i_status == PLAYING_S )
202         {
203             p_input->stream.control.i_rate = DEFAULT_RATE;
204             p_input->stream.control.b_mute = 0;
205         }
206         else
207         {
208             p_input->stream.control.i_rate = p_input->stream.i_new_rate;
209             p_input->stream.control.b_mute = 1;
210
211             /* Feed the audio decoders with a NULL packet to avoid
212              * discontinuities. */
213             input_EscapeAudioDiscontinuity( p_input );
214         }
215
216         val.i_int = p_input->stream.control.i_rate;
217         var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
218
219         val.i_int = p_input->stream.control.i_status;
220         var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
221
222         p_input->stream.i_new_status = UNDEF_S;
223         p_input->stream.i_new_rate = UNDEF_S;
224
225         vlc_mutex_unlock( &p_input->stream.control.control_lock );
226     }
227
228     vlc_mutex_unlock( &p_input->stream.stream_lock );
229
230     return( i_return_value );
231 #endif
232     return UNDEF_S;
233 }
234 #endif
235
236 /*****************************************************************************
237  * input_ClockSetPCR: manages a clock reference
238  *****************************************************************************/
239 void input_ClockSetPCR( input_thread_t *p_input,
240                         input_clock_t *cl, mtime_t i_clock )
241 {
242     if( ( cl->i_synchro_state != SYNCHRO_OK ) ||
243         ( i_clock == 0 && cl->last_cr != 0 ) )
244     {
245         /* Feed synchro with a new reference point. */
246         ClockNewRef( cl, i_clock,
247                      cl->last_pts + CR_MEAN_PTS_GAP > mdate() ?
248                      cl->last_pts + CR_MEAN_PTS_GAP : mdate() );
249         cl->i_synchro_state = SYNCHRO_OK;
250
251         if( p_input->b_can_pace_control && cl->b_master )
252         {
253             cl->last_cr = i_clock;
254             if( !p_input->b_out_pace_control )
255             {
256                 mtime_t i_wakeup = ClockToSysdate( p_input, cl, i_clock );
257                 while( (i_wakeup - mdate()) / CLOCK_FREQ > 1 )
258                 {
259                     msleep( CLOCK_FREQ );
260                     if( p_input->b_die ) i_wakeup = mdate();
261                 }
262                 mwait( i_wakeup );
263             }
264         }
265         else
266         {
267             cl->last_cr = 0;
268             cl->last_sysdate = 0;
269             cl->delta_cr = 0;
270             cl->i_delta_cr_residue = 0;
271         }
272     }
273     else
274     {
275         if ( cl->last_cr != 0 &&
276                (    (cl->last_cr - i_clock) > CR_MAX_GAP
277                  || (cl->last_cr - i_clock) < - CR_MAX_GAP ) )
278         {
279             /* Stream discontinuity, for which we haven't received a
280              * warning from the stream control facilities (dd-edited
281              * stream ?). */
282             msg_Warn( p_input, "clock gap, unexpected stream discontinuity" );
283             input_ClockInit( cl, cl->b_master, cl->i_cr_average );
284             /* FIXME needed ? */
285 #if 0
286             input_EscapeDiscontinuity( p_input );
287 #endif
288         }
289
290         cl->last_cr = i_clock;
291
292         if( p_input->b_can_pace_control && cl->b_master )
293         {
294             /* Wait a while before delivering the packets to the decoder.
295              * In case of multiple programs, we arbitrarily follow the
296              * clock of the selected program. */
297             if( !p_input->b_out_pace_control )
298             {
299                 mtime_t i_wakeup = ClockToSysdate( p_input, cl, i_clock );
300                 while( (i_wakeup - mdate()) / CLOCK_FREQ > 1 )
301                 {
302                     msleep( CLOCK_FREQ );
303                     if( p_input->b_die ) i_wakeup = mdate();
304                 }
305                 mwait( i_wakeup );
306             }
307             /* FIXME Not needed anymore ? */
308 #if 0
309             /* Now take into account interface changes. */
310             input_ClockManageControl( p_input, cl, i_clock );
311 #endif
312         }
313         else if ( mdate() - cl->last_sysdate > 200000 )
314         {
315             /* Smooth clock reference variations. */
316             mtime_t i_extrapoled_clock = ClockCurrent( p_input, cl );
317             mtime_t delta_cr;
318
319             /* Bresenham algorithm to smooth variations. */
320             delta_cr = ( cl->delta_cr * (cl->i_cr_average - 1)
321                                + ( i_extrapoled_clock - i_clock )
322                                + cl->i_delta_cr_residue )
323                            / cl->i_cr_average;
324             cl->i_delta_cr_residue = ( cl->delta_cr * (cl->i_cr_average - 1)
325                                        + ( i_extrapoled_clock - i_clock )
326                                        + cl->i_delta_cr_residue )
327                                     % cl->i_cr_average;
328             cl->delta_cr = delta_cr;
329             cl->last_sysdate = mdate();
330         }
331     }
332 }
333
334 /*****************************************************************************
335  * input_ClockGetTS: manages a PTS or DTS
336  *****************************************************************************/
337 mtime_t input_ClockGetTS( input_thread_t * p_input,
338                           input_clock_t *cl, mtime_t i_ts )
339 {
340     if( cl->i_synchro_state != SYNCHRO_OK )
341         return 0;
342
343     cl->last_pts = ClockToSysdate( p_input, cl, i_ts + cl->delta_cr );
344     return cl->last_pts + p_input->i_pts_delay;
345 }
346