]> git.sesse.net Git - vlc/blob - src/input/clock.c
Removed the ugly input clock.c sleep.
[vlc] / src / input / clock.c
1 /*****************************************************************************
2  * input_clock.c: Clock/System date convertions, stream management
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32
33 #include "input_internal.h"
34
35 /*
36  * DISCUSSION : SYNCHRONIZATION METHOD
37  *
38  * In some cases we can impose the pace of reading (when reading from a
39  * file or a pipe), and for the synchronization we simply sleep() until
40  * it is time to deliver the packet to the decoders. When reading from
41  * the network, we must be read at the same pace as the server writes,
42  * otherwise the kernel's buffer will trash packets. The risk is now to
43  * overflow the input buffers in case the server goes too fast, that is
44  * why we do these calculations :
45  *
46  * We compute a mean for the pcr because we want to eliminate the
47  * network jitter and keep the low frequency variations. The mean is
48  * in fact a low pass filter and the jitter is a high frequency signal
49  * that is why it is eliminated by the filter/average.
50  *
51  * The low frequency variations enable us to synchronize the client clock
52  * with the server clock because they represent the time variation between
53  * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
54  * the presentation dates for the audio and video frames. With those dates
55  * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
56  * as it is sent by the server and so we keep the synchronization between
57  * the server and the client.
58  *
59  * It is a very important matter if you want to avoid underflow or overflow
60  * in all the FIFOs, but it may be not enough.
61  */
62
63 /* p_input->p->i_cr_average : Maximum number of samples used to compute the
64  * dynamic average value.
65  * We use the following formula :
66  * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
67  */
68
69 enum /* Synchro states */
70 {
71     SYNCHRO_OK     = 0,
72     SYNCHRO_START  = 1,
73     SYNCHRO_REINIT = 2,
74 };
75
76 static void ClockNewRef( input_clock_t * p_pgrm,
77                          mtime_t i_clock, mtime_t i_sysdate );
78
79 /*****************************************************************************
80  * Constants
81  *****************************************************************************/
82
83 /* Maximum gap allowed between two CRs. */
84 #define CR_MAX_GAP (INT64_C(2000000)*100/9)
85
86 /* Latency introduced on DVDs with CR == 0 on chapter change - this is from
87  * my dice --Meuuh */
88 #define CR_MEAN_PTS_GAP 300000
89
90 /*****************************************************************************
91  * ClockToSysdate: converts a movie clock to system date
92  *****************************************************************************/
93 static mtime_t ClockToSysdate( input_clock_t *cl, mtime_t i_clock )
94 {
95     if( cl->i_synchro_state != SYNCHRO_OK )
96         return 0;
97
98     return (i_clock - cl->cr_ref) * cl->i_rate / INPUT_RATE_DEFAULT +
99            cl->sysdate_ref;
100 }
101
102 /*****************************************************************************
103  * ClockCurrent: converts current system date to clock units
104  *****************************************************************************
105  * Caution : the synchro state must be SYNCHRO_OK for this to operate.
106  *****************************************************************************/
107 static mtime_t ClockCurrent( input_clock_t *cl )
108 {
109     return (mdate() - cl->sysdate_ref) * INPUT_RATE_DEFAULT / cl->i_rate +
110            cl->cr_ref;
111 }
112
113 /*****************************************************************************
114  * ClockNewRef: writes a new clock reference
115  *****************************************************************************/
116 static void ClockNewRef( input_clock_t *cl,
117                          mtime_t i_clock, mtime_t i_sysdate )
118 {
119     cl->cr_ref = i_clock;
120     cl->sysdate_ref = i_sysdate ;
121 }
122
123 /*****************************************************************************
124  * input_ClockInit: reinitializes the clock reference after a stream
125  *                  discontinuity
126  *****************************************************************************/
127 void input_ClockInit( input_clock_t *cl, bool b_master, int i_cr_average, int i_rate )
128 {
129     cl->i_synchro_state = SYNCHRO_START;
130
131     cl->last_cr = 0;
132     cl->last_pts = 0;
133     cl->last_sysdate = 0;
134     cl->cr_ref = 0;
135     cl->sysdate_ref = 0;
136     cl->delta_cr = 0;
137     cl->i_delta_cr_residue = 0;
138     cl->i_rate = i_rate;
139
140     cl->i_cr_average = i_cr_average;
141
142     cl->b_master = b_master;
143 }
144
145 /*****************************************************************************
146  * input_ClockSetPCR: manages a clock reference
147  *****************************************************************************/
148 void input_ClockSetPCR( input_thread_t *p_input,
149                         input_clock_t *cl, mtime_t i_clock )
150 {
151     const bool b_synchronize = p_input->b_can_pace_control && cl->b_master;
152     const mtime_t i_mdate = mdate();
153
154     if( ( cl->i_synchro_state != SYNCHRO_OK ) ||
155         ( i_clock == 0 && cl->last_cr != 0 ) )
156     {
157         /* Feed synchro with a new reference point. */
158         ClockNewRef( cl, i_clock,
159                          __MAX( cl->last_pts + CR_MEAN_PTS_GAP, i_mdate ) );
160         cl->i_synchro_state = SYNCHRO_OK;
161
162         if( !b_synchronize )
163         {
164             cl->delta_cr = 0;
165             cl->i_delta_cr_residue = 0;
166             cl->last_update = 0;
167         }
168     }
169     else if ( cl->last_cr != 0 &&
170               ( (cl->last_cr - i_clock) > CR_MAX_GAP ||
171                 (cl->last_cr - i_clock) < - CR_MAX_GAP ) )
172     {
173         /* Stream discontinuity, for which we haven't received a
174          * warning from the stream control facilities (dd-edited
175          * stream ?). */
176         msg_Warn( p_input, "clock gap, unexpected stream discontinuity" );
177         input_ClockInit( cl, cl->b_master, cl->i_cr_average, cl->i_rate );
178         /* Feed synchro with a new reference point. */
179         msg_Warn( p_input, "feeding synchro with a new reference point trying to recover from clock gap" );
180         ClockNewRef( cl, i_clock,
181                          __MAX( cl->last_pts + CR_MEAN_PTS_GAP, i_mdate ) );
182         cl->i_synchro_state = SYNCHRO_OK;
183     }
184
185     cl->last_cr = i_clock;
186     cl->last_sysdate = i_mdate;
187
188     if( !b_synchronize && i_mdate - cl->last_update > 200000 )
189     {
190         /* Smooth clock reference variations. */
191         const mtime_t i_extrapoled_clock = ClockCurrent( cl );
192         /* Bresenham algorithm to smooth variations. */
193         const mtime_t i_tmp = cl->delta_cr * (cl->i_cr_average - 1) +
194                               ( i_extrapoled_clock - i_clock ) * 1  +
195                               cl->i_delta_cr_residue;
196
197         cl->i_delta_cr_residue = i_tmp % cl->i_cr_average;
198         cl->delta_cr           = i_tmp / cl->i_cr_average;
199
200         cl->last_update = i_mdate;
201     }
202 }
203
204 /*****************************************************************************
205  * input_ClockResetPCR:
206  *****************************************************************************/
207 void input_ClockResetPCR( input_clock_t *cl )
208 {
209     cl->i_synchro_state =  SYNCHRO_REINIT;
210     cl->last_pts = 0;
211 }
212
213 /*****************************************************************************
214  * input_ClockGetTS: manages a PTS or DTS
215  *****************************************************************************/
216 mtime_t input_ClockGetTS( input_thread_t * p_input,
217                           input_clock_t *cl, mtime_t i_ts )
218 {
219     if( cl->i_synchro_state != SYNCHRO_OK )
220         return 0;
221
222     cl->last_pts = ClockToSysdate( cl, i_ts + cl->delta_cr );
223     return cl->last_pts + p_input->i_pts_delay;
224 }
225
226 /*****************************************************************************
227  * input_ClockSetRate:
228  *****************************************************************************/
229 void input_ClockSetRate( input_clock_t *cl, int i_rate )
230 {
231     /* Move the reference point */
232     if( cl->i_synchro_state == SYNCHRO_OK )
233         ClockNewRef( cl, cl->last_cr, cl->last_sysdate );
234
235     cl->i_rate = i_rate;
236 }
237
238 /*****************************************************************************
239  * input_ClockGetWakeup
240  *****************************************************************************/
241 mtime_t input_ClockGetWakeup( input_thread_t *p_input, input_clock_t *cl )
242 {
243     /* Not synchronized, we cannot wait */
244     if( cl->i_synchro_state != SYNCHRO_OK )
245         return 0;
246
247     /* We must not wait if not pace controled, or we are not the
248      * master clock */
249     if( !p_input->b_can_pace_control || !cl->b_master ||
250         p_input->p->b_out_pace_control )
251         return 0;
252
253     /* */
254     return ClockToSysdate( cl, cl->last_cr );
255 }
256