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