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