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