]> git.sesse.net Git - vlc/blob - src/input/input_clock.c
* ALL: the first libvlc commit.
[vlc] / src / input / input_clock.c
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.32 2002/06/01 12:32:01 sam Exp $
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 <string.h>                                    /* memcpy(), memset() */
28 #include <sys/types.h>                                              /* off_t */
29
30 #include <vlc/vlc.h>
31
32 #include "stream_control.h"
33 #include "input_ext-intf.h"
34 #include "input_ext-dec.h"
35 #include "input_ext-plugins.h"
36
37 /*
38  * DISCUSSION : SYNCHRONIZATION METHOD
39  *
40  * In some cases we can impose the pace of reading (when reading from a
41  * file or a pipe), and for the synchronization we simply sleep() until
42  * it is time to deliver the packet to the decoders. When reading from
43  * the network, we must be read at the same pace as the server writes,
44  * otherwise the kernel's buffer will trash packets. The risk is now to
45  * overflow the input buffers in case the server goes too fast, that is
46  * why we do these calculations :
47  *
48  * We compute a mean for the pcr because we want to eliminate the
49  * network jitter and keep the low frequency variations. The mean is
50  * in fact a low pass filter and the jitter is a high frequency signal
51  * that is why it is eliminated by the filter/average.
52  *
53  * The low frequency variations enable us to synchronize the client clock
54  * with the server clock because they represent the time variation between
55  * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
56  * the presentation dates for the audio and video frames. With those dates
57  * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
58  * as it is sent by the server and so we keep the synchronization between
59  * the server and the client.
60  *
61  * It is a very important matter if you want to avoid underflow or overflow
62  * in all the FIFOs, but it may be not enough.
63  */
64
65 /*****************************************************************************
66  * Constants
67  *****************************************************************************/
68
69 /* Maximum number of samples used to compute the dynamic average value.
70  * We use the following formula :
71  * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
72  */
73 #define CR_MAX_AVERAGE_COUNTER 40
74
75 /* Maximum gap allowed between two CRs. */
76 #define CR_MAX_GAP 1000000
77
78 /*****************************************************************************
79  * ClockToSysdate: converts a movie clock to system date
80  *****************************************************************************/
81 static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
82                          mtime_t i_clock, mtime_t i_sysdate );
83 static mtime_t ClockToSysdate( input_thread_t * p_input,
84                                pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
85 {
86     mtime_t     i_sysdate = 0;
87
88     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
89     {
90         i_sysdate = (mtime_t)(i_clock - p_pgrm->cr_ref) 
91                         * (mtime_t)p_input->stream.control.i_rate
92                         * (mtime_t)300;
93         i_sysdate /= 27;
94         i_sysdate /= 1000;
95         i_sysdate += (mtime_t)p_pgrm->sysdate_ref;
96     }
97
98     return( i_sysdate );
99 }
100
101 /*****************************************************************************
102  * ClockCurrent: converts current system date to clock units
103  *****************************************************************************
104  * Caution : the synchro state must be SYNCHRO_OK for this to operate.
105  *****************************************************************************/
106 static mtime_t ClockCurrent( input_thread_t * p_input,
107                              pgrm_descriptor_t * p_pgrm )
108 {
109     return( (mdate() - p_pgrm->sysdate_ref) * 27 * DEFAULT_RATE
110              / p_input->stream.control.i_rate / 300
111              + p_pgrm->cr_ref );
112 }
113
114 /*****************************************************************************
115  * ClockNewRef: writes a new clock reference
116  *****************************************************************************/
117 static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
118                          mtime_t i_clock, mtime_t i_sysdate )
119 {
120     p_pgrm->cr_ref = i_clock;
121     /* this is actually a kludge, but it gives better results when scr
122     * is zero in DVDs: we are 3-4 ms in advance instead of sometimes
123     * 100ms late  */
124     p_pgrm->sysdate_ref = ( p_pgrm->last_syscr && !i_clock )
125                           ? p_pgrm->last_syscr
126                           : i_sysdate ;
127 }
128
129 /*****************************************************************************
130  * input_ClockInit: reinitializes the clock reference after a stream
131  *                  discontinuity
132  *****************************************************************************/
133 void input_ClockInit( pgrm_descriptor_t * p_pgrm )
134 {
135     p_pgrm->last_cr = 0;
136     p_pgrm->last_syscr = 0;
137     p_pgrm->cr_ref = 0;
138     p_pgrm->sysdate_ref = 0;
139     p_pgrm->delta_cr = 0;
140     p_pgrm->c_average_count = 0;
141 }
142
143 /*****************************************************************************
144  * input_ClockManageControl: handles the messages from the interface
145  *****************************************************************************
146  * Returns UNDEF_S if nothing happened, PAUSE_S if the stream was paused
147  *****************************************************************************/
148 int input_ClockManageControl( input_thread_t * p_input,
149                                pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
150 {
151     int i_return_value = UNDEF_S;
152
153     vlc_mutex_lock( &p_input->stream.stream_lock );
154
155     if( p_input->stream.i_new_status == PAUSE_S )
156     {
157         int i_old_status;
158
159         vlc_mutex_lock( &p_input->stream.control.control_lock );
160         i_old_status = p_input->stream.control.i_status;
161         p_input->stream.control.i_status = PAUSE_S;
162         vlc_mutex_unlock( &p_input->stream.control.control_lock );
163         
164         vlc_cond_wait( &p_input->stream.stream_wait,
165                        &p_input->stream.stream_lock );
166         p_pgrm->last_syscr = 0;
167         ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
168
169         if( p_input->stream.i_new_status == PAUSE_S )
170         { 
171             /* PAUSE_S undoes the pause state: Return to old state. */
172             vlc_mutex_lock( &p_input->stream.control.control_lock );
173             p_input->stream.control.i_status = i_old_status;
174             vlc_mutex_unlock( &p_input->stream.control.control_lock );
175             
176             p_input->stream.i_new_status = UNDEF_S;
177             p_input->stream.i_new_rate = UNDEF_S;
178         }
179
180         /* We handle i_new_status != PAUSE_S below... */
181
182         i_return_value = PAUSE_S;
183     }
184
185     if( p_input->stream.i_new_status != UNDEF_S )
186     {
187         vlc_mutex_lock( &p_input->stream.control.control_lock );
188
189         p_input->stream.control.i_status = p_input->stream.i_new_status;
190
191         ClockNewRef( p_input, p_pgrm, i_clock,
192                      ClockToSysdate( p_input, p_pgrm, i_clock ) );
193
194         if( p_input->stream.control.i_status == PLAYING_S )
195         {
196             p_input->stream.control.i_rate = DEFAULT_RATE;
197             p_input->stream.control.b_mute = 0;
198         }
199         else
200         {
201             p_input->stream.control.i_rate = p_input->stream.i_new_rate;
202             p_input->stream.control.b_mute = 1;
203
204             /* Feed the audio decoders with a NULL packet to avoid
205              * discontinuities. */
206             input_EscapeAudioDiscontinuity( p_input );
207         }
208
209         p_input->stream.i_new_status = UNDEF_S;
210         p_input->stream.i_new_rate = UNDEF_S;
211
212         vlc_mutex_unlock( &p_input->stream.control.control_lock );
213     }
214
215     vlc_mutex_unlock( &p_input->stream.stream_lock );
216
217     return( i_return_value );
218 }
219
220 /*****************************************************************************
221  * input_ClockManageRef: manages a clock reference
222  *****************************************************************************/
223 void input_ClockManageRef( input_thread_t * p_input,
224                            pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
225 {
226     /* take selected program if none specified */
227     if( !p_pgrm )
228     {
229         p_pgrm = p_input->stream.p_selected_program;
230     }
231
232     if( ( p_pgrm->i_synchro_state != SYNCHRO_OK ) || ( i_clock == 0 ) )
233     {
234         /* Feed synchro with a new reference point. */
235         ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
236         p_pgrm->i_synchro_state = SYNCHRO_OK;
237
238         if( p_input->stream.b_pace_control
239              && p_input->stream.p_selected_program == p_pgrm )
240         {
241             p_pgrm->last_cr = i_clock;
242             mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
243         }
244         else
245         {
246             p_pgrm->last_cr = 0;
247             p_pgrm->last_syscr = 0;
248             p_pgrm->delta_cr = 0;
249             p_pgrm->c_average_count = 0;
250         }
251     }
252     else
253     {
254         if ( p_pgrm->last_cr != 0 &&
255                (    (p_pgrm->last_cr - i_clock) > CR_MAX_GAP
256                  || (p_pgrm->last_cr - i_clock) < - CR_MAX_GAP ) )
257         {
258             /* Stream discontinuity, for which we haven't received a
259              * warning from the stream control facilities (dd-edited
260              * stream ?). */
261             msg_Warn( p_input, "clock gap, unexpected stream discontinuity" );
262             input_ClockInit( p_pgrm );
263             p_pgrm->i_synchro_state = SYNCHRO_START;
264             input_EscapeDiscontinuity( p_input );
265         }
266
267         p_pgrm->last_cr = i_clock;
268
269         if( p_input->stream.b_pace_control
270              && p_input->stream.p_selected_program == p_pgrm )
271         {
272             /* We remember the last system date to be able to restart
273              * the synchro we statistically better continuity, after 
274              * a zero scr */
275             p_pgrm->last_syscr = ClockToSysdate( p_input, p_pgrm, i_clock );
276             
277             /* Wait a while before delivering the packets to the decoder.
278              * In case of multiple programs, we arbitrarily follow the
279              * clock of the first program. */
280             mwait( p_pgrm->last_syscr );
281
282             /* Now take into account interface changes. */
283             input_ClockManageControl( p_input, p_pgrm, i_clock );
284         }
285         else
286         {
287             /* Smooth clock reference variations. */
288             mtime_t     i_extrapoled_clock = ClockCurrent( p_input, p_pgrm );
289
290             /* Bresenham algorithm to smooth variations. */
291             if( p_pgrm->c_average_count == CR_MAX_AVERAGE_COUNTER )
292             {
293                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
294                                         * (CR_MAX_AVERAGE_COUNTER - 1)
295                                       + ( i_extrapoled_clock - i_clock ) )
296                                     / CR_MAX_AVERAGE_COUNTER;
297             }
298             else
299             {
300                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
301                                         * p_pgrm->c_average_count
302                                       + ( i_extrapoled_clock - i_clock ) )
303                                     / (p_pgrm->c_average_count + 1);
304                 p_pgrm->c_average_count++;
305             }
306         }
307     }
308 }
309
310 /*****************************************************************************
311  * input_ClockGetTS: manages a PTS or DTS
312  *****************************************************************************/
313 mtime_t input_ClockGetTS( input_thread_t * p_input,
314                           pgrm_descriptor_t * p_pgrm, mtime_t i_ts )
315 {
316     /* take selected program if none specified */
317     if( !p_pgrm )
318     {
319         p_pgrm = p_input->stream.p_selected_program;
320     }
321
322     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
323     {
324         return( ClockToSysdate( p_input, p_pgrm, i_ts + p_pgrm->delta_cr )
325                  + DEFAULT_PTS_DELAY
326                  + (p_input->p_vlc->i_desync > 0
327                        ? p_input->p_vlc->i_desync : 0) );
328     }
329     else
330     {
331         return 0;
332     }
333 }
334