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