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