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