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