]> git.sesse.net Git - vlc/blob - src/input/input_clock.c
8073397bbaf7d90cc629cbbe75ad09c0e102361d
[vlc] / src / input / input_clock.c
1 /*****************************************************************************
2  * input_clock.c: Clock/System date convertions, stream management
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: input_clock.c,v 1.10 2001/04/26 03:55:44 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 "defs.h"
28
29 #include <string.h>                                    /* memcpy(), memset() */
30
31 #include "config.h"
32 #include "common.h"
33 #include "threads.h"
34 #include "mtime.h"
35 #include "intf_msg.h"
36
37 #include "stream_control.h"
38 #include "input_ext-intf.h"
39 #include "input_ext-dec.h"
40
41 #include "input.h"
42
43 /*
44  * DISCUSSION : SYNCHRONIZATION METHOD
45  *
46  * In some cases we can impose the pace of reading (when reading from a
47  * file or a pipe), and for the synchronization we simply sleep() until
48  * it is time to deliver the packet to the decoders. When reading from
49  * the network, we must be read at the same pace as the server writes,
50  * otherwise the kernel's buffer will trash packets. The risk is now to
51  * overflow the input buffers in case the server goes too fast, that is
52  * why we do these calculations :
53  *
54  * We compute a mean for the pcr because we want to eliminate the
55  * network jitter and keep the low frequency variations. The mean is
56  * in fact a low pass filter and the jitter is a high frequency signal
57  * that is why it is eliminated by the filter/average.
58  *
59  * The low frequency variations enable us to synchronize the client clock
60  * with the server clock because they represent the time variation between
61  * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
62  * the presentation dates for the audio and video frames. With those dates
63  * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
64  * as it is sent by the server and so we keep the synchronization between
65  * the server and the client.
66  *
67  * It is a very important matter if you want to avoid underflow or overflow
68  * in all the FIFOs, but it may be not enough.
69  */
70
71 /*****************************************************************************
72  * Constants
73  *****************************************************************************/
74
75 /* Maximum number of samples used to compute the dynamic average value.
76  * We use the following formula :
77  * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
78  */
79 #define CR_MAX_AVERAGE_COUNTER 40
80
81 /* Maximum gap allowed between two CRs. */
82 #define CR_MAX_GAP 1000000
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                         / (mtime_t)27
98                         / (mtime_t)DEFAULT_RATE
99                         + (mtime_t)p_pgrm->sysdate_ref;
100     }
101
102     return( i_sysdate );
103 }
104
105 /*****************************************************************************
106  * ClockCurrent: converts current system date to clock units
107  *****************************************************************************
108  * Caution : the synchro state must be SYNCHRO_OK for this to operate.
109  *****************************************************************************/
110 static mtime_t ClockCurrent( input_thread_t * p_input,
111                              pgrm_descriptor_t * p_pgrm )
112 {
113     return( (mdate() - p_pgrm->sysdate_ref) * 27 * DEFAULT_RATE
114              / p_input->stream.control.i_rate / 300
115              + p_pgrm->cr_ref );
116 }
117
118 /*****************************************************************************
119  * ClockNewRef: writes a new clock reference
120  *****************************************************************************/
121 static void ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
122                          mtime_t i_clock, mtime_t i_sysdate )
123 {
124     p_pgrm->cr_ref = i_clock;
125     p_pgrm->sysdate_ref = 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->cr_ref = 0;
136     p_pgrm->sysdate_ref = 0;
137     p_pgrm->delta_cr = 0;
138     p_pgrm->c_average_count = 0;
139 }
140
141 /*****************************************************************************
142  * input_ClockManageRef: manages a clock reference
143  *****************************************************************************/
144 void input_ClockManageRef( input_thread_t * p_input,
145                            pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
146 {
147     if( p_pgrm->i_synchro_state != SYNCHRO_OK )
148     {
149         /* Feed synchro with a new reference point. */
150         ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
151         p_pgrm->i_synchro_state = SYNCHRO_OK;
152     }
153     else
154     {
155         if ( p_pgrm->last_cr != 0 &&
156                (    (p_pgrm->last_cr - i_clock) > CR_MAX_GAP
157                  || (p_pgrm->last_cr - i_clock) < - CR_MAX_GAP ) )
158         {
159             /* Stream discontinuity, for which we haven't received a
160              * warning from the stream control facilities (dd-edited
161              * stream ?). */
162             intf_WarnMsg( 3, "Clock gap, unexpected stream discontinuity" );
163             input_ClockInit( p_pgrm );
164             p_pgrm->i_synchro_state = SYNCHRO_START;
165             input_EscapeDiscontinuity( p_input, p_pgrm );
166         }
167
168         p_pgrm->last_cr = i_clock;
169
170         if( p_input->stream.b_pace_control
171              && p_input->stream.pp_programs[0] == p_pgrm )
172         {
173             /* Wait a while before delivering the packets to the decoder.
174              * In case of multiple programs, we arbitrarily follow the
175              * clock of the first program. */
176             mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
177
178             /* Now take into account interface changes. */
179             vlc_mutex_lock( &p_input->stream.stream_lock );
180
181             if( p_input->stream.i_new_status == PAUSE_S )
182             {
183                 int i_old_status;
184                 vlc_mutex_lock( &p_input->stream.control.control_lock );
185                 i_old_status = p_input->stream.control.i_status;
186
187                 p_input->stream.control.i_status = PAUSE_S;
188                 vlc_cond_wait( &p_input->stream.stream_wait,
189                                &p_input->stream.stream_lock );
190                 ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
191
192                 if( p_input->stream.i_new_status == PAUSE_S )
193                 { 
194                     /* PAUSE_S undoes the pause state: Return to old state. */
195                     p_input->stream.control.i_status = i_old_status;
196                     p_input->stream.i_new_status = UNDEF_S;
197                     p_input->stream.i_new_rate = UNDEF_S;
198                 }
199
200                 /*  We handle i_new_status != PAUSE_S below... */
201                 vlc_mutex_unlock( &p_input->stream.control.control_lock );
202             }
203
204             if( p_input->stream.i_new_status != UNDEF_S )
205             {
206                 vlc_mutex_lock( &p_input->stream.control.control_lock );
207
208                 p_input->stream.control.i_status = p_input->stream.i_new_status;
209
210                 ClockNewRef( p_input, p_pgrm, i_clock,
211                            ClockToSysdate( p_input, p_pgrm, i_clock ) );
212
213                 if( p_input->stream.control.i_status == PLAYING_S )
214                 {
215                     p_input->stream.control.i_rate = DEFAULT_RATE;
216                     p_input->stream.control.b_mute = 0;
217                 }
218                 else
219                 {
220                     p_input->stream.control.i_rate = p_input->stream.i_new_rate;
221                     p_input->stream.control.b_mute = 1;
222
223                     /* Feed the audio decoders with a NULL packet to avoid
224                      * discontinuities. */
225                     input_EscapeAudioDiscontinuity( p_input, p_pgrm );
226                 }
227
228                 p_input->stream.i_new_status = UNDEF_S;
229                 p_input->stream.i_new_rate = UNDEF_S;
230
231                 vlc_mutex_unlock( &p_input->stream.control.control_lock );
232             }
233
234             vlc_mutex_unlock( &p_input->stream.stream_lock );
235         }
236         else
237         {
238             /* Smooth clock reference variations. */
239             mtime_t     i_extrapoled_clock = ClockCurrent( p_input, p_pgrm );
240
241             /* Bresenham algorithm to smooth variations. */
242             if( p_pgrm->c_average_count == CR_MAX_AVERAGE_COUNTER )
243             {
244                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
245                                         * (CR_MAX_AVERAGE_COUNTER - 1)
246                                       + i_extrapoled_clock )
247                                     / CR_MAX_AVERAGE_COUNTER;
248             }
249             else
250             {
251                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
252                                         * p_pgrm->c_average_count
253                                       + i_extrapoled_clock )
254                                     / (p_pgrm->c_average_count + 1);
255                 p_pgrm->c_average_count++;
256             }
257         }
258     }
259 }
260
261 /*****************************************************************************
262  * input_ClockGetTS: manages a PTS or DTS
263  *****************************************************************************/
264 mtime_t input_ClockGetTS( input_thread_t * p_input,
265                           pgrm_descriptor_t * p_pgrm, mtime_t i_ts )
266 {
267     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
268     {
269         return( ClockToSysdate( p_input, p_pgrm, i_ts + p_pgrm->delta_cr )
270                  + DEFAULT_PTS_DELAY );
271     }
272     else
273     {
274         return 0;
275     }
276 }
277