]> git.sesse.net Git - vlc/blob - src/input/input_clock.c
* Functional fast forward and slow motion API ;
[vlc] / src / input / input_clock.c
1 /*****************************************************************************
2  * input_clock.c: Clock/System date convertions, stream management
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: input_clock.c,v 1.3 2001/02/07 17:44:52 massiot Exp $
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 "defs.h"
28
29 #include "config.h"
30 #include "common.h"
31 #include "threads.h"
32 #include "mtime.h"
33 #include "intf_msg.h"
34
35 #include "stream_control.h"
36 #include "input_ext-intf.h"
37 #include "input_ext-dec.h"
38
39 #include "input.h"
40
41 /*
42  * DISCUSSION : SYNCHRONIZATION METHOD
43  *
44  * In some cases we can impose the pace of reading (when reading from a
45  * file or a pipe), and for the synchronization we simply sleep() until
46  * it is time to deliver the packet to the decoders. When reading from
47  * the network, we must be read at the same pace as the server writes,
48  * otherwise the kernel's buffer will trash packets. The risk is now to
49  * overflow the input buffers in case the server goes too fast, that is
50  * why we do these calculations :
51  *
52  * We compute a mean for the pcr because we want to eliminate the
53  * network jitter and keep the low frequency variations. The mean is
54  * in fact a low pass filter and the jitter is a high frequency signal
55  * that is why it is eliminated by the filter/average.
56  *
57  * The low frequency variations enable us to synchronize the client clock
58  * with the server clock because they represent the time variation between
59  * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
60  * the presentation dates for the audio and video frames. With those dates
61  * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
62  * as it is sent by the server and so we keep the synchronization between
63  * the server and the client.
64  *
65  * It is a very important matter if you want to avoid underflow or overflow
66  * in all the FIFOs, but it may be not enough.
67  */
68
69 /*****************************************************************************
70  * Constants
71  *****************************************************************************/
72
73 /* Maximum number of samples used to compute the dynamic average value.
74  * We use the following formula :
75  * new_average = (old_average * c_average + new_sample_value) / (c_average +1)
76  */
77 #define CR_MAX_AVERAGE_COUNTER 40
78
79 /* Maximum gap allowed between two CRs. */
80 #define CR_MAX_GAP 1000000
81
82 /*****************************************************************************
83  * ClockToSysdate: converts a movie clock to system date
84  *****************************************************************************/
85 static mtime_t ClockToSysdate( input_thread_t * p_input,
86                                pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
87 {
88     mtime_t     i_sysdate = 0;
89
90     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
91     {
92         i_sysdate = (i_clock - p_pgrm->cr_ref) 
93                         * p_input->stream.control.i_rate
94                         * 300
95                         / 27
96                         / DEFAULT_RATE
97                         + p_pgrm->sysdate_ref;
98     }
99
100     return( i_sysdate );
101 }
102
103 /*****************************************************************************
104  * ClockCurrent: converts current system date to clock units
105  *****************************************************************************
106  * Caution : the synchro state must be SYNCHRO_OK for this to operate.
107  *****************************************************************************/
108 static mtime_t ClockCurrent( input_thread_t * p_input,
109                              pgrm_descriptor_t * p_pgrm )
110 {
111     return( (mdate() - p_pgrm->sysdate_ref) * 27 * DEFAULT_RATE
112              / p_input->stream.control.i_rate / 300
113              + p_pgrm->cr_ref );
114 }
115
116 /*****************************************************************************
117  * input_ClockNewRef: writes a new clock reference
118  *****************************************************************************/
119 void input_ClockNewRef( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
120                         mtime_t i_clock, mtime_t i_sysdate )
121 {
122     intf_WarnMsg( 1, "Old ref: %lld/%lld, New ref: %lld/%lld", p_pgrm->cr_ref,
123               p_pgrm->sysdate_ref, i_clock, i_sysdate );
124     p_pgrm->cr_ref = i_clock;
125     p_pgrm->sysdate_ref = i_sysdate;
126 }
127
128 /*****************************************************************************
129  * EscapeDiscontinuity: send a NULL packet to the decoders
130  *****************************************************************************/
131 static void EscapeDiscontinuity( input_thread_t * p_input,
132                                  pgrm_descriptor_t * p_pgrm )
133 {
134     int     i_es;
135
136     for( i_es = 0; i_es < p_pgrm->i_es_number; i_es++ )
137     {
138         es_descriptor_t * p_es = p_pgrm->pp_es[i_es];
139
140         if( p_es->p_decoder_fifo != NULL )
141         {
142             input_NullPacket( p_input, p_es );
143         }
144     }
145 }
146
147 /*****************************************************************************
148  * EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
149  *****************************************************************************/
150 static void EscapeAudioDiscontinuity( input_thread_t * p_input,
151                                       pgrm_descriptor_t * p_pgrm )
152 {
153     int     i_es;
154
155     for( i_es = 0; i_es < p_pgrm->i_es_number; i_es++ )
156     {
157         es_descriptor_t * p_es = p_pgrm->pp_es[i_es];
158
159         if( p_es->p_decoder_fifo != NULL && p_es->b_audio )
160         {
161             input_NullPacket( p_input, p_es );
162         }
163     }
164 }
165
166 /*****************************************************************************
167  * input_ClockInit: reinitializes the clock reference after a stream
168  *                  discontinuity
169  *****************************************************************************/
170 void input_ClockInit( pgrm_descriptor_t * p_pgrm )
171 {
172     p_pgrm->last_cr = 0;
173     p_pgrm->cr_ref = 0;
174     p_pgrm->sysdate_ref = 0;
175     p_pgrm->delta_cr = 0;
176     p_pgrm->c_average_count = 0;
177 }
178
179 /*****************************************************************************
180  * input_ClockManageRef: manages a clock reference
181  *****************************************************************************/
182 void input_ClockManageRef( input_thread_t * p_input,
183                            pgrm_descriptor_t * p_pgrm, mtime_t i_clock )
184 {
185     if( p_pgrm->i_synchro_state != SYNCHRO_OK )
186     {
187         /* Feed synchro with a new reference point. */
188         input_ClockNewRef( p_input, p_pgrm, i_clock, mdate() );
189         p_pgrm->i_synchro_state = SYNCHRO_OK;
190     }
191     else
192     {
193         if ( p_pgrm->last_cr != 0 &&
194                (    (p_pgrm->last_cr - i_clock) > CR_MAX_GAP
195                  || (p_pgrm->last_cr - i_clock) < - CR_MAX_GAP ) )
196         {
197             /* Stream discontinuity, for which we haven't received a
198              * warning from the stream control facilities (dd-edited
199              * stream ?). */
200             intf_WarnMsg( 3, "Clock gap, unexpected stream discontinuity" );
201             input_ClockInit( p_pgrm );
202             p_pgrm->i_synchro_state = SYNCHRO_START;
203             input_EscapeDiscontinuity( p_input, p_pgrm );
204         }
205
206         p_pgrm->last_cr = i_clock;
207
208         if( p_input->stream.b_pace_control
209              && p_input->stream.pp_programs[0] == p_pgrm )
210         {
211             /* Wait a while before delivering the packets to the decoder.
212              * In case of multiple programs, we arbitrarily follow the
213              * clock of the first program. */
214             mwait( ClockToSysdate( p_input, p_pgrm, i_clock ) );
215
216             /* Now take into account interface changes. */
217             vlc_mutex_lock( &p_input->stream.stream_lock );
218             if( p_input->stream.i_new_status != UNDEF_S )
219             {
220                 /* For the moment, only PLAYING_S and FORWARD_S are
221                  * supported. */
222                 input_ClockNewRef( p_input, p_pgrm, i_clock,
223                                    ClockToSysdate( p_input, p_pgrm, i_clock ) );
224
225                 vlc_mutex_lock( &p_input->stream.control.control_lock );
226                 p_input->stream.control.i_status = p_input->stream.i_new_status;
227
228                 if( p_input->stream.control.i_status != PLAYING_S )
229                 {
230                     p_input->stream.control.i_rate = p_input->stream.i_new_rate;
231                     p_input->stream.control.b_mute = 1;
232
233                     /* Feed the audio decoders with a NULL packet to avoid
234                      * discontinuities. */
235                     EscapeAudioDiscontinuity( p_input, p_pgrm );
236                 }
237                 else
238                 {
239                     p_input->stream.control.i_rate = DEFAULT_RATE;
240                     p_input->stream.control.b_mute = 0;
241                 }
242                 vlc_mutex_unlock( &p_input->stream.control.control_lock );
243
244                 p_input->stream.i_new_status = UNDEF_S;
245                 p_input->stream.i_new_rate = UNDEF_S;
246             }
247             vlc_mutex_unlock( &p_input->stream.stream_lock );
248         }
249         else
250         {
251             /* Smooth clock reference variations. */
252             mtime_t     i_extrapoled_clock = ClockCurrent( p_input, p_pgrm );
253
254             /* Bresenham algorithm to smooth variations. */
255             if( p_pgrm->c_average_count == CR_MAX_AVERAGE_COUNTER )
256             {
257                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
258                                         * (CR_MAX_AVERAGE_COUNTER - 1)
259                                       + i_extrapoled_clock )
260                                     / CR_MAX_AVERAGE_COUNTER;
261             }
262             else
263             {
264                 p_pgrm->delta_cr = ( p_pgrm->delta_cr
265                                         * p_pgrm->c_average_count
266                                       + i_extrapoled_clock )
267                                     / (p_pgrm->c_average_count + 1);
268                 p_pgrm->c_average_count++;
269             }
270         }
271     }
272 }
273
274 /*****************************************************************************
275  * input_ClockGetTS: manages a PTS or DTS
276  *****************************************************************************/
277 mtime_t input_ClockGetTS( input_thread_t * p_input,
278                           pgrm_descriptor_t * p_pgrm, mtime_t i_ts )
279 {
280     if( p_pgrm->i_synchro_state == SYNCHRO_OK )
281     {
282         return( ClockToSysdate( p_input, p_pgrm, i_ts + p_pgrm->delta_cr )
283                  + DEFAULT_PTS_DELAY );
284     }
285     else
286     {
287         return 0;
288     }
289 }
290