]> git.sesse.net Git - vlc/blob - src/input/input_clock.c
Coding-style updates (whitespace related)
[vlc] / src / input / input_clock.c
1 /*****************************************************************************
2  * input_clock.c: Clock/System date convertions, stream management
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
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 /* p_input->i_cr_average : Maximum number of samples used to compute the
65  * dynamic average value.
66  * We use the following formula :
67  * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
68  */
69
70 static void ClockNewRef( pgrm_descriptor_t * p_pgrm,
71                          mtime_t i_clock, mtime_t i_sysdate );
72
73 /*****************************************************************************
74  * Constants
75  *****************************************************************************/
76
77 /* Maximum gap allowed between two CRs. */
78 #define CR_MAX_GAP 2000000
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
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( 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->last_pts = 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_ClockManageControl: handles the messages from the interface
144  *****************************************************************************
145  * Returns UNDEF_S if nothing happened, PAUSE_S if the stream was paused
146  *****************************************************************************/
147 int input_ClockManageControl( input_thread_t * p_input,
148                                pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
149 {
150     vlc_value_t val;
151     int i_return_value = UNDEF_S;
152
153     vlc_mutex_lock( &p_input->stream.stream_lock );
154
155     if( p_input->stream.i_new_status == PAUSE_S )
156     {
157         int i_old_status;
158
159         vlc_mutex_lock( &p_input->stream.control.control_lock );
160         i_old_status = p_input->stream.control.i_status;
161         p_input->stream.control.i_status = PAUSE_S;
162         vlc_mutex_unlock( &p_input->stream.control.control_lock );
163
164         vlc_cond_wait( &p_input->stream.stream_wait,
165                        &p_input->stream.stream_lock );
166         ClockNewRef( p_pgrm, i_clock, p_pgrm->last_pts > mdate() ?
167                                       p_pgrm->last_pts : mdate() );
168
169         if( p_input->stream.i_new_status == PAUSE_S )
170         {
171             /* PAUSE_S undoes the pause state: Return to old state. */
172             vlc_mutex_lock( &p_input->stream.control.control_lock );
173             p_input->stream.control.i_status = i_old_status;
174             vlc_mutex_unlock( &p_input->stream.control.control_lock );
175
176             p_input->stream.i_new_status = UNDEF_S;
177             p_input->stream.i_new_rate = UNDEF_S;
178         }
179
180         /* We handle i_new_status != PAUSE_S below... */
181
182         i_return_value = PAUSE_S;
183     }
184
185     if( p_input->stream.i_new_status != UNDEF_S )
186     {
187         vlc_mutex_lock( &p_input->stream.control.control_lock );
188
189         p_input->stream.control.i_status = p_input->stream.i_new_status;
190
191         ClockNewRef( p_pgrm, i_clock,
192                      ClockToSysdate( p_input, p_pgrm, i_clock ) );
193
194         if( p_input->stream.control.i_status == PLAYING_S )
195         {
196             p_input->stream.control.i_rate = DEFAULT_RATE;
197             p_input->stream.control.b_mute = 0;
198         }
199         else
200         {
201             p_input->stream.control.i_rate = p_input->stream.i_new_rate;
202             p_input->stream.control.b_mute = 1;
203
204             /* Feed the audio decoders with a NULL packet to avoid
205              * discontinuities. */
206             input_EscapeAudioDiscontinuity( p_input );
207         }
208
209         val.i_int = p_input->stream.control.i_rate;
210         var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
211
212         val.i_int = p_input->stream.control.i_status;
213         var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
214
215         p_input->stream.i_new_status = UNDEF_S;
216         p_input->stream.i_new_rate = UNDEF_S;
217
218         vlc_mutex_unlock( &p_input->stream.control.control_lock );
219     }
220
221     vlc_mutex_unlock( &p_input->stream.stream_lock );
222
223     return( i_return_value );
224 }
225
226 /*****************************************************************************
227  * input_ClockManageRef: manages a clock reference
228  *****************************************************************************/
229 void input_ClockManageRef( input_thread_t * p_input,
230                            pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
231 {
232     /* take selected program if none specified */
233     if( !p_pgrm )
234     {
235         p_pgrm = p_input->stream.p_selected_program;
236     }
237
238     if( ( p_pgrm->i_synchro_state != SYNCHRO_OK ) ||
239         ( i_clock == 0 && p_pgrm->last_cr != 0 ) )
240     {
241         /* Feed synchro with a new reference point. */
242         ClockNewRef( p_pgrm, i_clock,
243                      p_pgrm->last_pts + CR_MEAN_PTS_GAP > mdate() ?
244                      p_pgrm->last_pts + CR_MEAN_PTS_GAP : mdate() );
245         p_pgrm->i_synchro_state = SYNCHRO_OK;
246
247         if( p_input->stream.b_pace_control
248              && p_input->stream.p_selected_program == p_pgrm )
249         {
250             p_pgrm->last_cr = i_clock;
251             if( !p_input->b_out_pace_control )
252             {
253                 mtime_t i_wakeup = ClockToSysdate( p_input, p_pgrm, i_clock );
254                 while( (i_wakeup - mdate()) / CLOCK_FREQ > 1 )
255                 {
256                     msleep( CLOCK_FREQ );
257                     if( p_input->b_die ) i_wakeup = mdate();
258                 }
259                 mwait( i_wakeup );
260             }
261         }
262         else
263         {
264             p_pgrm->last_cr = 0;
265             p_pgrm->delta_cr = 0;
266             p_pgrm->c_average_count = 0;
267         }
268     }
269     else
270     {
271         if ( p_pgrm->last_cr != 0 &&
272                (    (p_pgrm->last_cr - i_clock) > CR_MAX_GAP
273                  || (p_pgrm->last_cr - i_clock) < - CR_MAX_GAP ) )
274         {
275             /* Stream discontinuity, for which we haven't received a
276              * warning from the stream control facilities (dd-edited
277              * stream ?). */
278             msg_Warn( p_input, "clock gap, unexpected stream discontinuity" );
279             input_ClockInit( p_pgrm );
280             p_pgrm->i_synchro_state = SYNCHRO_START;
281             input_EscapeDiscontinuity( p_input );
282         }
283
284         p_pgrm->last_cr = i_clock;
285
286         if( p_input->stream.b_pace_control
287              && p_input->stream.p_selected_program == p_pgrm )
288         {
289             /* Wait a while before delivering the packets to the decoder.
290              * In case of multiple programs, we arbitrarily follow the
291              * clock of the selected program. */
292             if( !p_input->b_out_pace_control )
293             {
294                 mtime_t i_wakeup = ClockToSysdate( p_input, p_pgrm, i_clock );
295                 while( (i_wakeup - mdate()) / CLOCK_FREQ > 1 )
296                 {
297                     msleep( CLOCK_FREQ );
298                     if( p_input->b_die ) i_wakeup = mdate();
299                 }
300                 mwait( i_wakeup );
301             }
302
303             /* Now take into account interface changes. */
304             input_ClockManageControl( p_input, p_pgrm, i_clock );
305         }
306         else
307         {
308             /* Smooth clock reference variations. */
309             mtime_t     i_extrapoled_clock = ClockCurrent( p_input, p_pgrm );
310
311             /* Bresenham algorithm to smooth variations. */
312             if( p_pgrm->c_average_count == p_input->i_cr_average )
313             {
314                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
315                                         * (p_input->i_cr_average - 1)
316                                       + ( i_extrapoled_clock - i_clock ) )
317                                     / p_input->i_cr_average;
318             }
319             else
320             {
321                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
322                                         * p_pgrm->c_average_count
323                                       + ( i_extrapoled_clock - i_clock ) )
324                                     / (p_pgrm->c_average_count + 1);
325                 p_pgrm->c_average_count++;
326             }
327         }
328     }
329 }
330
331 /*****************************************************************************
332  * input_ClockGetTS: manages a PTS or DTS
333  *****************************************************************************/
334 mtime_t input_ClockGetTS( input_thread_t * p_input,
335                           pgrm_descriptor_t * p_pgrm, mtime_t i_ts )
336 {
337     /* take selected program if none specified */
338     if( !p_pgrm )
339     {
340         p_pgrm = p_input->stream.p_selected_program;
341     }
342
343     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
344     {
345         p_pgrm->last_pts = ClockToSysdate( p_input, p_pgrm,
346                                            i_ts + p_pgrm->delta_cr );
347         return( p_pgrm->last_pts + p_input->i_pts_delay );
348     }
349     else
350     {
351         return 0;
352     }
353 }
354