]> git.sesse.net Git - vlc/blob - src/input/clock.c
Privatized input_clock_t to clock.c
[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 /*****************************************************************************
70  * Constants
71  *****************************************************************************/
72
73 /* Maximum gap allowed between two CRs. */
74 #define CR_MAX_GAP (INT64_C(2000000)*100/9)
75
76 /* Latency introduced on DVDs with CR == 0 on chapter change - this is from
77  * my dice --Meuuh */
78 #define CR_MEAN_PTS_GAP 300000
79
80 /*****************************************************************************
81  * Structures
82  *****************************************************************************/
83 struct input_clock_t
84 {
85     /* Synchronization information */
86     mtime_t                 delta_cr;
87     mtime_t                 cr_ref, sysdate_ref;
88     mtime_t                 last_sysdate;
89     mtime_t                 last_cr; /* reference to detect unexpected stream
90                                       * discontinuities                      */
91     mtime_t                 last_pts;
92     mtime_t                 last_update;
93     bool                    b_has_reference;
94
95     bool                    b_master;
96
97     int                     i_rate;
98
99     /* Config */
100     int                     i_cr_average;
101     int                     i_delta_cr_residue;
102 };
103
104 /*****************************************************************************
105  * ClockToSysdate: converts a movie clock to system date
106  *****************************************************************************/
107 static mtime_t ClockToSysdate( input_clock_t *cl, mtime_t i_clock )
108 {
109     if( !cl->b_has_reference )
110         return 0;
111
112     return (i_clock - cl->cr_ref) * cl->i_rate / INPUT_RATE_DEFAULT +
113            cl->sysdate_ref;
114 }
115
116 /*****************************************************************************
117  * ClockFromSysdate: converts a system date to movie clock
118  *****************************************************************************
119  * Caution : a valid reference point is needed for this to operate.
120  *****************************************************************************/
121 static mtime_t ClockFromSysdate( input_clock_t *cl, mtime_t i_ck_system )
122 {
123     assert( cl->b_has_reference );
124     return ( i_ck_system - cl->sysdate_ref ) * INPUT_RATE_DEFAULT / cl->i_rate +
125             cl->cr_ref;
126 }
127
128 /*****************************************************************************
129  * ClockNewRef: writes a new clock reference
130  *****************************************************************************/
131 static void ClockNewRef( input_clock_t *cl,
132                          mtime_t i_clock, mtime_t i_sysdate )
133 {
134     cl->b_has_reference = true;
135     cl->cr_ref = i_clock;
136     cl->sysdate_ref = i_sysdate ;
137 }
138
139 /*****************************************************************************
140  * input_ClockNew: create a new clock
141  *****************************************************************************/
142 input_clock_t *input_ClockNew( bool b_master, int i_cr_average, int i_rate )
143 {
144     input_clock_t *cl = malloc( sizeof(*cl) );
145     if( !cl )
146         return NULL;
147
148     cl->b_has_reference = false;
149
150     cl->last_cr = 0;
151     cl->last_pts = 0;
152     cl->last_sysdate = 0;
153     cl->cr_ref = 0;
154     cl->sysdate_ref = 0;
155     cl->delta_cr = 0;
156     cl->i_delta_cr_residue = 0;
157     cl->i_rate = i_rate;
158
159     cl->i_cr_average = i_cr_average;
160
161     cl->b_master = b_master;
162
163     return cl;
164 }
165
166 /*****************************************************************************
167  * input_ClockDelete: destroy a new clock
168  *****************************************************************************/
169 void input_ClockDelete( input_clock_t *cl )
170 {
171     free( cl );
172 }
173
174 /*****************************************************************************
175  * input_ClockSetPCR: manages a clock reference
176  *
177  *  i_ck_stream: date in stream clock
178  *  i_ck_system: date in system clock
179  *****************************************************************************/
180 void input_ClockSetPCR( input_thread_t *p_input,
181                         input_clock_t *cl,
182                         mtime_t i_ck_stream, mtime_t i_ck_system )
183 {
184     const bool b_synchronize = p_input->b_can_pace_control && cl->b_master;
185     bool b_reset_reference = false;
186
187     if( ( !cl->b_has_reference ) ||
188         ( i_ck_stream == 0 && cl->last_cr != 0 ) )
189     {
190         cl->last_update = 0;
191
192         /* */
193         b_reset_reference= true;
194     }
195     else if ( cl->last_cr != 0 &&
196               ( (cl->last_cr - i_ck_stream) > CR_MAX_GAP ||
197                 (cl->last_cr - i_ck_stream) < - CR_MAX_GAP ) )
198     {
199         /* Stream discontinuity, for which we haven't received a
200          * warning from the stream control facilities (dd-edited
201          * stream ?). */
202         msg_Warn( p_input, "clock gap, unexpected stream discontinuity" );
203         cl->last_pts = 0;
204
205         /* */
206         msg_Warn( p_input, "feeding synchro with a new reference point trying to recover from clock gap" );
207         b_reset_reference= true;
208     }
209     if( b_reset_reference )
210     {
211         cl->delta_cr = 0;
212         cl->i_delta_cr_residue = 0;
213
214         /* Feed synchro with a new reference point. */
215         ClockNewRef( cl, i_ck_stream,
216                          __MAX( cl->last_pts + CR_MEAN_PTS_GAP, i_ck_system ) );
217     }
218
219     cl->last_cr = i_ck_stream;
220     cl->last_sysdate = i_ck_system;
221
222     if( !b_synchronize && i_ck_system - cl->last_update > 200000 )
223     {
224         /* Smooth clock reference variations. */
225         const mtime_t i_extrapoled_clock = ClockFromSysdate( cl, i_ck_system );
226         /* Bresenham algorithm to smooth variations. */
227         const mtime_t i_tmp = cl->delta_cr * (cl->i_cr_average - 1) +
228                               ( i_extrapoled_clock - i_ck_stream ) * 1  +
229                               cl->i_delta_cr_residue;
230
231         cl->i_delta_cr_residue = i_tmp % cl->i_cr_average;
232         cl->delta_cr           = i_tmp / cl->i_cr_average;
233
234         cl->last_update = i_ck_system;
235     }
236 }
237
238 /*****************************************************************************
239  * input_ClockResetPCR:
240  *****************************************************************************/
241 void input_ClockResetPCR( input_clock_t *cl )
242 {
243     cl->b_has_reference = false;
244     cl->last_pts = 0;
245 }
246
247 /*****************************************************************************
248  * input_ClockGetTS: manages a PTS or DTS
249  *****************************************************************************/
250 mtime_t input_ClockGetTS( input_thread_t * p_input,
251                           input_clock_t *cl, mtime_t i_ts )
252 {
253     if( !cl->b_has_reference )
254         return 0;
255
256     cl->last_pts = ClockToSysdate( cl, i_ts + cl->delta_cr );
257     return cl->last_pts + p_input->i_pts_delay;
258 }
259
260 /*****************************************************************************
261  * input_ClockSetRate:
262  *****************************************************************************/
263 void input_ClockSetRate( input_clock_t *cl, int i_rate )
264 {
265     /* Move the reference point */
266     if( cl->b_has_reference )
267         ClockNewRef( cl, cl->last_cr, cl->last_sysdate );
268
269     cl->i_rate = i_rate;
270 }
271
272 /*****************************************************************************
273  * input_ClockSetMaster:
274  *****************************************************************************/
275 void input_ClockSetMaster( input_clock_t *cl, bool b_master )
276 {
277     cl->b_master = b_master;
278 }
279
280 /*****************************************************************************
281  * input_ClockGetWakeup
282  *****************************************************************************/
283 mtime_t input_ClockGetWakeup( input_thread_t *p_input, input_clock_t *cl )
284 {
285     /* Not synchronized, we cannot wait */
286     if( !cl->b_has_reference )
287         return 0;
288
289     /* We must not wait if not pace controled, or we are not the
290      * master clock */
291     if( !p_input->b_can_pace_control || !cl->b_master ||
292         p_input->p->b_out_pace_control )
293         return 0;
294
295     /* */
296     return ClockToSysdate( cl, cl->last_cr );
297 }
298